diff --git a/.changeset/fuzzy-toes-float.md b/.changeset/fuzzy-toes-float.md new file mode 100644 index 000000000000..c66b28c7f51d --- /dev/null +++ b/.changeset/fuzzy-toes-float.md @@ -0,0 +1,5 @@ +--- +'@astrojs/sitemap': patch +--- + +Ensure nested 404 and 500 pages are always excluded diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index f9ef360fb790..2094aa3b193a 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -49,7 +49,15 @@ function formatConfigErrorMessage(err: ZodError) { const PKG_NAME = '@astrojs/sitemap'; const OUTFILE = 'sitemap-index.xml'; -const STATUS_CODE_PAGES = new Set(['/404', '/500']); +const STATUS_CODE_PAGES = new Set(['404', '500']); + +function isStatusCodePage(pathname: string): boolean { + if (pathname.endsWith('/')) { + pathname = pathname.slice(0, -1); + } + const end = pathname.split('/').pop() ?? ''; + return STATUS_CODE_PAGES.has(end); +} const createPlugin = (options?: SitemapOptions): AstroIntegration => { let config: AstroConfig; @@ -87,7 +95,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { } let pageUrls = pages - .filter((p) => !STATUS_CODE_PAGES.has('/' + p.pathname.slice(0, -1))) + .filter((p) => !isStatusCodePage(p.pathname)) .map((p) => { if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/')) finalSiteUrl.pathname += '/'; @@ -103,7 +111,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { * Dynamic URLs have entries with `undefined` pathnames */ if (r.pathname) { - if (STATUS_CODE_PAGES.has(r.pathname)) return urls; + if (isStatusCodePage(r.pathname ?? r.route)) return urls; /** * remove the initial slash from relative pathname * because `finalSiteUrl` always has trailing slash diff --git a/packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro b/packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro new file mode 100644 index 000000000000..9e307c5c292c --- /dev/null +++ b/packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro @@ -0,0 +1,8 @@ + + + 404 + + +

404

+ + diff --git a/packages/integrations/sitemap/test/staticPaths.test.js b/packages/integrations/sitemap/test/staticPaths.test.js index 3365ff1e8429..d5d95b2d3afd 100644 --- a/packages/integrations/sitemap/test/staticPaths.test.js +++ b/packages/integrations/sitemap/test/staticPaths.test.js @@ -26,6 +26,10 @@ describe('getStaticPaths support', () => { expect(urls).to.not.include('http://example.com/404/'); }); + it('does not include nested 404 pages', () => { + expect(urls).to.not.include('http://example.com/de/404/'); + }); + it('includes numerical pages', () => { expect(urls).to.include('http://example.com/123/'); });