Next.js Discord

Discord Forum

How to use `generateStaticParms` on runtime instead of build-time?

Unanswered
Dutch posted this in #help-forum
Open in Discord
Avatar
DutchOP
How can I not build all static pages in advance, but rather use on-demand during build-time? So the first time a user visits the page it gets cached as a static html. It should not revalidate itself automatically. Only if use revalidatePath manually somewhere else.

10 Replies

Avatar
you can do this by returning an empty array inside your generateStaticParams. Like that no route will be build. Then make sure to to enable ISR using this: https://nextjs.org/docs/canary/app/building-your-application/data-fetching/incremental-static-regeneration

And after that it will generate no routes during build time but when a user visits a route, the route will be cached and only revalidated when you call revalidatePath for example
Avatar
DutchOP
@B33fb0n3 It doesn't seem to work
I get these Cache headers:
cache-control: private, no-cache, no-store, max-age=0, must-revalidate

This is my code:
export const dynamicParams = true
// export const revalidate = 60 // also doesn't work

export async function generateStaticParams() {
  // do not delete this, otherwise SSR pages won't be cached
  return []
}

Or is this expected? Am I missing something?
Avatar
check the cache header. Check if you get a HIT or a MISS
Avatar
DutchOP
I am not using Vercel, I don't see such kind of header:
Image
@B33fb0n3
Avatar
where do you host? Does it have a serverside data cache?
Avatar
DutchOP
I am using coolify with a vps
Avatar
Serengeti
@Dutch try adding
export const dynamic = "force-static";

to the page, i had to do this on my vercel deployed app to get it to be statically generated/cached at request time.
Avatar
DutchOP
I will try this!
Avatar
DutchOP
That worked! I also get headers now too
Image