Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading