Skip to content

useQuery didn't fetch from server at mount time with { fetchPolicy: 'network-only', nextFetchPolicy: 'cache-only' } when React.StrictMode is enabled. #10159

@karukade

Description

@karukade

Intended outcome:

Fetch data from the server at mount time even when React.StrictMode is enabled and useQuery is used with options {fetchPolicy: 'network-only', nextFetchPolicy: 'cache-only'}.

Actual outcome:

Did not fetch on mount.
I think it caused by StrictMode's double-invokes effects behavior.
useQuery seems to behaves like below.

mount -> trying to fetch from server
unmount -> cancel fetch
mount -> trying to fetch from cache because of nextFetchPolicy: cache-only

When StrictMode disabled, useQuery fetch from server on mount.

How to reproduce the issue:

  • Enable the React.StrictMode.
  • Use useQuery with {fetchPolicy: 'network-only', nextFetchPolicy: 'cache-only'}.

Reproduction: https://codesandbox.io/s/apollo-react-strict-mode-pd01ll?file=/src/App.js

Versions

System:
    OS: macOS 11.6.2
  Binaries:
    Node: 16.13.0 - ~/.volta/tools/image/node/16.13.0/bin/node
    Yarn: 1.22.10 - ~/.volta/tools/image/yarn/1.22.10/bin/yarn
    npm: 8.1.0 - ~/.volta/tools/image/node/16.13.0/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Firefox: 95.0.2
    Safari: 15.1
  npmPackages:
    @apollo/client: ^3.6.9 => 3.6.9 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions