The Wayback Machine - https://web.archive.org/web/20220525171426/https://github.com/vercel/next.js/discussions/17631
Skip to content

Incremental Static Generation for multiple rendering paths #17631

eric-burel started this conversation in Ideas
Incremental Static Generation for multiple rendering paths #17631
Oct 5, 2020 · 7 comments · 7 replies

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:

  • You want to render the page in the language requested by the user.
  • You want to render portion of the page depending on the current user role
  • You want to do some A/B test
  • You want to render a light and a dark version of your page
  • etc.

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 getServerSideProps

Those 2 use cases needs you to read the request dynamically. You have 2 choices, not pre-rendering and doing everything client side, or using getServerSideProps just 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 getServerSideProps to 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

Replies

7 comments
·
7 replies

You want to render the page in the language requested by the user.

This will be possible with the new i18n support: #17078

2 replies
@eric-burel

Yeah this proposal is mostly a generalization, I think what we can do for languages we could do for roles or for any dimension that makes sense for a given project.

@eric-burel

I am just digging i18n routing introduced in v10. i18n routing creates a strong relationship between the route, and the rendered page:

  1. Build the page for each locale
  2. On request, get the locale (I suppose you have automatic locale detection)
  3. Redirect the user to "/[locale]/thepage"

This is acceptable for locale but weird for any other dimension. Like you wouldn't want /fr/admin/logged-in/your-page. So instead the workflow could be:

  1. Build the pages for all possible combinations of the parameter (for i18n, that's just all locales)
  2. Get the parameters you want from the query (for i18n, the accept-language when you have auto-detection)
  3. Return the correct statically rendered page based on those parameter OR redirect (for i18n, Next does a redirect as a default but it means you have to accept to have the parameter in the URL)

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)
https://stackoverflow.com/questions/64338118/nextjs-static-pages-for-authenticated-and-unauthenticated-users

1 reply
@eric-burel

Another use case I am imagining is building for different organization. You may for example have a getStaticProps that returns a slightly different result depending on the company your user belongs to.

If you have big organization, that's worth building the page once per organization, instead of rendering the same thing on every access of every user of the same organization.
It is comparable to the roles use case.

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.

4 replies
@leerob

leerob Mar 12, 2021
Maintainer

Isn't that what fallback: blocking is doing essentially? https://nextjs.org/docs/basic-features/data-fetching#fallback-blocking

I'd also note that for complex & dynamic use cases, you're always going to have more flexibility with SSR + edge caching. Because you don't have access to the incoming request in getStaticProps, there's less flexibility there versus getServerSideProps and using cache-control. Using static generation is great, but doesn't work for every use case.

@eric-burel

It's not my post initially but I think the point of the question was to apply the same reasoning to a non dynamic route, without route parameter.

@eric-burel

I can't put my finger on it precisely, but getStaticProps and getServerSideRenderProps seems to be special case of a more generic concept that would allow way more custom behaviour (like getInitialProps in a way). All the more with ISG where getStaticProps can be called on the fly using fallback.

Route parameters are a way to pass request time information. In a way, getStaticProps with a fallback on a dynamic route is equivalent to getServerSideRenderProps with a cache + data fetching based on req.route.params.

@flagg19

Isn't that what fallback: blocking is doing essentially? https://nextjs.org/docs/basic-features/data-fetching#fallback-blocking

I'd also note that for complex & dynamic use cases, you're always going to have more flexibility with SSR + edge caching. Because you don't have access to the incoming request in getStaticProps, there's less flexibility there versus getServerSideProps and using cache-control. Using static generation is great, but doesn't work for every use case.

Hi there, I'm the one who posted the question on StackOverflow, the problem with fallback: blocking is that it only applies to dynamic routes, I would like to use it also on static routes. I'm aware that there are other ways to achive similar results but I feel like all the pieces are already there, maybe with minimal effort this case could be handled directly on next.js

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

0 replies

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 getStaticPaths could be vastly improved, by simply reusing an API very similar to rewrites.

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.

0 replies

Hi, middlewares have finally made this not only possible, but very easy to implement.
I've detailed everything in an article: https://blog.vulcanjs.org/render-anything-statically-with-next-js-and-the-megaparam-4039e66ffde
It includes a demo, with 2 parameters: the tenant, and the dark/light theme.
I don't use ISR yet, only static generation, but it should easily extend to ISR.

0 replies

Here's my latest work: same idea, but just the HTTP cache instead of relying on ISR.

https://blog.vulcanjs.org/treat-your-users-right-with-http-cache-and-segmented-rendering-7a4f4761b549

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.

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
4 participants