0

I have 30,000 products in my database, but only a few hundred of them have actual useful information.

I want to generate /product/[productId] pages statically to make the loading faster.

Statically generating all 30,000 pages fails on Vercel because it takes too long.

Is there a way to generate the /product/[productId] pages statically for few hundred productIds and dynamically for the rest?

Eric
  • 2,635
  • 6
  • 26
  • 66
  • Have you tried looking into `getStaticPaths`? [link](https://nextjs.org/docs/basic-features/data-fetching/get-static-paths) – Samy Rahmani Oct 29 '22 at 14:14
  • @SamyRahmani What exactly do I do with `getStaticPaths`? – Eric Oct 29 '22 at 14:49
  • Well, you can define what page will be statically generated. – Samy Rahmani Oct 29 '22 at 15:45
  • @SamyRahmani I'm generating `/product/[productId]` pages, but statically for a few hundred `productId`s and dynamically for the rest. What makes it difficult is that it's the same route. – Eric Oct 29 '22 at 19:11

2 Answers2

0

You could create a middleware page that would refer you to the appropriate page.

Let's say product1 should be generated dynamically and product2 should be generated statically.

You could have /product/[productId] refer product1 to /product/dynamic/[productId] and refer product2 to /product/static/[productId].

  • In /product/dynamic/[productId], you generate products dynamically.
  • In /product/static/[productId], you generate products statically.
Samy Rahmani
  • 320
  • 1
  • 7
0

I found fallback: true is the solution for this. https://nextjs.org/docs/api-reference/data-fetching/get-static-paths

Eric
  • 2,635
  • 6
  • 26
  • 66