404 page (#2719)
* Fix: build to 404.html in the static build * Adds a changeset
This commit is contained in:
parent
a39fca5cfa
commit
049ab7dc96
5 changed files with 53 additions and 2 deletions
5
.changeset/five-stingrays-collect.md
Normal file
5
.changeset/five-stingrays-collect.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes the static build to write to 404.html
|
|
@ -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);
|
||||
|
|
8
packages/astro/test/fixtures/status-code/src/pages/404.astro
vendored
Normal file
8
packages/astro/test/fixtures/status-code/src/pages/404.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Testing</h1>
|
||||
</body>
|
||||
<html>
|
8
packages/astro/test/fixtures/status-code/src/pages/index.astro
vendored
Normal file
8
packages/astro/test/fixtures/status-code/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Testing</h1>
|
||||
</body>
|
||||
<html>
|
19
packages/astro/test/status-page.test.js
Normal file
19
packages/astro/test/status-page.test.js
Normal 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;
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue