Skip to content

Can't get cache working on Next.js #5268

Answered by TkDodo
hkrawczyk asked this question in Q&A
Discussion options

You must be logged in to vote
  1. if you prefetch data on the server, it will always run, because you create a new QueryClient() in getServerSideProps. The queryClient holds the cache, and a new client means an empty cache. So passing staleTime there is likely irrelevant. So the caching will work, but only on the client. If you prefetch on the server, every request will refetch. The only way to also cache that would be to setup server-side caching, e.g. with redis, and maybe use the persisters to write to there. That is simply because the react-query cache is an in-memory cache. You can theoretically create the QueryClient outside of getServerSideProps, but then it will be shared between users and requests, and it's bas…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@hkrawczyk
Comment options

@synasapmob
Comment options

@pjborowiecki
Comment options

Answer selected by hkrawczyk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants