apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.32k stars 2.65k forks source link

Server-Side Rendering Error with @apollo/client 3.11.0 in NextJS 14.2.5 #11976

Closed apshenichniy closed 1 month ago

apshenichniy commented 1 month ago

Issue Description

I have a NextJS application using apollo/client v3.11.0. the following error occurs during server-side rendering:

Error: Missing getServerSnapshot, which is required for server-rendered content. Will revert to client rendering. at Object.useSyncExternalStore (/var/task/node_modules/.pnpm/next@14.2.5_@babel+core@7.24.5_@playwright+test@1.45.1_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:41308)

The snippet below triggered an error:

"use client";

const Component = () => {
....
  useSubscription(query, {
    fetchPolicy: "no-cache",
    onData: ({ data: { data } }) => {
      if (data) {
        setState(data);
      }
    },
  });

...
};

The error seems to be related to the implementation of the useSubscription hook. Specifically, it appears that the third parameter (callback for getServerSnapshot) is not being passed to the useSyncExternalStore hook: [Link](https://github.com/apollographql/apollo-client/blob/3d9eb47f292206272478056fa138e0a31d84b0aa/src/react/hooks/useSubscription.ts#L221)

Downgrading to @apollo/client version 3.10.8 resolves the issue.

Link to Reproduction

/

Reproduction Steps

No response

@apollo/client version

3.11.0

jerelmiller commented 1 month ago

Hey @apshenichniy 👋

You're right! We did some refactoring in useSubscription for 3.11 to better work with React Compiler, but looks like we forgot to add that 3rd argument for SSR. useSyncExternalStore was added in 3.11 with this.

I'll dig into this soon and try and figure out what we did before in the SSR case to ensure we mimic that here. Thanks for bringing this to our attention!

github-actions[bot] commented 1 month ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

jerelmiller commented 1 month ago

Hey @apshenichniy 👋

This was just released in 3.11.2. Please upgrade to that version and you should see the error go away. Thanks for reporting this!

github-actions[bot] commented 1 week ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.