From fc5f4163826f5aaf7db95d630b306d5aec33516e Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 22 Nov 2021 12:06:15 -0600 Subject: [PATCH] fix: output `404.astro` to `404.html` (#1949) * fix: output 404.astro to 404.html * chore: revert example change * fix: update status code to only match 404 --- .changeset/twenty-hounds-approve.md | 5 +++++ packages/astro/src/vite-plugin-build-html/index.ts | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changeset/twenty-hounds-approve.md diff --git a/.changeset/twenty-hounds-approve.md b/.changeset/twenty-hounds-approve.md new file mode 100644 index 0000000000..6596f010da --- /dev/null +++ b/.changeset/twenty-hounds-approve.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix regression with `astro build` 404.astro output diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts index c3b24ddf19..211145fd15 100644 --- a/packages/astro/src/vite-plugin-build-html/index.ts +++ b/packages/astro/src/vite-plugin-build-html/index.ts @@ -21,7 +21,7 @@ const ASTRO_PAGE_PREFIX = '@astro-page'; const ASTRO_SCRIPT_PREFIX = '@astro-script'; const ASTRO_EMPTY = '@astro-empty'; - +const STATUS_CODE_RE = /^404$/; const tagsWithSrcSet = new Set(['img', 'source']); const isAstroInjectedLink = (node: parse5.Element) => isStylesheetLink(node) && getAttribute(node, 'data-astro-injected') === ''; @@ -424,7 +424,17 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin { } const outHTML = parse5.serialize(document); - const outPath = npath.posix.join(pathname.substr(1), 'index.html'); + const name = pathname.substr(1); + let outPath: string; + + // Output directly to 404.html rather than 400/index.html + // Supports any other status codes, too + if (name.match(STATUS_CODE_RE)) { + outPath = npath.posix.join(`${name}.html`) + } else { + outPath = npath.posix.join(name, 'index.html') + } + this.emitFile({ fileName: outPath, source: outHTML,