Replies: 6 comments 19 replies
-
I read this goal, and it seems this proposal wants to add some authentication in the middleware for prerendered pages. |
Beta Was this translation helpful? Give feedback.
-
Hi, cross-posting an issue I've opened in the doc, which appears more appropriate here: withastro/docs#6884 (comment) To put it in a nutshell, middlewares in front of static pages allow to implement a "static personalization by redirection", which is an umbrella for a massive amount of patterns:
For some patterns, additional ingredients may be needed:
The caveats: this work has been receiving mitigated feedback over the years.
In addition, I'd like to mention that a much bolder move would be refactoring "getStaticPaths" so it thinks in terms of request and not just dynamic URL params. Static rendering = caching renders for requests. Currently all frameworks use dynamic route params as cache key, which for instance makes Next.js terrible at handling search params (you can't statically render common search params unless you use a hacky setup with a rewrite to a URL param). This is my "bold static rendering framework" architecture (the JIT thing is mostly to look fancier than it needs ignore that ':)) : Bibliography:
(all this can be done today using Edge Middlewares or any similar host-provided feature, or even a separate proxy server, the point here would be more a deeper integration in Astro) |
Beta Was this translation helpful? Give feedback.
-
Amazing, thanks for kicking this off @lilnasy! One question about the API: would this have to be global flag? Or could it also be something like |
Beta Was this translation helpful? Give feedback.
-
I wonder how much this would align with platform specific middlewares. I could only use the server mode, or have the build create static html pages which would not have the middleware in front of it. What I'm wondering now: If the above was implemented, would the (presumably non-platform-specific) middleware function run and simply pick up the static html from the fs? Or since filesystem might not even work on all platforms, in some other serializable format it can import? It would still be nice to define such middleware in a generic Astro shape and then delegate that to the adapters which would use the platform-specific middleware and also warn/throw for platform limitations. I noticed that the Vercel Adapter already has some edge middleware integration, but that one actually created more confusion at first when I explored how it might work for my usecase. (It doesn't, simply bypasses static pages aswell by default) |
Beta Was this translation helpful? Give feedback.
-
Redirecting users away from paywalled content has been discussed extensively here. I want to add another variation of this use case. Today, when users come to our homepage we do 1 of 2 things depending on the users state:
Use case 2 and 3 are somewhat tricky because we have to make an API call to check user authentication. In these cases, creating a dynamic route for the homepage might be an acceptable trade off. As mentioned elsewhere in this thread, the bottleneck in this case likely becomes the middleware itself. Use case 1, however, accounts for a huge portion of our homepage traffic and in the majority of cases, these users don't even have an authentication cookie set. When this happens, we can skip calling our authentication API entirely. In this case, returning static HTML should be faster than dynamically rendering our homepage. Speed is especially important for these users because they are new users who are interested in our product. We want to show them our homepage as soon as possible. We do have some options for workarounds, but would love to see "native" Astro support for this. For more context, we are serving Astro via Docker on AWS. We are not using vercel, cloudflare,, netlify today. |
Beta Was this translation helpful? Give feedback.
-
We (the core team) discussed about it yesterday and we were not 100% sure what you'd want technically speaking. Is it like the follwing? The current SSR setup but:
|
Beta Was this translation helpful? Give feedback.
-
Summary
For prerendered pages, the middleware runs during build-time, and only during build-time. Some middleware may need to run on-request for all pages.
Background & Motivation
There is no straightforward way to password-protect prerendered pages in a hybrid site. This comes up frequently from starlight users, and users naturally gravitate towards middleware only to find out that it only runs at build time for these routes.
We have implemented the internals for this feature as part of the edge middleware feature used by the netlify and vercel adapters.
Goals
Non-goals
It would likely require creating a distinction between build-time SSR code and runtime SSR code, which is an unsolved problem of its own.
As a workaround, users can check for a runtime environment variable that is different across the two environments, and make the middleware act differently based on it.
Harmony
How might this proposal interact with other in-progress proposals?
Example
Beta Was this translation helpful? Give feedback.
All reactions