mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
pass original locals to the 404 page render (#12935)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
parent
2c99b5218b
commit
3d47e6baff
9 changed files with 127 additions and 1 deletions
5
.changeset/famous-gorillas-confess.md
Normal file
5
.changeset/famous-gorillas-confess.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes an issue where `Astro.locals` coming from an adapter weren't available in the `404.astro`, when using the `astro dev` command,
|
|
@ -246,7 +246,7 @@ export async function handleRoute({
|
|||
const fourOhFourRoute = await matchRoute('/404', manifestData, pipeline);
|
||||
if (fourOhFourRoute) {
|
||||
renderContext = await RenderContext.create({
|
||||
locals: {},
|
||||
locals,
|
||||
pipeline,
|
||||
pathname,
|
||||
middleware: isDefaultPrerendered404(fourOhFourRoute.route) ? undefined : middleware,
|
||||
|
|
59
packages/astro/test/custom-404-locals.test.js
Normal file
59
packages/astro/test/custom-404-locals.test.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
describe('Custom 404 locals', () => {
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
root: './fixtures/custom-404-locals/',
|
||||
site: 'http://example.com',
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
assert.equal($('h1').text(), 'Home');
|
||||
});
|
||||
|
||||
it('renders 404 for /a', async () => {
|
||||
const res = await fixture.fetch('/a');
|
||||
assert.equal(res.status, 404);
|
||||
|
||||
const html = await res.text();
|
||||
$ = cheerio.load(html);
|
||||
|
||||
assert.equal($('h1').text(), 'Page not found');
|
||||
assert.equal($('p.message').text(), 'This 404 is a dynamic HTML file.');
|
||||
assert.equal($('p.runtime').text(), 'locals');
|
||||
});
|
||||
|
||||
it('renders 404 for /404-return', async () => {
|
||||
const res = await fixture.fetch('/404-return');
|
||||
assert.equal(res.status, 404);
|
||||
|
||||
const html = await res.text();
|
||||
$ = cheerio.load(html);
|
||||
|
||||
assert.equal($('h1').text(), 'Page not found');
|
||||
assert.equal($('p.message').text(), 'This 404 is a dynamic HTML file.');
|
||||
assert.equal($('p.runtime').text(), 'locals');
|
||||
});
|
||||
});
|
||||
});
|
19
packages/astro/test/fixtures/custom-404-locals/astro.config.mjs
vendored
Normal file
19
packages/astro/test/fixtures/custom-404-locals/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
integrations: [{
|
||||
name: 'locals-integration',
|
||||
hooks: {
|
||||
'astro:server:setup': ({ server }) => {
|
||||
server.middlewares.use(async function middleware(req, res, next) {
|
||||
const clientLocalsSymbol = Symbol.for('astro.locals');
|
||||
Reflect.set(req, clientLocalsSymbol, {
|
||||
runtime: 'locals'
|
||||
});
|
||||
next();
|
||||
});
|
||||
},
|
||||
},
|
||||
}]
|
||||
});
|
8
packages/astro/test/fixtures/custom-404-locals/package.json
vendored
Normal file
8
packages/astro/test/fixtures/custom-404-locals/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/custom-404-locals",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
5
packages/astro/test/fixtures/custom-404-locals/src/pages/404-return.astro
vendored
Normal file
5
packages/astro/test/fixtures/custom-404-locals/src/pages/404-return.astro
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
return new Response(null, { status: 404 });
|
||||
---
|
||||
|
||||
I should never load
|
13
packages/astro/test/fixtures/custom-404-locals/src/pages/404.astro
vendored
Normal file
13
packages/astro/test/fixtures/custom-404-locals/src/pages/404.astro
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
const runtime = Astro.locals.runtime
|
||||
---
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Not Found - Custom 404</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Page not found</h1>
|
||||
<p class="message">This 404 is a dynamic HTML file.</p>
|
||||
<p class="runtime">{runtime}</p>
|
||||
</body>
|
||||
</html>
|
11
packages/astro/test/fixtures/custom-404-locals/src/pages/index.astro
vendored
Normal file
11
packages/astro/test/fixtures/custom-404-locals/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>
|
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
|
@ -2888,6 +2888,12 @@ importers:
|
|||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/custom-404-locals:
|
||||
dependencies:
|
||||
astro:
|
||||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/custom-404-loop-case-1:
|
||||
dependencies:
|
||||
astro:
|
||||
|
|
Loading…
Add table
Reference in a new issue