You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using React Query in a Next.js project where pages rely on getInitialProps (I can't migrate to getServerSideProps yet).
I'm trying to avoid refetching data like authentication status on client-side navigation, so I'm using dehydrate(queryClient) in getInitialProps (on SSR), and relying on hydration + cache on the client. This works well — except that in client-side navigations, getInitialProps runs again and creates a new QueryClient().
To avoid re-fetching, I tried using a singleton QueryClient instance, and it works — but I'm concerned about SSR safety and cross-request data leaks, since I couldn't find any official guidance for this specific case (getInitialProps + SSR + hydration).
Is this approach considered safe in terms of avoiding SSR data leakage? Or is there a better/officially recommended way to handle QueryClient instantiation across SSR + getInitialProps usage?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there
I'm using React Query in a Next.js project where pages rely on
getInitialProps
(I can't migrate to getServerSideProps yet).I'm trying to avoid refetching data like authentication status on client-side navigation, so I'm using
dehydrate(queryClient)
ingetInitialProps
(on SSR), and relying on hydration + cache on the client. This works well — except that in client-side navigations,getInitialProps
runs again and creates a newQueryClient()
.To avoid re-fetching, I tried using a singleton
QueryClient
instance, and it works — but I'm concerned about SSR safety and cross-request data leaks, since I couldn't find any official guidance for this specific case (getInitialProps + SSR + hydration).Here's the utility I'm using:
I use it like this:
In _app.js, for QueryClientProvider :
const [queryClient] = React.useState(() => getQueryClient());
In
getInitialProps
, to create the client for hydration :Is this approach considered safe in terms of avoiding SSR data leakage? Or is there a better/officially recommended way to handle QueryClient instantiation across SSR + getInitialProps usage?
Thanks a lot for your work and your time
Beta Was this translation helpful? Give feedback.
All reactions