I am currently using NextJS 13.5 to build an ecommerce.
In the app/products/[slug]/page.jsx I have product name, description, price and other product related information. I use the generateStaticParams()
function in order to statically build each product page with next build
.
Having said all that, the problem I am facing is that a product's price cannot be static! Products' prices change frequently, sometimes even daily.
How could I have the price of every product to be dynamic and fetched from the database on each visit, even though the rest of the product page (name, description etc) is static, and basically SSG?
Any knowledge or ideas are appreciated!
I had two ideas when reading your question. The first should be accessible on Next.js 13, and the second requires updating to 14.
1. Using
fetch
withcache: 'no-store'
I found some information in the Next.js Pages -> App Router migration guide that I believe will be relevant in your case.
Explanation:
Example:
Link
Source: https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#server-side-rendering-getserversideprops
2. Partial Prerendering
If you upgrade to 14 you can opt in to this experimental feature.
Explanation:
Links
API reference: https://nextjs.org/docs/app/api-reference/next-config-js/partial-prerendering
Tutorial: https://nextjs.org/learn/dashboard-app/partial-prerendering