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(astro): don't run middleware in dev for prerendered 404 routes #11273

Merged
merged 1 commit into from
Jun 18, 2024

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Jun 17, 2024

Changes

When a 404 page is prerendered, middleware is not run in production. This is because middleware is only run at build time for prerendered pages. Currently this behaves differently in dev, which was running middleware for all 404 requests. This is confusing, causing reports such as #10785

As part of the work making this change it became clear there was a lot of unused logic in the dev 404 handling. Currently it checks if there is no matching route to handle the 404 case. However there is always a matched route in practice, because ensure404Route means a 404 route will always match it, so those code branches are never hit. This PR removes a lot of this unused code.

Testing

Added new tests, and fixed bugs in other tests.

Docs

Just fixes a bug, though maybe there could be docs explaining that middleware doesn't run for prerendered 404s?

Copy link

changeset-bot bot commented Jun 17, 2024

🦋 Changeset detected

Latest commit: 851b0fa

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jun 17, 2024
Comment on lines +210 to +219
const isPrerendered404 = matchedRoute.route.route === '/404' && matchedRoute.route.prerender;

renderContext = RenderContext.create({
locals,
pipeline,
pathname,
middleware: isPrerendered404 ? undefined : middleware,
request,
routeData: route,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual changed logic

@@ -177,106 +176,47 @@ export async function handleRoute({
const middleware = (await loadMiddleware(loader)).onRequest;
const locals = Reflect.get(incomingRequest, clientLocalsSymbol);

if (!matchedRoute) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matchedRoute is always truthy because of ensure404Route, so this branch is never hit

Comment on lines +5 to +7
Corrects an inconsistency in dev where middleware would run for prerendered 404 routes.
Middleware is not run for prerendered 404 routes in production, so this was incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Corrects an inconsistency in dev where middleware would run for prerendered 404 routes.
Middleware is not run for prerendered 404 routes in production, so this was incorrect.
Corrects an inconsistency in development where middlewares would run for prerendered 404 routes. It doesn't happen anymore to match production's behavior

@matthewp
Copy link
Contributor

Do we not run middleware for output: 'static'? I assume that we did. If so then I think we should probably do so for prerendered routes. I definitely understand how this can be confusing, however. I'm curious if people are using middleware for static pages already, and if that will break some expectations they have.

@ascorbic
Copy link
Contributor Author

ascorbic commented Jun 18, 2024

@matthewp yeah, I was a bit unclear there. You're of course right that middleware is run for prerendered pages at build time. The stuff I'm talking about is specific to 404 routes. The issue with prerendered 404 pages is that when we prerender it there's no way of knowing what the actual route will be, so there's no way that middleware can be used for things like i18n redirects if it's prerendered.

@ascorbic ascorbic merged commit cb4d078 into main Jun 18, 2024
13 checks passed
@ascorbic ascorbic deleted the plt-2202 branch June 18, 2024 15:05
@astrobot-houston astrobot-houston mentioned this pull request Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants