Deno custom 404 pages (#4562)
* Add Failing Test For Deno Custom 404 Pages * Make Deno SSR Serve Custom 404 Pages
This commit is contained in:
parent
23d4f80145
commit
294122b4e4
4 changed files with 35 additions and 2 deletions
5
.changeset/sharp-brooms-drum.md
Normal file
5
.changeset/sharp-brooms-drum.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/deno': patch
|
||||
---
|
||||
|
||||
Make Deno SSR Backend Render Custom 404 Pages
|
|
@ -28,10 +28,22 @@ export function start(manifest: SSRManifest, options: Options) {
|
|||
Reflect.set(request, Symbol.for('astro.clientAddress'), ip);
|
||||
return await app.render(request);
|
||||
}
|
||||
|
||||
|
||||
// If the request path wasn't found in astro,
|
||||
// try to fetch a static file instead
|
||||
const url = new URL(request.url);
|
||||
const localPath = new URL('.' + url.pathname, clientRoot);
|
||||
return fetch(localPath.toString());
|
||||
const fileResp = await fetch(localPath.toString());
|
||||
|
||||
// If the static file can't be found
|
||||
if (fileResp.status == 404) {
|
||||
// Render the astro custom 404 page
|
||||
return await app.render(request);
|
||||
|
||||
// If the static file is found
|
||||
} else {
|
||||
return fileResp;
|
||||
}
|
||||
};
|
||||
|
||||
const port = options.port ?? 8085;
|
||||
|
|
|
@ -26,6 +26,21 @@ Deno.test({
|
|||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: 'Custom 404',
|
||||
async fn() {
|
||||
await startApp(async () => {
|
||||
const resp = await fetch('http://127.0.0.1:8085/this-does-not-exist');
|
||||
assertEquals(resp.status, 404);
|
||||
const html = await resp.text();
|
||||
assert(html);
|
||||
const doc = new DOMParser().parseFromString(html, `text/html`);
|
||||
const header = doc.querySelector('#custom-404');
|
||||
assert(header, 'displays custom 404');
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: 'Loads style assets',
|
||||
async fn() {
|
||||
|
|
1
packages/integrations/deno/test/fixtures/basics/src/pages/404.astro
vendored
Normal file
1
packages/integrations/deno/test/fixtures/basics/src/pages/404.astro
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<h1 id="custom-404">Custom 404 Page</h1>
|
Loading…
Reference in a new issue