Next.js Discord

Discord Forum

Dynamic Route Segments caching

Answered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
Hello is it possible to have the cache on the routes handler using the dynamic route segment? If yes how please?

I try to do pagination by retrieving the number of the requested page through the dynamic segment, but the documentation says that dynamic routes are not cached by default is there a way to force this so that if 100 users request the first page at the same time and another person requests the second page and although only 2 calls are not cached
Answered by joulev
generateStaticParams is available in dynamic route handlers if you need it.

unstable_cache can be used too if you want very granular caching.
View full answer

14 Replies

Yes, it is possible to implement caching for dynamic routes, though it requires some additional handling since dynamic routes are not cached by default. The goal is to ensure that requests to the same dynamic route (e.g., the same page of a paginated result) can be served from the cache.

Here’s how you can achieve this:

1. Middleware for Caching
Use middleware to handle caching for dynamic routes. You can implement custom caching logic based on the route parameters.

2. Cache Storage
Choose a cache storage system, such as Redis or an in-memory store, to store the responses.

3. Cache Key
Create a unique cache key based on the dynamic route segment (e.g., the page number in pagination).
Southeastern blueberry beeOP
I mean without going through an external caching solution but using that of nextjs
Answer
Southeastern blueberry beeOP
@joulev i didnt found the generateStaticParams part on route handler documentation, this function is for generating html at build time right ? But i want to cache the return of a GET() not generating html I must have missed something, but I can't figure out what.
(though, also try export const revalidate = 10 in combination with generateStaticParams, i think it should work)
Southeastern blueberry beeOP
that's what I just did generateStaticParams() request an external service to get the total number of pages on the pagination (an array where each item is a number going from 1 to the last page) but if the number of pages (pagination pages not nextjs pages) evolves after the build then there won't be any cache when these pages are requested for those right?
@Southeastern blueberry bee that's what I just did generateStaticParams() request an external service to get the total number of pages on the pagination (an array where each item is a number going from 1 to the last page) but if the number of pages (pagination pages not nextjs pages) evolves after the build then there won't be any cache when these pages are requested for those right?
By default the response should still remain the same even when the data changes. I think revalidateTag/revalidatePath can be used to tell Next.js that the data has changed and should be recomputed.

Though you can just use unstable_cache for the most granular and easily configurable cache. Despite the name it’s pretty stable these days and I’ve been using it extensively at work (for the lack of a better alternatives).
all of my unstable_cache so far, keyParts has been []
@joulev the keyParts you most likely don't need. the one used in revalidateTag is the tags parameter.
Southeastern blueberry beeOP
Okay I think I now understand this granularity system with the tags makes me think of the key in react query I'm not lost at least
Ty