Skip to content

Commit

Permalink
fix(rss): fix an issue where trailing slash is not removed even if `t…
Browse files Browse the repository at this point in the history
…railingSlash` is set to `false` (#11050)

* refactor(createCanonicalURL): return string instead of URL object

* fix(rss): fix an issue where trailing slash is not removed even if `trailingSlash` is set to `false`

* test(rss): update test case related to trailing slash

* chore: add changeset
  • Loading branch information
mingjunlu committed May 15, 2024
1 parent 530ef95 commit 841df1f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-otters-destroy.md
Original file line number Diff line number Diff line change
@@ -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`.
8 changes: 4 additions & 4 deletions packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
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(
Expand All @@ -220,7 +220,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
// 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' };
}
Expand All @@ -246,7 +246,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
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(
Expand All @@ -256,7 +256,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
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 url="${enclosureURL}" length="${result.enclosure.length}" type="${result.enclosure.type}"/>`
).enclosure;
Expand Down
15 changes: 9 additions & 6 deletions packages/astro-rss/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<'));
});

Expand Down

0 comments on commit 841df1f

Please sign in to comment.