* Fix: build to 404.html in the static build

* Adds a changeset
This commit is contained in:
Matthew Phillips 2022-03-07 09:49:50 -05:00 committed by GitHub
parent a39fca5cfa
commit 049ab7dc96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes the static build to write to 404.html

View file

@ -37,6 +37,8 @@ export interface StaticBuildOptions {
const MAX_CONCURRENT_RENDERS = 10;
const STATUS_CODE_PAGES = new Set(['/404', '/500']);
function addPageName(pathname: string, opts: StaticBuildOptions): void {
opts.pageNames.push(pathname.replace(/\/?$/, '/').replace(/^\//, ''));
}
@ -479,8 +481,12 @@ function getOutFolder(astroConfig: AstroConfig, pathname: string, routeType: Rou
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
case 'page':
switch (astroConfig.buildOptions.pageUrlFormat) {
case 'directory':
case 'directory': {
if(STATUS_CODE_PAGES.has(pathname)) {
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
}
return new URL('.' + appendForwardSlash(pathname), outRoot);
}
case 'file': {
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
}
@ -495,8 +501,13 @@ function getOutFile(astroConfig: AstroConfig, outFolder: URL, pathname: string,
return new URL(npath.basename(pathname), outFolder);
case 'page':
switch (astroConfig.buildOptions.pageUrlFormat) {
case 'directory':
case 'directory': {
if(STATUS_CODE_PAGES.has(pathname)) {
const baseName = npath.basename(pathname);
return new URL('./' + (baseName || 'index') + '.html', outFolder);
}
return new URL('./index.html', outFolder);
}
case 'file': {
const baseName = npath.basename(pathname);
return new URL('./' + (baseName || 'index') + '.html', outFolder);

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
<html>

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
<html>

View file

@ -0,0 +1,19 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
// Asset bundling
describe('Status Code Pages', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/status-code/',
});
await fixture.build();
});
it('builds to 404.html', async () => {
const html = await fixture.readFile('/404.html');
expect(html).to.be.ok;
});
});