summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2023-07-19 17:05:44 -0500
committerGravatar GitHub <noreply@github.com> 2023-07-19 17:05:44 -0500
commit77ffcc8f8b0ca9f8b9da29525f03028e666fd8df (patch)
tree67913b0a87d5f410e223e95463a4bd0fb9cd78b5
parent7a3f4efcd96a8de04fe1a5afdb7e526650cb506d (diff)
downloadastro-77ffcc8f8b0ca9f8b9da29525f03028e666fd8df.tar.gz
astro-77ffcc8f8b0ca9f8b9da29525f03028e666fd8df.tar.zst
astro-77ffcc8f8b0ca9f8b9da29525f03028e666fd8df.zip
fix(sitemap): ensure nested 404 and 500 pages are excluded (#7722)
-rw-r--r--.changeset/fuzzy-toes-float.md5
-rw-r--r--packages/integrations/sitemap/src/index.ts14
-rw-r--r--packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro8
-rw-r--r--packages/integrations/sitemap/test/staticPaths.test.js4
4 files changed, 28 insertions, 3 deletions
diff --git a/.changeset/fuzzy-toes-float.md b/.changeset/fuzzy-toes-float.md
new file mode 100644
index 000000000..c66b28c7f
--- /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 f9ef360fb..2094aa3b1 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 000000000..9e307c5c2
--- /dev/null
+++ b/packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro
@@ -0,0 +1,8 @@
+<html>
+ <head>
+ <title>404</title>
+ </head>
+ <body>
+ <h1>404</h1>
+ </body>
+</html>
diff --git a/packages/integrations/sitemap/test/staticPaths.test.js b/packages/integrations/sitemap/test/staticPaths.test.js
index 3365ff1e8..d5d95b2d3 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/');
});