Hello,
I am implementing an e-commerce application based on Vercel commerce GitHub - vercel/commerce: Next.js Commerce, with a Spree backend instead of Shopify.
Unlike Vercel commerce, I would like the product pages to use Incremental Static Regeneration (ISR) for improved performances. (Vercel commerce has dynamic product pages)
So I implemented the generateStaticParams
function in my product page:
export const dynamicParams = true
export const revalidate = false
export async function generateStaticParams() {
return []
}
type ProductProps = {
params: {
slug: string
}
}
const getProductWithRedirect = async (slug: string) => {
try {
return product = await getProduct(slug)
} catch (error: any) {
notFound()
}
}
export default async function Product({ params: { slug } }: ProductProps) {
const product = await getProductWithRedirect(slug)
if (!product || typeof product == 'string') notFound()
return
}
generateStaticParams
returns an empty array: we have to many products to build all the pages at build time, I want the pages to be built on demand and then saved into the cache (ISR).
With this implementation, product pages raise a 500 error:
Page changed from static to dynamic at runtime /product/example-slug, reason: cookies
Note: I am indeed reading from cookies in the main layout to retrieve the current cart.
Now if I update generateStaticParams
to return at least one slug, the error disappears from the page built at build time, but also from all other product pages that are built at request time and stored into the cache (ISR)
export async function generateStaticParams() {
return ['a-single-product-slug']
}
So this actually fixes my problem!
However, since I have no idea what is going on, I wonder if this is safe and won’t break in a future Next.js update.
Can anyone explain to me what is happening?