Next.js Discord

Discord Forum

Data cache for client?

Unanswered
Taigan posted this in #help-forum
Open in Discord
TaiganOP
I see that the Next Data Cache (https://nextjs.org/docs/app/building-your-application/caching#data-cache) can persistently cache requests made from server components (and presumably from route handlers?). Is there anything like this for client-side requests?

I have a paginated table I'd like to globally cache (with revalidation of, say, 5 minutes), which is exposed through a route handler. On initial load, it's easy to cache just the first page because I fetch it on the server. However, I'm not able to cache subsequent requests from the client (eg ?page=2 etc).

Are there any good options here? One way I suppose is to just make another route handler that handles pagination requests, and have that route fetch and use the Data Cache.

10 Replies

@DirtyCajunRice | AppDir or is prefetch more suited
TaiganOP
prefetching as in loading the first 3 pages in the server component for ex? I thought of caching the route handler because even if I prefetch 3 pages to start, everyone who loads page 4 would query the db for it even though it’s the same data
@DirtyCajunRice | AppDir depends on how you build your table. if you make it page.tsx based it wont
TaiganOP
oh could you explain what you mean by page.tsx based?
@Taigan oh could you explain what you mean by page.tsx based?
i mean the table is "static" in that the "pages" are actually different paths. e.g. /mypage /mypage/2 /mypage/3
it all shows as the same page
but under the hood you are using a slug as [pageId]
TaiganOP
gotcha, that approach makes sense. I'll consider that. thank you! @DirtyCajunRice | AppDir