Kevin
Kevin

Reputation: 75

How to handle large amount of dynamic path when using next.js getStaticPaths?

In next.js getStaticPaths runs in build time, but imagine if you have large amount of dynamic path

url/[id_1]
url/[id_2]
url/[id_3]
url/[id_4]
url/[id_5]
...
url/[id_1million]

it seems like building all id will not scale (server disk size, and build time), how do you handle this case?

Is there a getServerSideProps equivalent of getStaticPaths like getServerSidePaths?

I plan to pregenerate top 1k popular id, but it would not be feasible to pre generate all 5 million+ id.

Upvotes: 1

Views: 1110

Answers (1)

ivanatias
ivanatias

Reputation: 4033

This would be a good scenario for incremental static regeneration (ISR). You could statically generate only the top pages that you need. If a user requests a page that is not built yet, what would happen is that Next.js will server-render that page only on the first request and then, it will be cached for subsequent requests.

Use this with fallback: true if you want to show some sort of fallback UI while the page is being server-rendered (using router.isFallback on the component) or with fallback: 'blocking'.

You can read more details about how this works here.

Upvotes: 2

Related Questions