diff --git a/.changeset/tame-otters-destroy.md b/.changeset/tame-otters-destroy.md new file mode 100644 index 000000000000..b9416a1bedf2 --- /dev/null +++ b/.changeset/tame-otters-destroy.md @@ -0,0 +1,5 @@ +--- +"@astrojs/rss": patch +--- + +Fixes an issue where trailing slash is not removed even if the `trailingSlash` option is set to `false`. diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index b3e0253d2ce7..89be50309bcf 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -202,7 +202,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise { root.rss.channel = { title: rssOptions.title, description: rssOptions.description, - link: createCanonicalURL(site, rssOptions.trailingSlash, undefined).href, + link: createCanonicalURL(site, rssOptions.trailingSlash, undefined), }; if (typeof rssOptions.customData === 'string') Object.assign( @@ -220,7 +220,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise { // If the item's link is already a valid URL, don't mess with it. const itemLink = isValidURL(result.link) ? result.link - : createCanonicalURL(result.link, rssOptions.trailingSlash, site).href; + : createCanonicalURL(result.link, rssOptions.trailingSlash, site); item.link = itemLink; item.guid = { '#text': itemLink, '@_isPermaLink': 'true' }; } @@ -246,7 +246,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise { if (typeof result.commentsUrl === 'string') { item.comments = isValidURL(result.commentsUrl) ? result.commentsUrl - : createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site).href; + : createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site); } if (result.source) { item.source = parser.parse( @@ -256,7 +256,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise { if (result.enclosure) { const enclosureURL = isValidURL(result.enclosure.url) ? result.enclosure.url - : createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site).href; + : createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site); item.enclosure = parser.parse( `` ).enclosure; diff --git a/packages/astro-rss/src/util.ts b/packages/astro-rss/src/util.ts index 1e49b3d77521..16db5b587191 100644 --- a/packages/astro-rss/src/util.ts +++ b/packages/astro-rss/src/util.ts @@ -6,18 +6,21 @@ export function createCanonicalURL( url: string, trailingSlash?: RSSOptions['trailingSlash'], base?: string -): URL { +): string { let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical - if (trailingSlash === false) { - // remove the trailing slash - pathname = pathname.replace(/\/*$/, ''); - } else if (!getUrlExtension(url)) { + if (!getUrlExtension(url)) { // add trailing slash if there’s no extension or `trailingSlash` is true pathname = pathname.replace(/\/*$/, '/'); } pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t) - return new URL(pathname, base); + + const canonicalUrl = new URL(pathname, base).href; + if (trailingSlash === false) { + // remove the trailing slash + return canonicalUrl.replace(/\/*$/, ''); + } + return canonicalUrl; } /** Check if a URL is already valid */ diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 764cc1301b32..547b75b4e634 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -176,7 +176,7 @@ describe('getRssString', () => { trailingSlash: false, }); - assert.ok(str.includes('https://example.com/<')); + assert.ok(str.includes('https://example.com<')); assert.ok(str.includes('https://example.com/php<')); });