fix(sitemap): ensure nested 404 and 500 pages are excluded (#7722)
This commit is contained in:
parent
7a3f4efcd9
commit
77ffcc8f8b
4 changed files with 28 additions and 3 deletions
5
.changeset/fuzzy-toes-float.md
Normal file
5
.changeset/fuzzy-toes-float.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/sitemap': patch
|
||||
---
|
||||
|
||||
Ensure nested 404 and 500 pages are always excluded
|
|
@ -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
|
||||
|
|
8
packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro
vendored
Normal file
8
packages/integrations/sitemap/test/fixtures/static/src/pages/de/404.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>404</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>404</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -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/');
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue