summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Zade Viggers <74938858+zadeviggers@users.noreply.github.com> 2022-02-03 18:39:18 +1300
committerGravatar GitHub <noreply@github.com> 2022-02-02 21:39:18 -0800
commited4969d1132c9e7d59382b3d40acf350106f3a38 (patch)
tree93976461173df658587a0948876addb91b039e23
parent81238bf77d9d2234b23b58b1c770022e4765b3b3 (diff)
downloadastro-ed4969d1132c9e7d59382b3d40acf350106f3a38.tar.gz
astro-ed4969d1132c9e7d59382b3d40acf350106f3a38.tar.zst
astro-ed4969d1132c9e7d59382b3d40acf350106f3a38.zip
Handles all http error code file names the same as 404 files. (#2525)
* Fix #2195 * Filter out error code files from sitemap
Diffstat (limited to '')
-rw-r--r--packages/astro/src/core/ssr/sitemap.ts5
-rw-r--r--packages/astro/src/vite-plugin-build-html/index.ts6
2 files changed, 7 insertions, 4 deletions
diff --git a/packages/astro/src/core/ssr/sitemap.ts b/packages/astro/src/core/ssr/sitemap.ts
index e392dd368..d641113f8 100644
--- a/packages/astro/src/core/ssr/sitemap.ts
+++ b/packages/astro/src/core/ssr/sitemap.ts
@@ -1,3 +1,5 @@
+const ERROR_STATUS_CODE_REGEXES = [ /400\/?$/,/401\/?$/,/402\/?$/,/403\/?$/,/404\/?$/,/405\/?$/,/406\/?$/,/407\/?$/,/408\/?$/,/409\/?$/,/410\/?$/,/411\/?$/,/412\/?$/,/413\/?$/,/414\/?$/,/415\/?$/,/416\/?$/,/417\/?$/,/418\/?$/,/421\/?$/,/422\/?$/,/423\/?$/,/424\/?$/,/425\/?$/,/426\/?$/,/428\/?$/,/429\/?$/,/431\/?$/,/451\/?$/,/500\/?$/,/501\/?$/,/502\/?$/,/503\/?$/,/504\/?$/,/505\/?$/,/506\/?$/,/507\/?$/,/508\/?$/,/510\/?$/,/511\/?$/ ];
+
/** Construct sitemap.xml given a set of URLs */
export function generateSitemap(pages: string[]): string {
// TODO: find way to respect <link rel="canonical"> URLs here
@@ -6,7 +8,8 @@ export function generateSitemap(pages: string[]): string {
// copy just in case original copy is needed
// make sure that 404 page is excluded
- const urls = [...pages].filter((url) => !/404\/?$/.test(url));
+ // also works for other error pages
+ const urls = [...pages].filter((url) => !ERROR_STATUS_CODE_REGEXES.find((code) => code.test(url)));
urls.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); // sort alphabetically so sitemap is same each time
let sitemap = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
for (const url of urls) {
diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts
index 542340581..8c4016658 100644
--- a/packages/astro/src/vite-plugin-build-html/index.ts
+++ b/packages/astro/src/vite-plugin-build-html/index.ts
@@ -25,7 +25,7 @@ const ASTRO_PAGE_PREFIX = '@astro-page';
const ASTRO_SCRIPT_PREFIX = '@astro-script';
const ASTRO_EMPTY = '@astro-empty';
-const STATUS_CODE_RE = /^404$/;
+const ERROR_STATUS_CODES = [ '400', '401', '402', '403', '404', '405', '406', '407', '408', '409', '410', '411', '412', '413', '414', '415', '416', '417', '418', '421', '422', '423', '424', '425', '426', '428', '429', '431', '451', '500', '501', '502', '503', '504', '505', '506', '507', '508', '510', '511' ];
interface PluginOptions {
astroConfig: AstroConfig;
@@ -481,9 +481,9 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
const name = pathname.substr(1);
let outPath: string;
- // Output directly to 404.html rather than 400/index.html
+ // Output directly to 404.html rather than 404/index.html
// Supports any other status codes, too
- if (name.match(STATUS_CODE_RE) || astroConfig.buildOptions.pageUrlFormat === 'file') {
+ if (ERROR_STATUS_CODES.find(code => code === name) || astroConfig.buildOptions.pageUrlFormat === 'file') {
outPath = `${removeEndingForwardSlash(name || 'index')}.html`;
} else {
outPath = npath.posix.join(name, 'index.html');