|
I'm trying to make the The Advanced SSR guide suggests exporting a However, the Server Rendering & Hydration guide seems to disagree with this. In a code comment, it says "Creating the queryClient at the file root level makes the cache shared between all requests and means all data gets passed to all users. Besides being bad for performance, this also leaks any sensitive data." First, I don't understand how the data can be passed to all users when it's running in the browser. Second, how is it bad for performance? Which is the correct method for accessing the query client from non-React functions in the browser? |
Replies: 2 comments
|
The “Server Rendering & Hydration” guide comes first. It explains general principles, and one of them is: Don’t create a The Advanced SSR guide specifically focuses on nextJs and streaming in the app directory. Because of how nextJs creates a global suspense fallback (loading.tsx), whatever code we write could be below that suspense boundary, which means the So, there is no disagreeing between the two - they just cover different use-cases. |
|
For anybody reading this looking for a way to satisfy both concernrs:
you might want to put your server-side QueryClient inside an AsyncLocalStorage: https://nodejs.org/api/async_context.html. And |
The “Server Rendering & Hydration” guide comes first. It explains general principles, and one of them is: Don’t create a
new QueryClientas a const outside of the component, because it will be shared between requests. It’s not about the browser, but about the server. With SSR, your code is isomorphic and runs in both environments. So create it inuseStateor an instance ref inside the component, as the guide shows:const [queryClient] = React.useState(...)The Advanced SSR guide specifically focuses on nextJs and streaming in the app directory. Because of how nextJs creates a global suspense fallback (loading.tsx), whatever code we write could be below that suspense boundary, which means …