0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

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
This commit is contained in:
Ming-jun Lu 2024-05-15 22:33:50 +08:00 committed by GitHub
parent 530ef95a20
commit 841df1f1b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 11 deletions

View file

@ -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`.

View file

@ -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(
@ -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' };
}
@ -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(
@ -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;

View file

@ -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 theres no extension or `trailingSlash` is true
pathname = pathname.replace(/\/*$/, '/');
}
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() wont)
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 */

View file

@ -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<'));
});