Skip to content

SSR: queries are cached on server #70

@cherniavskii

Description

@cherniavskii

I've started to use react-query with next.js SSR using initialData option.
It works pretty good, but looks like queries are cached in server js bundle and are used across all requests.

Here's a minimal example reproducing the issue: https://codesandbox.io/s/react-query-ssr-cache-rkfsh

And a GIF which demonstrates the issue:
Screen Recording 2019-12-09 at 15 55 15

Note, that user index is incremented every time, but server always renders 1.
Here's what happens:

  • index is 1, there's no queries stored yet
  • user requests the page, data query is created and initialData is set
  • user refreshes the page
  • new initialData is passed to useQuery, but there already is a data query, so cached data is user on server render
  • page is rendered on client, since there's no queries in client bundle, new initialData is used

I've also checked if swr suffer from the same issue, but it works just fine - see https://codesandbox.io/s/swr-ssr-cache-qrmgn

Do you have any idea how it can be handled on react-query side?
As a workaround, I'm calling clearCachedQueries in _document.getInitialProps, which clears cached queries on server on every request

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