11

I have a MERN app in which I have integrated NextJS. First time with NextJS so bear with me. I have initially used SSR everywhere (getServerSideProps).

Key points:

  • I have over 150,000 pages with static content that it's never going to change.
  • Every week I add around +100 new pages.

I guess the ideal situation here would be using getStaticProps and getStaticPaths and, after an initial build of those 150k pages, just build the new added pages every week and keep what I already have built as it is since it's never going to change.

How can I achieve this? Should I use revalidate here? I have been reading about it in the documentation but I don't completely understand it.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
barcel0
  • 113
  • 1
  • 6

1 Answers1

15

That can be achieved with getStaticProps/getStaticPaths. You'll have to use fallback: true or fallback: 'blocking' in getStaticPaths.

With fallback: true the paths not generated at build time will serve a fallback page on the first request while Next.js statically generates the page. When this is done the page will be swapped from the fallback page to the actual full page.

With fallback: 'blocking', the paths not generated at build time will wait for the HTML to be generated by Next.js, then serve the page once that's done. Unlike fallback: true, since there's no fallback the rendering gets blocked until the page is generated, similar to what happens during server-side rendering.

In both cases the page gets added to the list of pre-rendered pages. Subsequent requests to the same path will serve the pre-generated page.

Neither of these options is supported by next export, in case you depend on that.


Note that revalidate is used in getStaticProps for Incremental Static Regeneration - in cases where you'd want to update existing, generated pages. Since you mentioned generated pages will never change, then there's no need to use revalidate.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • 2
    Thanks! I was misleading fallback with revalidate. I'll use getStaticProps + revalidate only in pages where these 150k static pages are listed, since the list changes every week. – barcel0 Feb 05 '21 at 08:12
  • @juliomalves what about a scenario of SSG user profile pages where user creates/changes their data. Is it possible to pregenerate their profile pages manualy so that there is no need for fallback? – radulle Nov 06 '21 at 15:24
  • @radulle _"pregenerate their profile pages manualy"_ - What do you mean by that? With SSG, you have to use `fallback` (either `true` or `'blocking'`) if you want to add new pages that were not pre-generated at build time. – juliomalves Nov 06 '21 at 15:58
  • 1
    @juliomalves I worded it wrongly. Blocking fallback is ok, but what i really want is not to revalidate by a set timer but on some event (e.g database update or api call). This way when user creates/modifies their profile new static page is generated and old one is invalidated. – radulle Nov 06 '21 at 16:28
  • 1
    Since Next.js 12.1 on-demand revalidation is now possible, see https://nextjs.org/blog/next-12-1#on-demand-incremental-static-regeneration-beta. – juliomalves Mar 21 '22 at 07:10