Skip to content

Commit 841df1f

Browse files
authoredMay 15, 2024··
fix(rss): fix an issue where trailing slash is not removed even if trailingSlash 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
1 parent 530ef95 commit 841df1f

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed
 

‎.changeset/tame-otters-destroy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/rss": patch
3+
---
4+
5+
Fixes an issue where trailing slash is not removed even if the `trailingSlash` option is set to `false`.

‎packages/astro-rss/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
202202
root.rss.channel = {
203203
title: rssOptions.title,
204204
description: rssOptions.description,
205-
link: createCanonicalURL(site, rssOptions.trailingSlash, undefined).href,
205+
link: createCanonicalURL(site, rssOptions.trailingSlash, undefined),
206206
};
207207
if (typeof rssOptions.customData === 'string')
208208
Object.assign(
@@ -220,7 +220,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
220220
// If the item's link is already a valid URL, don't mess with it.
221221
const itemLink = isValidURL(result.link)
222222
? result.link
223-
: createCanonicalURL(result.link, rssOptions.trailingSlash, site).href;
223+
: createCanonicalURL(result.link, rssOptions.trailingSlash, site);
224224
item.link = itemLink;
225225
item.guid = { '#text': itemLink, '@_isPermaLink': 'true' };
226226
}
@@ -246,7 +246,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
246246
if (typeof result.commentsUrl === 'string') {
247247
item.comments = isValidURL(result.commentsUrl)
248248
? result.commentsUrl
249-
: createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site).href;
249+
: createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site);
250250
}
251251
if (result.source) {
252252
item.source = parser.parse(
@@ -256,7 +256,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
256256
if (result.enclosure) {
257257
const enclosureURL = isValidURL(result.enclosure.url)
258258
? result.enclosure.url
259-
: createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site).href;
259+
: createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site);
260260
item.enclosure = parser.parse(
261261
`<enclosure url="${enclosureURL}" length="${result.enclosure.length}" type="${result.enclosure.type}"/>`
262262
).enclosure;

‎packages/astro-rss/src/util.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ export function createCanonicalURL(
66
url: string,
77
trailingSlash?: RSSOptions['trailingSlash'],
88
base?: string
9-
): URL {
9+
): string {
1010
let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
11-
if (trailingSlash === false) {
12-
// remove the trailing slash
13-
pathname = pathname.replace(/\/*$/, '');
14-
} else if (!getUrlExtension(url)) {
11+
if (!getUrlExtension(url)) {
1512
// add trailing slash if there’s no extension or `trailingSlash` is true
1613
pathname = pathname.replace(/\/*$/, '/');
1714
}
1815

1916
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
20-
return new URL(pathname, base);
17+
18+
const canonicalUrl = new URL(pathname, base).href;
19+
if (trailingSlash === false) {
20+
// remove the trailing slash
21+
return canonicalUrl.replace(/\/*$/, '');
22+
}
23+
return canonicalUrl;
2124
}
2225

2326
/** Check if a URL is already valid */

‎packages/astro-rss/test/rss.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ describe('getRssString', () => {
176176
trailingSlash: false,
177177
});
178178

179-
assert.ok(str.includes('https://example.com/<'));
179+
assert.ok(str.includes('https://example.com<'));
180180
assert.ok(str.includes('https://example.com/php<'));
181181
});
182182

0 commit comments

Comments
 (0)
Please sign in to comment.