Incremental Static Generation for multiple rendering paths #17631
Replies
This will be possible with the new i18n support: #17078 |
|
Besides i18n, this would also be great if you want to fetch data in the server and return the page with the data if the user is not authenticated (mainly for SEO), and in the case the user is logged return a different static page (same url) with a spinner/skeleton. This might be related to render the page based on the user role, as specified in the draft. A general approach in this case would be much better, instead of just for i18n. Related: #17248 (reply in thread) |
|
Question somewhat related on Stack Overflow, user is asking how to postpone the static generation to the first query: https://stackoverflow.com/questions/66582107/postpone-ssg-to-first-request. If we can postpone static generation to first request, we could extend this reasoning and generate multiple versions of the page. It is still static generation of a static page because the route is not dynamic. |
|
Another great use case from Stack Overflow trenches: generating pages in dark mode vs light mode https://stackoverflow.com/questions/67094919/how-to-fix-dark-mode-background-color-flicker-in-nextjs |
|
I've spent a few hours on this question, and I've tried to figure a more formal definition of SSR: https://tinyurl.com/ssr-theory From this, I conclude that By letting us define the path not only in terms of URL params, but also in terms of static properties of the request, like the cookies and headers, you open up a vast area of new use cases for a very limited change in API. |
|
Hi, middlewares have finally made this not only possible, but very easy to implement. |
|
Here's my latest work: same idea, but just the HTTP cache instead of relying on ISR. ISR is still way more advanced but it's cool to know that sometimes a correct HTTP cache configuration can drastically reduce the number of server renders of an app. |


Problem: you can't pre-render multiple versions of the same page
Edit: I've now written a full article that describe more clearly the architecture I magine, called "Rainbow Rendering": https://blog.vulcanjs.org/lets-bring-the-jamstack-to-saas-introducing-rainbow-rendering-ad1834fe62ff
Edit 2: I've now fully implemented this architecture, thanks to Next 12 middlewares: https://blog.vulcanjs.org/render-anything-statically-with-next-js-and-the-megaparam-4039e66ffde
Say that, for instance:
I suppose that data-fetching can be done client-side here. I suppose you want to statically prerender the page, even if it's private (say you secure it using another pattern like a reverse proxy) so you don't use
getServerSidePropsThose 2 use cases needs you to read the request dynamically. You have 2 choices, not pre-rendering and doing everything client side, or using
getServerSidePropsjust for this.You don't need dynamic, per request SSR to pre-render a finite number of pages
But what if I told you that you don't actually need
getServerSidePropsto do this?Rendering the page on each request would be suboptimal, because you actually have a finite number of combinations. Say you support 50 languages, and have 3 main roles. That's about 150 versions of your page.
That's a lot for a human. That's not for a framework that can render 150k+ pages in split seconds.
If you are lucky, you probably have more people requesting your page than possible combinations of your page. You only need 151 people to access your wonderful page to be better with static generation than SSR.
And even if you have less requests than possible rendering paths, you don't need to render all those pages. Thanks to Incremental Static Generation, you could build them on demand, when they are actually requested. You don't have any French speaking administrator? Great, the French version of the bottom admin area of your page will never ever be rendered.
The proposal
Note: I have no idea yet how Incremental Static Generation work so the following proposal is probably naive.
If I were to implement this tomorrow, I would simply implement a cache system whose key is computed based on the request. You need to render per role and language => key is "role:language" (for a given page). You would also define or compute a TTL on some conditions (in the best scenario you could base it on some usage statistics).
SSR is just a special case where the number of possibilities is in theory infinite and you always cache miss.
Replace the cache with writing HTML files on disk and you can call that static generation, but that's the same concept.
Edit: This comment links to an app that does basically this using SSR + a cache header #17260 (comment)
Alternative
Rebuilding based on an API calls seems to have been mentioned in the initial RFC. #11552
But it seems to still imply 1 page = 1 build, it is targeted at people wanting to update the page after an event (like you've added a comment on your blog article, so you invalidate the previous render of this article). I think this proposal is a bit different, we want N build with N cache entry for 1 page.
Sorry if the idea has already been exposed, I haven't seen it explicitly described though I saw hint of this regarding variations of the same page (#11552 (comment)).
You could have different routes too, that's great for languages for instance. But that doesn't scale much, it would be weird to have "my-page/fr/admin" just because there is some admin block somewhere in this specific page.
What happens to the browser and next export
The browser would just fetch as usual, the work would be on the server to get the correct page depending on the scenario. If all relevant infos are on the request you can suppose that both have enough info to make it work.
So no "my-page/admin", instead you just fetch "my-page" and the backend serves you "my-page.html" stored at "fr:admin" key for instance.
If you do a Next export, you just forget all this and fill the data client-side as usual. Hopefully for i18n there are smarter approaches like exporting for all languages and having language based routes, because languages are complicated (you might want to switch programmatically etc.), but for role it already works like this so you don't lose anything.
Related
I had the idea while digging Blitz.js doc: https://blitzjs.com/docs/authorization and experimenting with SSR in my own apps with private pages.
Next-roots package may be generalized to support this usage