Matching custom 404 pages using the manifest's route patterns (#3390)
* matching custom 404 pages using the manifest's route patterns * windows: still need to use the relative paths for 404 matching
This commit is contained in:
parent
f54072bd3f
commit
d8e5dfb977
8 changed files with 90 additions and 1 deletions
|
@ -166,8 +166,10 @@ async function handle500Response(
|
|||
}
|
||||
|
||||
function getCustom404Route(config: AstroConfig, manifest: ManifestData) {
|
||||
// For Windows compat, use relative page paths to match the 404 route
|
||||
const relPages = resolvePages(config).href.replace(config.root.href, '');
|
||||
return manifest.routes.find((r) => r.component === appendForwardSlash(relPages) + '404.astro');
|
||||
const pattern = new RegExp(`${appendForwardSlash(relPages)}404.(astro|md)`);
|
||||
return manifest.routes.find((r) => r.component.match(pattern));
|
||||
}
|
||||
|
||||
function log404(logging: LogOptions, pathname: string) {
|
||||
|
|
40
packages/astro/test/custom-404-md.test.js
Normal file
40
packages/astro/test/custom-404-md.test.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { expect } from 'chai';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
describe('Custom 404 Markdown', () => {
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
root: './fixtures/custom-404-md/',
|
||||
});
|
||||
});
|
||||
|
||||
describe('dev', () => {
|
||||
let devServer;
|
||||
let $;
|
||||
|
||||
before(async () => {
|
||||
devServer = await fixture.startDevServer();
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await devServer.stop();
|
||||
});
|
||||
|
||||
it('renders /', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
$ = cheerio.load(html);
|
||||
|
||||
expect($('h1').text()).to.equal('Home');
|
||||
});
|
||||
|
||||
it('renders 404 for /abc', async () => {
|
||||
const html = await fixture.fetch('/a').then((res) => res.text());
|
||||
$ = cheerio.load(html);
|
||||
|
||||
expect($('h1').text()).to.equal('Page not found');
|
||||
});
|
||||
});
|
||||
});
|
4
packages/astro/test/fixtures/custom-404-md/astro.config.mjs
vendored
Normal file
4
packages/astro/test/fixtures/custom-404-md/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({});
|
8
packages/astro/test/fixtures/custom-404-md/package.json
vendored
Normal file
8
packages/astro/test/fixtures/custom-404-md/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/custom-404-md",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
12
packages/astro/test/fixtures/custom-404-md/src/layouts/Base.astro
vendored
Normal file
12
packages/astro/test/fixtures/custom-404-md/src/layouts/Base.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
const { content } = Astro.props;
|
||||
---
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>{content.title}</title>
|
||||
</head>
|
||||
<body>
|
||||
<slot />
|
||||
</body>
|
||||
</html>
|
6
packages/astro/test/fixtures/custom-404-md/src/pages/404.md
vendored
Normal file
6
packages/astro/test/fixtures/custom-404-md/src/pages/404.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
layout: '../layouts/Base.astro'
|
||||
title: 'Not Found - Custom 404'
|
||||
---
|
||||
|
||||
# Page not found
|
11
packages/astro/test/fixtures/custom-404-md/src/pages/index.astro
vendored
Normal file
11
packages/astro/test/fixtures/custom-404-md/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
---
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Custom 404</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Home</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -1017,6 +1017,12 @@ importers:
|
|||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/custom-404-md:
|
||||
specifiers:
|
||||
astro: workspace:*
|
||||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/custom-elements:
|
||||
specifiers:
|
||||
'@test/custom-element-renderer': workspace:*
|
||||
|
|
Loading…
Reference in a new issue