Replies: 3 comments 6 replies
-
you can not |
Beta Was this translation helpful? Give feedback.
-
Hi , I've also followed the above docs to setup ssr in nextjs app router
Currently every time a user clicks a link to this page it will trigger a prefetch and request from the server. If a new queryClient is returned for each request how would it know if the data is stale? Is there any way to force Nextjs not to call the server when a link is clicked so that the client queryClient will handle data fetching? |
Beta Was this translation helpful? Give feedback.
-
Hello, I've encountered another issue related to this. If the data is already stale, even though server components perform a prefetch on subsequent visits to the same page, the client doesn’t actually use that prefetch. Instead, it makes its own query to the server, resulting in two consecutive requests to the API, even though only one is actually used. This only happens when revisiting the same route—on the initial load, everything works fine, and React Query correctly utilizes the serialized promise. On the other hand, I’ve been researching possible solutions, and so far, I’ve found a way to prevent Next.js from re-rendering server components on subsequent visits to the same route. This can be achieved using the experimental staleTimes option, which caches pages on the client: 🔗 Next.js staleTimes documentation The downside is that this setting applies to all routes globally. However, in my case, it solves the issue since I only need server components to execute on the first visit during a browsing session. Another possible solution, without having to persist the entire queryCache on the server, is to implement a cookie-based system to track which components have already performed a prefetch and prevent them from doing it again. However, it would be necessary to ensure these cookies are invalidated between sessions. |
Beta Was this translation helpful? Give feedback.
-
Expected:
Previously in Pages Router, it was achieved using
getInitialProps
, like so:In App Router, based on the docs, this is the only way to query data on the server:
https://tanstack.com/query/latest/docs/framework/react/guides/advanced-ssr
With this setup it will hit the server and wait for data fetching all the time, which is a slow navigation experience. How to detect whether it's the first load? Or return cached data instantly?
Beta Was this translation helpful? Give feedback.
All reactions