summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Fred K. Schott <fkschott@gmail.com> 2022-02-02 21:50:01 -0800
committerGravatar Fred K. Schott <fkschott@gmail.com> 2022-02-03 12:16:08 -0800
commit069e497a74ffbba644916980b1873ebbf01bfbd5 (patch)
treeb7cb8d261d6e05ad24f50f68349e8c0932487077
parenteb29cd5d339c09d78ecb858bd47a51fea2720453 (diff)
downloadastro-069e497a74ffbba644916980b1873ebbf01bfbd5.tar.gz
astro-069e497a74ffbba644916980b1873ebbf01bfbd5.tar.zst
astro-069e497a74ffbba644916980b1873ebbf01bfbd5.zip
simplify status code regex
-rw-r--r--packages/astro/src/core/ssr/sitemap.ts45
-rw-r--r--packages/astro/src/vite-plugin-build-html/index.ts46
2 files changed, 4 insertions, 87 deletions
diff --git a/packages/astro/src/core/ssr/sitemap.ts b/packages/astro/src/core/ssr/sitemap.ts
index c4f85b556..a5ef54f6a 100644
--- a/packages/astro/src/core/ssr/sitemap.ts
+++ b/packages/astro/src/core/ssr/sitemap.ts
@@ -1,45 +1,4 @@
-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\/?$/,
-];
+const STATUS_CODE_PAGE_REGEXP = /\/[0-9]{3}\/?$/;
/** Construct sitemap.xml given a set of URLs */
export function generateSitemap(pages: string[]): string {
@@ -50,7 +9,7 @@ export function generateSitemap(pages: string[]): string {
// copy just in case original copy is needed
// make sure that 404 page is excluded
// also works for other error pages
- const urls = [...pages].filter((url) => !ERROR_STATUS_CODE_REGEXES.find((code) => code.test(url)));
+ const urls = [...pages].filter((url) => !STATUS_CODE_PAGE_REGEXP.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 33b4ab70f..48bb617c9 100644
--- a/packages/astro/src/vite-plugin-build-html/index.ts
+++ b/packages/astro/src/vite-plugin-build-html/index.ts
@@ -25,48 +25,7 @@ const ASTRO_PAGE_PREFIX = '@astro-page';
const ASTRO_SCRIPT_PREFIX = '@astro-script';
const ASTRO_EMPTY = '@astro-empty';
-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',
-];
+const STATUS_CODE_REGEXP = /^[0-9]{3}$/;
interface PluginOptions {
astroConfig: AstroConfig;
@@ -523,8 +482,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
let outPath: string;
// Output directly to 404.html rather than 404/index.html
- // Supports any other status codes, too
- if (ERROR_STATUS_CODES.find((code) => code === name) || astroConfig.buildOptions.pageUrlFormat === 'file') {
+ if (astroConfig.buildOptions.pageUrlFormat === 'file' || STATUS_CODE_REGEXP.test(name)) {
outPath = `${removeEndingForwardSlash(name || 'index')}.html`;
} else {
outPath = npath.posix.join(name, 'index.html');