diff options
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 */ |