Skip to content

Commit

Permalink
fix(sitemap): ensure nested 404 and 500 pages are excluded (#7722)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Jul 19, 2023
1 parent 7a3f4ef commit 77ffcc8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-toes-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Ensure nested 404 and 500 pages are always excluded
14 changes: 11 additions & 3 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 += '/';
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>404</title>
</head>
<body>
<h1>404</h1>
</body>
</html>
4 changes: 4 additions & 0 deletions packages/integrations/sitemap/test/staticPaths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
});
Expand Down

0 comments on commit 77ffcc8

Please sign in to comment.