Skip to content

initialStale not working, at least not always #1007

Closed

Description

@emanuel-lundman

Describe the bug
Upgraded from version 2.5.14 to 2.17.0.
Now I've got an issue I believe is a bug with initialStale true not working as it should

When running my app I notice that at:
https://github.com/tannerlinsley/react-query/blob/c7e342a91a79ea9d005ffa93e491a6c879a1a7e8/src/core/queryObserver.ts#L232

I have a currentResult before any fetch has been made and only initialData has loaded.
So above statement is not true and initialStale value is never taken into account.
That's what I believe without any deep knowledge of the react-query code.

This is what currentResult looks like for me when reaching that point without any fetch and then never running a fetch even though initialStale is set to true:

currentResult:

{
  "status": "success",
  "isLoading": false,
  "isSuccess": true,
  "isError": false,
  "isIdle": false,
  "data": [],
  "error": null,
  "failureCount": 0,
  "isFetched": false,
  "isFetchedAfterMount": false,
  "isFetching": false,
  "isFetchingMore": false,
  "isInitialData": true,
  "isPreviousData": false,
  "isStale": true,
  "updatedAt": 1599993452356
}

Expected behavior
New data to be fetched on load

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Browser: chrome, safari

Activity

  1. boschni commented on Sep 13, 2020

    @boschni
    Contributor

    We do have a test which should cover this: https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useQuery.test.tsx#L1209 . Is your case different?

  2. emanuel-lundman commented on Sep 13, 2020

    @emanuel-lundman
    Author

    @boschni
    Must be, there must be some change between 2.5.14 and 2.17.0 that alters the behavior because before upgrading initialStale true worked fine, now I have to wait (since I don't always have staleTime set to 0 in query config).
    If I load the page, leave the window and wait for the staletime to expire and go back (trigger a refetch with window focus) the data is loaded and page propagated.

    The test checks for the following QueryResults

    expect(states).toMatchObject([
          { data: 'initial', isStale: true, isFetching: false },
          { data: 'initial', isStale: true, isFetching: true },
          { data: 'data', isStale: true, isFetching: false },
        ])

    I get this QueryResult:

    {
      data: [], // my inital data
      isStale: false,
      isFetching: false,
    
      error: null,
      failureCount: 0,
      isError: false,
      isFetched: false,
      isFetchedAfterMount: false,
      isFetchingMore: false,
      isIdle: false,
      isInitialData: true,
      isLoading: false,
      isPreviousData: false,
      isSuccess: true,
      status: "success",
      updatedAt: 1600006270582,
      ...
    };

    And from what I gather from browser devtools it happens because I've got a currentResult object when reaching
    https://github.com/tannerlinsley/react-query/blob/c7e342a91a79ea9d005ffa93e491a6c879a1a7e8/src/core/queryObserver.ts#L232

    So the initialStale logic is never run because execution goes directly to:
    https://github.com/tannerlinsley/react-query/blob/c7e342a91a79ea9d005ffa93e491a6c879a1a7e8/src/core/queryObserver.ts#L241

    Now, I don't know why I have a currentResult at that stage if I'm not supposed to have one,
    but from the data in the currentResult I think it should anyway figure out that the conditions in the if statement should be true and initialStale logic and everything should be run?

    (copied from original issue above)
    currentResult

    {
      "status": "success",
      "isLoading": false,
      "isSuccess": true,
      "isError": false,
      "isIdle": false,
      "data": [],
      "error": null,
      "failureCount": 0,
      "isFetched": false,
      "isFetchedAfterMount": false,
      "isFetching": false,
      "isFetchingMore": false,
      "isInitialData": true,
      "isPreviousData": false,
      "isStale": true,
      "updatedAt": 1599993452356
    }

    One could probably fix it by just changing the if statement to something like
    if ((!currentResult || currentResult.isInitialData) && state.isInitialData) {
    or something, but I know too little about the source to know if that's a fix or just a bad workaround for my particular problem.

  3. emanuel-lundman commented on Sep 13, 2020

    @emanuel-lundman
    Author

    @boschni about the test, if you add staleTime: 60_000 to the test https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useQuery.test.tsx#L1209
    The test fails. initialStale=true with a staleTime seems not to be working. Should still be stale before first fetch even though it has a stale time.
    Haven't had time yet to check how to fix it without breaking anything else though.

  4. boschni commented on Sep 13, 2020

    @boschni
    Contributor

    Are you able to test if this PR fixes it? #1010

  5. emanuel-lundman commented on Sep 13, 2020

    @emanuel-lundman
    Author

    @boschni Seems like it works! At least for me :)
    It now fetches on first page load when initialStale and staleTime has been set.

    By the way. What is the best/easiest way to test a react-query pull request?
    Tried pulling, build and link but got the Error: Invalid hook call... (more than one react error)

    So resorted to yarn add https://github.com/boschni/react-query.git#feature/keep-initial-stale and then:
    yarn, yarn build, yarn build:types
    and then remove the node_modules directory in the react-query folder to get it running.

  6. emanuel-lundman commented on Sep 14, 2020

    @emanuel-lundman
    Author

    Fixed and working in: 460ab33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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