Skip to content

Commit

Permalink
Merge branch 'main' into chore-prevent-actions-from-running-on-forks
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Jan 23, 2023
2 parents e93d7ad + b5ccef2 commit ba3dacd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-bananas-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Passes `getServerSnapshot` to `useSyncExternalStore` so that it doesn't trigger a `Missing getServerSnapshot` error when using `useFragment_experimental` on the server.
23 changes: 13 additions & 10 deletions src/react/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,30 @@ export function useFragment_experimental<TData, TVars>(
const resultRef = useRef<UseFragmentResult<TData>>();
let latestDiff = cache.diff<TData>(diffOptions);

// Used for both getSnapshot and getServerSnapshot
const getSnapshot = () => {
const latestDiffToResult = diffToResult(latestDiff);
return resultRef.current &&
equal(resultRef.current.data, latestDiffToResult.data)
? resultRef.current
: (resultRef.current = latestDiffToResult);
};

return useSyncExternalStore(
forceUpdate => {
(forceUpdate) => {
return cache.watch({
...diffOptions,
immediate: true,
callback(diff) {
if (!equal(diff, latestDiff)) {
resultRef.current = diffToResult(latestDiff = diff);
resultRef.current = diffToResult((latestDiff = diff));
forceUpdate();
}
},
});
},

() => {
const latestDiffToResult = diffToResult(latestDiff);
return resultRef.current &&
equal(resultRef.current.data, latestDiffToResult.data)
? resultRef.current
: (resultRef.current = latestDiffToResult);
},
getSnapshot,
getSnapshot
);
}

Expand Down

0 comments on commit ba3dacd

Please sign in to comment.