Skip to content
Source
Sign inSign up
TanStack/query

fix(react-query): ensureSuspenseTimers should ALWAYS set staleTime to 1000 when it is below 1000. #8398

Mergedminseong0324(GitHub profile, opens in a new tab)merged 11 commits intomain fromrefs/pull/8398/headmerged by TkDodo View on GitHub

issue link

Changes

  • Modified ensureSuspenseTimers to enforce a minimum staleTime of 1000ms in Suspense mode
  • Added support for both direct values and function-based staleTime configurations
  • Maintained the existing behavior for values greater than 1000ms
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query

    2. View your CI Pipeline Execution ↗ for commit e9b6f963c5d6fe08d4aa781eb57f27a5c4da872f.

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded2m 4sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded22sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-01-03 17:25:53 UTC

    3. TkDodo reviewed

      can you please add a test for this scenario?

    4. changes:

      1. Fixed logic in ensureSuspenseTimers to properly handle all cases where staleTime needs to be enforced (values below 1000ms, undefined values, and functions returning values below 1000ms)
      2. Added comprehensive test coverage for these scenarios to ensure the behavior works as intended
      • Testing when using a short numeric staleTime value
      • Testing when using a function that returns a short staleTime value
      • Testing when staleTime is explicitly undefined
      • Testing when staleTime is set above 1000ms
      • Testing when using a function that returns a value above 1000ms
      1. Removed the "should refetch when re-mounting" test as it no longer aligns with the new implementation
      image
    5. Open in Stackblitz

      More templates

      @tanstack/angular-query-devtools-experimental

      npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@8398

      @tanstack/angular-query-experimental

      npm i https://pkg.pr.new/@tanstack/angular-query-experimental@8398

      @tanstack/eslint-plugin-query

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@8398

      @tanstack/query-async-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@8398

      @tanstack/query-broadcast-client-experimental

      npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@8398

      @tanstack/query-core

      npm i https://pkg.pr.new/@tanstack/query-core@8398

      @tanstack/query-devtools

      npm i https://pkg.pr.new/@tanstack/query-devtools@8398

      @tanstack/query-persist-client-core

      npm i https://pkg.pr.new/@tanstack/query-persist-client-core@8398

      @tanstack/query-sync-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@8398

      @tanstack/react-query

      npm i https://pkg.pr.new/@tanstack/react-query@8398

      @tanstack/react-query-devtools

      npm i https://pkg.pr.new/@tanstack/react-query-devtools@8398

      @tanstack/react-query-next-experimental

      npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@8398

      @tanstack/react-query-persist-client

      npm i https://pkg.pr.new/@tanstack/react-query-persist-client@8398

      @tanstack/solid-query

      npm i https://pkg.pr.new/@tanstack/solid-query@8398

      @tanstack/solid-query-devtools

      npm i https://pkg.pr.new/@tanstack/solid-query-devtools@8398

      @tanstack/solid-query-persist-client

      npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@8398

      @tanstack/svelte-query

      npm i https://pkg.pr.new/@tanstack/svelte-query@8398

      @tanstack/svelte-query-devtools

      npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@8398

      @tanstack/svelte-query-persist-client

      npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@8398

      @tanstack/vue-query

      npm i https://pkg.pr.new/@tanstack/vue-query@8398

      @tanstack/vue-query-devtools

      npm i https://pkg.pr.new/@tanstack/vue-query-devtools@8398

      commit: e9b6f96

    6. TkDodo reviewed

      packages/react-query/src/__tests__/useSuspenseQuery.test.tsx line 330
      @@ -327,4 +327,3 @@
      327327 consoleMock.mockRestore()
      328328 })
      329329
      330- it('should refetch when re-mounting', async () => {
      TkDodocommented

      why was this test removed?

      Since we now enforce a minimum staleTime of 1000ms in Suspense mode for better performance and consistency, the "should refetch when re-mounting" test is no longer valid. This test expected immediate refetching on remount with staleTime: 0, but this behavior would conflict with our implementation that ensures queries remain fresh for at least 1000ms when using Suspense.

      The test was designed to verify a behavior that we've intentionally changed to prevent unnecessary refetches and provide a more stable user experience in Suspense mode. Removing this test ensures our test suite accurately reflects our current design decision of enforcing minimum staleTime.

      OutdatedResolvedby TkDodo
    7. TkDodo approved these changes

    8. TkDodo merged this0503282

    9. TkDodo closed this

Sign in to join this conversation.