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

feat: middleware and virtual routes #7254

Merged
merged 12 commits into from
Mar 11, 2024
Merged

feat: middleware and virtual routes #7254

merged 12 commits into from
Mar 11, 2024

Conversation

lilnasy
Copy link
Contributor

@lilnasy lilnasy commented Mar 8, 2024

Description (required)

  • Before the linked PR, a 404.astro had to exist for the middleware to run for the "not found" case.
  • After it merges, astro will run the middleware for a blank page, but only if the adapter doesn't override this behavior (Cloudflare, SST, and Vercel adapters do.)
  • If the user project had a 404.astro, there is no difference.

Related issues & labels (optional)

Copy link

vercel bot commented Mar 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview Mar 11, 2024 10:40am
1 Ignored Deployment
Name Status Preview Updated (UTC)
docs-i18n ⬜️ Ignored (Inspect) Mar 11, 2024 10:40am

:::note
On all prerendered pages, the middleware runs only during the **build process** to create static pages and does not run in the deployed website. This applies to all pages in a `static` (SSG) project. This also includes prerendered pages in `hybrid` mode (default) and any pages in `server` mode with `export const prerender = true`.
### Timing of middleware execution
By default, astro builds your project into a static site. In this [mode](https://docs.astro.build/en/basics/rendering-modes/), the middleware runs during the build as well. This allows the pages access to [`locals` object](#storing-data-in-contextlocals). Additionally, the static files can be served immediately since no server-side code is run on-request, ensuring fast response times.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Users ask "why not run middleware only on-request for static pages". So I tried to give the 2 reasons here:

  • you'll need Astro.locals, and
  • you probably don't want server-side code to run on-request if it can run ahead of time for performance.

Copy link
Member

Choose a reason for hiding this comment

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

OK, so I see this less about timing and more about "why would I run middleware on static pages?"

In that framing, I can see adding something here. Let me try something!


For on-demand rendered pages in `server` (default) or `hybrid` (with `export const prerender = false`) mode, the middleware runs in the deployed website when the route is requested.
In server deployments, if a request does not match a page in your project, astro will serve a 404 page. The middleware will be run during the rendering if the 404 page is on-demand rendered. If the middleware throws an error, a 500 error page will be served without running the middleware.
Copy link
Contributor Author

@lilnasy lilnasy Mar 8, 2024

Choose a reason for hiding this comment

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

As far as prerendering and middleware execution is concerned, the 404 page behaves the same as any other page. However, it's worth reiterating because the 500 page does not.

When on-demand rendered, the 500 page will be rendered without the middleware if the middleware is the reason that the 500 error page has to be served (it errror'd).

@sarah11918 sarah11918 added this to the 4.5.0 milestone Mar 8, 2024
@sarah11918 sarah11918 added add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) minor-release For the next minor release; in the milestone, "merge queue" when approved by Sarah! labels Mar 8, 2024
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Hey @lilnasy ! It felt like there was more here than needed to be, and with the deleted lines and everything, it was awkward to use GitHub here to suggest changes.

So sorry, I just edited the file so I could do something different here. So, we're going to start small, and build back up with info that is truly needed. How is this for a start?

@lilnasy
Copy link
Contributor Author

lilnasy commented Mar 8, 2024

It is great for a start!

Some bullet points we should get across:

  • middleware will run for not-found case even if the user didn't author a custom 404 page (new in 4.5)
  • it will NOT run for not-found case if the user's custom 404 page is prerendered (unchanged from before, still true)
  • it may run twice on errors -
    first for the original on-demand rendered page that error'd, then for the on-demand rendered 500.astro page
  • locals will not be available to the on-demand rendered custom 500 page if the middleware itself errors when run the second time

It's fine to leave some out if they feel too specific of details.

I'll add suggestions for these points.


### Handling 404 pages

Middleware will run for an on-demand rendered route while Astro attempts to render a 404 page. However, it is up to the [adapter](/en/guides/server-side-rendering/) to decide whether that code runs. Some adapters may serve a platform-specific error page instead.
Copy link
Contributor Author

@lilnasy lilnasy Mar 8, 2024

Choose a reason for hiding this comment

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

Suggested change
Middleware will run for an on-demand rendered route while Astro attempts to render a 404 page. However, it is up to the [adapter](/en/guides/server-side-rendering/) to decide whether that code runs. Some adapters may serve a platform-specific error page instead.
Middleware will run for an on-demand rendered route while Astro attempts to render a 404 page. However, it is up to the [adapter](/en/guides/server-side-rendering/) to decide whether that code runs. Some adapters may serve a platform-specific error page instead.
If a page fails to render due to an error, astro will attempt to render your custom 500 page. If the error page is set to render on-demand, the middleware will run a second time for that request.
If there is an error caused by middleware code, the on-demand rendered custom 500 page will be rendered without the middleware. `Astro.locals`, therefore, may be missing.

Copy link
Member

@sarah11918 sarah11918 Mar 10, 2024

Choose a reason for hiding this comment

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

I'm trying to figure out how important it is to add these two paragraphs. 500 is generic error, right? Does it matter that middleware runs if the page 500s? What practical value is this to the author, and would they change anything about how they write their code knowing this? In which situations would it make a difference?

Similarly, if a 500 page is being served, how much does it matter that they don't have Astro.locals? How common is it to create a custom 500 page (at all) and then in particular, one that would make use of locals?

I'm going by the "not everything is true belongs in docs" principle, and want to include things that will affect people's decisions, and that they need to know to build properly. I'm going to need your help to figure out if these fit that criteria.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that important.

The most important point would be that 500 page could miss locals sometimes. However, even that I don't feel too strongly about - it came up only once.

Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
@sarah11918
Copy link
Member

OK @lilnasy - I made one adjustment and then a couple of suggestions that I think might address your outstanding points? See what you think of this, whether you feel those both a) are correct 😅 and b) provide helpful information to be worth including!

Then, we'll be finished here!

@sarah11918
Copy link
Member

@arsh For the sake of having something ready for minor release tomorrow, I'm committing my suggestions, but I'm totally open to your feedback! Still at least 12 hours before I'm merging. 😂

If you don't have a chance to look at this before it merges with the minor release and you want to change something, then happy to PR an update to it!

@lilnasy
Copy link
Contributor Author

lilnasy commented Mar 11, 2024

Thanks for getting this all the info in a much better shape!

Just one suggestion. Even without it, looks ready to publish.

sarah11918 and others added 2 commits March 11, 2024 07:14
Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
@sarah11918 sarah11918 changed the base branch from main to 4.5.0 March 11, 2024 10:23
@sarah11918 sarah11918 merged commit 34fc48f into 4.5.0 Mar 11, 2024
2 of 3 checks passed
@sarah11918 sarah11918 deleted the 10206 branch March 11, 2024 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) minor-release For the next minor release; in the milestone, "merge queue" when approved by Sarah!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants