Skip to content

Cached data bug after page navigation with next-urql + exchange-graphcache #3160

@dizlexik

Description

@dizlexik

Describe the bug

I've created a reproduction of an issue where useQuery is returning data with __typename fields before page navigation and the same data without __typename fields after navigating. All cached data returned from useQuery after any navigation has occurred on the site is missing __typenames, which isn't a problem in itself since we would never rely on those values without explicitly including that field in the query, but what is having a negative effect is that the cached data coming out of useQuery does not match the previous data by reference. And this is true on every render so when any other component on a page fetches data that touches the cache it results in rerendering everything everywhere. This is bad for performance and also is breaking part of our application where we are feeding an array of objects out of useQuery into a table component which replaces rows when object references change, resulting in any row state (selected checkboxes in our case) being wiped out and replaced with default state.

Our workaround at the moment is to always include __typename in all of our queries. This makes Graphcache behave predictably across page navigation and cached data is always returned with the same references as previous results so things are working as expected/desired. Of course we would rather not have to include this unnecessary field in all of our queries, and there's now a chance that team members may forget to do this and introduce subtle bugs and performance issues. Ideally the results from useQuery would either always include __typename or never include it, and in either case references should always match previous results.

In troubleshooting this we came across a number of issues/PRs that were somewhat related to our issue here, but not exactly, and I couldn't find anything calling out page navigation in Next causing this strange change in how Graphcache behaves. Here are some links here for reference: #1268 #1366 #2701 #2718

Reproduction

https://codesandbox.io/p/sandbox/condescending-resonance-pndq8r

Urql version

urql v4.0.0
@urql/exchange-graphcache v6.0.1
next-urql v5.0.0

Validations

  • I can confirm that this is a bug report, and not a feature request, RFC, question, or discussion, for which GitHub Discussions should be used
  • Read the docs.
  • Follow our Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Oh no! A bug or unintented behaviour.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions