diff options
author | 2022-06-08 12:07:12 -0300 | |
---|---|---|
committer | 2022-06-08 10:07:12 -0500 | |
commit | c601ce59b5740e7ff48c6575a6168d6a2408f7a3 (patch) | |
tree | 520b6a14a769be9aa97980e42b99f4a470c15c42 | |
parent | 16cf649e0a3413420edbd5dea68366e2aa2f8001 (diff) | |
download | astro-c601ce59b5740e7ff48c6575a6168d6a2408f7a3.tar.gz astro-c601ce59b5740e7ff48c6575a6168d6a2408f7a3.tar.zst astro-c601ce59b5740e7ff48c6575a6168d6a2408f7a3.zip |
Prevent sitemap URLs without pathname (#3553)
* fix(@astrojs/sitemap): handle base/pathname correctly
* chore: add changeset
-rw-r--r-- | .changeset/large-rings-invite.md | 5 | ||||
-rw-r--r-- | packages/integrations/sitemap/src/index.ts | 14 |
2 files changed, 16 insertions, 3 deletions
diff --git a/.changeset/large-rings-invite.md b/.changeset/large-rings-invite.md new file mode 100644 index 000000000..edf16f8d0 --- /dev/null +++ b/.changeset/large-rings-invite.md @@ -0,0 +1,5 @@ +--- +'@astrojs/sitemap': patch +--- + +Prevent sitemap URLs with trimmed paths diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 1c1cf29ab..e43b8f6d6 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -61,14 +61,22 @@ export default function createPlugin({ config = _config; }, 'astro:build:done': async ({ pages, dir }) => { - const finalSiteUrl = canonicalURL || config.site; - if (!finalSiteUrl) { + let finalSiteUrl: URL; + if (canonicalURL) { + finalSiteUrl = new URL(canonicalURL); + finalSiteUrl.pathname += finalSiteUrl.pathname.endsWith('/') ? '' : '/'; // normalizes the final url since it's provided by user + } else if (config.site) { + finalSiteUrl = new URL(config.base, config.site); + } else { console.warn( 'The Sitemap integration requires either the `site` astro.config option or `canonicalURL` integration option. Skipping.' ); return; } - let pageUrls = pages.map((p) => new URL(p.pathname, finalSiteUrl).href); + let pageUrls = pages.map((p) => { + const path = finalSiteUrl.pathname + p.pathname + return new URL(path, finalSiteUrl).href + }); if (filter) { pageUrls = pageUrls.filter((page: string) => filter(page)); } |