Caching Strategy for Large Next.js E-commerce Site with OpenNext on Cloudflare #84120
Replies: 1 comment 2 replies
-
Yeah this setup makes sense, great work and congrats on the site's traction. Using the build ID to namespace your assets is a good way to make sure old HTML points to the right JS chunks. The main trade off is how many old builds you want to keep around before cleaning them up since storage will creep up fast. For the KV side I would lean on lazy revalidation like you described instead of trying to invalidate everything on each deploy. That scales better when you are talking millions of pages. The only real gotcha I have seen is forgetting to update cache keys when you change how you generate pages which can leave stale data hanging around. Overall it looks maintainable as long as you have a cleanup policy and some automation around invalidation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We’re running a large e-commerce site with around 1.5M pages that are frequently updated. Our goal is to implement a caching strategy that works across builds, using OpenNext on Cloudflare.
Here’s the approach we’ve come up with:
Build step
During the build, we upload all static assets (i.e.
_next/...
) to an R2 bucket, namespaced by the build ID.Cache miss
When a page request results in a cache miss, we generate the page, store it in our KV cache along with the current build ID, and return it to the user.
Cache hit
If the page is found in the cache, we check the stored build ID:
This gives us a balance between freshness and performance.
Static chunks issue
The main complication is with static Next.js chunks.
Questions
Any thoughts, feedback, or pointers would be much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions