0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

fix(routing): call middleware when rendering 404.astro (#12034)

This commit is contained in:
Emanuele Stoppa 2024-09-20 10:12:05 +01:00 committed by GitHub
parent fc3e40ecde
commit 5b3ddfadcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where the middleware wasn't called when a project uses `404.astro`.

View file

@ -1,6 +1,7 @@
import type http from 'node:http';
import type { ComponentInstance, ManifestData, RouteData } from '../@types/astro.js';
import {
DEFAULT_404_COMPONENT,
REROUTE_DIRECTIVE_HEADER,
REWRITE_DIRECTIVE_HEADER_KEY,
clientLocalsSymbol,
@ -192,13 +193,16 @@ export async function handleRoute({
mod = preloadedComponent;
const isPrerendered404 = matchedRoute.route.route === '/404' && matchedRoute.route.prerender;
const isDefaultPrerendered404 =
matchedRoute.route.route === '/404' &&
matchedRoute.route.prerender &&
matchedRoute.route.component === DEFAULT_404_COMPONENT;
renderContext = RenderContext.create({
locals,
pipeline,
pathname,
middleware: isPrerendered404 ? undefined : middleware,
middleware: isDefaultPrerendered404 ? undefined : middleware,
request,
routeData: route,
});

View file

@ -53,9 +53,9 @@ describe('Dev server manual routing', () => {
assert.equal(text.includes('Hola.'), true);
});
it('should not redirect prerendered 404 routes in dev', async () => {
it('should call the middleware for 404.astro pages', async () => {
const response = await fixture.fetch('/redirect-me');
assert.equal(response.status, 404);
assert.equal(response.status, 200);
});
});