diff options
author | 2024-05-15 22:33:50 +0800 | |
---|---|---|
committer | 2024-05-15 15:33:50 +0100 | |
commit | 841df1f1b192f39849509cda49b7243940cc30f9 (patch) | |
tree | b8614c678f275765ebb68dc016a1f896cb7f0efe /packages/astro-rss/src/util.ts | |
parent | 530ef95a20dee8027757aa1677d8af5f86cf9912 (diff) | |
download | astro-841df1f1b192f39849509cda49b7243940cc30f9.tar.gz astro-841df1f1b192f39849509cda49b7243940cc30f9.tar.zst astro-841df1f1b192f39849509cda49b7243940cc30f9.zip |
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
Diffstat (limited to 'packages/astro-rss/src/util.ts')
-rw-r--r-- | packages/astro-rss/src/util.ts | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/packages/astro-rss/src/util.ts b/packages/astro-rss/src/util.ts index 1e49b3d77..16db5b587 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 */ |