Skip to content

Commit 294122b

Browse files
authored
Deno custom 404 pages (#4562)
* Add Failing Test For Deno Custom 404 Pages * Make Deno SSR Serve Custom 404 Pages
1 parent 23d4f80 commit 294122b

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.changeset/sharp-brooms-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/deno': patch
3+
---
4+
5+
Make Deno SSR Backend Render Custom 404 Pages

packages/integrations/deno/src/server.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,22 @@ export function start(manifest: SSRManifest, options: Options) {
2828
Reflect.set(request, Symbol.for('astro.clientAddress'), ip);
2929
return await app.render(request);
3030
}
31-
31+
32+
// If the request path wasn't found in astro,
33+
// try to fetch a static file instead
3234
const url = new URL(request.url);
3335
const localPath = new URL('.' + url.pathname, clientRoot);
34-
return fetch(localPath.toString());
36+
const fileResp = await fetch(localPath.toString());
37+
38+
// If the static file can't be found
39+
if (fileResp.status == 404) {
40+
// Render the astro custom 404 page
41+
return await app.render(request);
42+
43+
// If the static file is found
44+
} else {
45+
return fileResp;
46+
}
3547
};
3648

3749
const port = options.port ?? 8085;

packages/integrations/deno/test/basics.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ Deno.test({
2626
},
2727
});
2828

29+
Deno.test({
30+
name: 'Custom 404',
31+
async fn() {
32+
await startApp(async () => {
33+
const resp = await fetch('http://127.0.0.1:8085/this-does-not-exist');
34+
assertEquals(resp.status, 404);
35+
const html = await resp.text();
36+
assert(html);
37+
const doc = new DOMParser().parseFromString(html, `text/html`);
38+
const header = doc.querySelector('#custom-404');
39+
assert(header, 'displays custom 404');
40+
});
41+
},
42+
});
43+
2944
Deno.test({
3045
name: 'Loads style assets',
3146
async fn() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1 id="custom-404">Custom 404 Page</h1>

0 commit comments

Comments
 (0)