Unable to read cached data in getInitialProps #3764
-
Hello 👋 I'm using Next.js and want to read cached data inside However, despite fetching the data prior to the route change, the data does not exist in the cache. Here's a simple example... Data is fetched via
When the user redirects to the next page, the cached data is not found and a fresh api call is made.
Oddly, I'm able to read the existing data just fine via I couldn't find a discussion on this or an explanation in the SSR section of the docs as to why this is happening. Is it something to do with Any help would be much appreciated, thanks 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think when you do frontend routing with next, it will still call functions like Some ways around this that I know of:
|
Beta Was this translation helpful? Give feedback.
I think when you do frontend routing with next, it will still call functions like
getInitialProps
orgetServerSideProps
. The functions are usually self-contained, meaning thenew QueryClient
is created inside of it. That also means that the client is always empty, so the fetch from prefetch will always happen.Some ways around this that I know of:
cache-control: max-age
on the response with the time of caching that you want. That way, the request will be made, but it will not hit your server, but come from the browser cache instead. As an advantage, those fetches will also succeed while you're offline.