The SSR docs on hydration currently describe an unsafe pattern:
import { QueryClient, QueryClientProvider } from 'react-query'
import { Hydrate } from 'react-query/hydration'
const queryClient = new QueryClient()
export default function MyApp({ Component, pageProps }) {
return (
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<Component {...pageProps} />
</Hydrate>
</QueryClientProvider>
)
}
Creating a client (and behind the scenes a cache) in the module scope will mean that cache is shared between all requests on the server which is unsafe since it can leak sensitive data between requests.
The Next.js example uses the same pattern and should also be updated.
The correct way to create the client for SSR is to do it on the initial render and save it in state/on a ref (for reference, this was done automatically in the QueryCacheProvider in React Query v2 if no queryCache was provided).
I wont have time to submit a fix for this myself in the near future, so if someone wants to submit a PR with an update that would be much appreciated!
I’ll take care of this.
Tanner Linsley On Feb 7, 2021, 1:29 PM -0700, Fredrik Höglund <notifications@github.com>, wrote: