Skip to content

SSR + NextJS does not seem to Hydrate data #1661

Closed

Description

@RickEyre

I'm following the guide here to setup NextJS with Hydration. I can confirm that my dehydrated state is being passed correctly through page props, however on the first SSR render for my page component the hydrated state does not seem to be present. data is undefined and my component breaks as I depend on it to be loaded initially.

export default function App({ Component, pageProps }) {
  // console.log here confirms dehydrated state exists and data has been populated
  return (
    <React.Fragment>
      <QueryClientProvider client={queryClient}>
        <Hydrate state={pageProps.dehydratedState}>
          <Component {...pageProps} />
        </Hydrate>
      </QueryClientProvider>
    </React.Fragment>
  );
}
export async function getServerSideProps(context) {
  const { query: { id } } = context;
  const queryClient = new QueryClient();
  
  const api = getAPI(context);
  await api.reAuthenticate();
  
  await queryClient.prefetchQuery(
    [ 'resources', id ],
    () => api.getResource(id)
  );
  
  return {
    props: { dehydratedState: dehydrate(queryClient) }
  };

}
export default function Component() {
  const router = useRouter();
  
  // user query client confirms here that dehydrated data is not present
  const { data } = useQuery(
    [ 'resources', router.query.id ],
    () => getAPI().getResource(router.query.id)
  );
  
  // breaks because data is not loaded initially
  return (
    <div>{data.id}</div>
  );
}

Activity

  1. lynxtaa commented on Jan 17, 2021

    @lynxtaa

    It works in react-query@3.5.13, doesn't work in 3.5.14.
    Maybe because of this PR #1643 ?

  2. TkDodo commented on Jan 17, 2021

    @TkDodo
    Collaborator

    Maybe because of this PR #1643 ?

    possibly. I was raising that concern in the origin discussion here:
    #1641 (reply in thread)

  3. RickEyre commented on Jan 17, 2021

    @RickEyre
    Author

    Ah I see. Yes that would break it, I think, as useEffect is not run during SSR in NextJS.

  4. tannerlinsley commented on Jan 17, 2021

    @tannerlinsley
    Member

    Fixed with eb3d700

  5. RickEyre commented on Jan 17, 2021

    @RickEyre
    Author

    Thanks everyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions