Skip to content

Commit

Permalink
fix(routing): call middleware when rendering 404.astro (#12034)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Sep 20, 2024
1 parent fc3e40e commit 5b3ddfa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-vans-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where the middleware wasn't called when a project uses `404.astro`.
8 changes: 6 additions & 2 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/i18n-routing-manual.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 5b3ddfa

Please sign in to comment.