Skip to content
Source
Sign inSign up
TanStack/query

Unsafe SSR-pattern described in docs and example #1762

Send to my agent
ClosedEphem(GitHub profile, opens in a new tab) opened this issue 2 comments

The SSR docs on hydration currently describe an unsafe pattern:

// _app.jsx
 import { QueryClient, QueryClientProvider } from 'react-query'
 import { Hydrate } from 'react-query/hydration'
 
 // ---- THIS IS THE PART THAT IS UNSAFE ----
 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!

  1. Ephem(GitHub profile, opens in a new tab) added labelsdocumentation

  2. Ephem(GitHub profile, opens in a new tab) added labelshelp wanted

  3. Ephem(GitHub profile, opens in a new tab) added labelsgood first issue

  4. I’ll take care of this.

    Tanner Linsley On Feb 7, 2021, 1:29 PM -0700, Fredrik Höglund <notifications@github.com>, wrote:

    The SSR docs on hydration currently describe an unsafe pattern: // _app.jsx import { QueryClient, QueryClientProvider } from 'react-query' import { Hydrate } from 'react-query/hydration'

    // ---- THIS IS THE PART THAT IS UNSAFE ---- const queryClient = new QueryClient()

    export default function MyApp({ Component, pageProps }) { return ( <Component {...pageProps} /> ) } 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! — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

  5. tannerlinsley closed this

  6. :tada: This issue has been resolved in version 3.8.3 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  7. tannerlinsley added labelsreleased

Sign in to join this conversation.