Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved the data constant outside of the ssrExchange closure #3652

Closed
wants to merge 1 commit into from

Conversation

sebaholesz
Copy link

Summary

In scenarios when ssrExchange was called multiple times, each time a new reference of the data constant was created. However, within the ssr function defined inside ssrExchange, only the very first reference of the data variable was used, which caused a mismatch. For example, in Next.js this caused a bug where during client-side navigation, if a query was prefetched inside getServerSideProps of the page the user has navigated to, the result was stored in the SSR cache, but it was never used, as it was filled into a different object (different reference) than the one that was used originally. Because of this, the URQL client searched for the data in the wrong place and it never found it. This caused the query to be refetched and the layout to shift because of the missing data.

Set of changes

The data constant (object to which the SSR-prefetched data is stored) was moved outside of the ssrExchange closure to ensure just a single reference being used.

- this was done because in certain scenarios when ssrExchange
was called multiple times, there were multiple referrences of
data involved, but the ssr function only worked with the original
one, which caused invalid scenarios
Copy link

changeset-bot bot commented Aug 7, 2024

⚠️ No Changeset found

Latest commit: 3ac82a3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@kitten
Copy link
Member

kitten commented Aug 7, 2024

This change isn't quite right and is a small misunderstanding of how SSR hydration basically has to work.

We can't hoist const data: Record<string, SerializedResult | null> outside of ssrExchange because that would mean that we create a global record. This would mean that, on the server-side, all results are combined from every request. That's explicitly not what the ssrExchange is for.

In fact, that's basically exposing clients to results that have been generated for different requests, and in fact breaks the ssrExchange guarantees.

To also note this, it's very rare that an exchange will have global state in general, since all state should be scoped to a specific Client. The ssrExchange is somewhat of an exception even, since we create it separately to the Client to gain access to some of its methods, but that's still state that's scoped per instance.

@kitten kitten closed this Aug 7, 2024
@sebaholesz
Copy link
Author

@kitten I understand and agree that this was rather a hotfix that did not make any sense. After more investigation and looking into the problem, as well as trying to reproduce it inside a minimal Stackblitz, I was able to write my thoughts down and would appreciate your opinion on this: #3653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants