feat(query-core): add refetchIntervalOnServer option#10267
feat(query-core): add refetchIntervalOnServer option#10267K-Mistele wants to merge 1 commit intoTanStack:mainfrom
Conversation
Add a new `refetchIntervalOnServer` option to `QueryObserverOptions` that allows `refetchInterval` to remain active in server environments. By default, `refetchInterval` is disabled when `isServer` is true (i.e. `typeof window === 'undefined'`), which makes sense for SSR scenarios. However, long-running server processes like daemons and background workers that use TanStack Query for data synchronization need polling to function. This option follows the same pattern as `refetchIntervalInBackground` -- a per-query boolean that defaults to `false`, preserving existing behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
📝 WalkthroughWalkthroughThis PR introduces an opt-in mechanism for server-side refetch interval behavior by adding a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can generate a title for your PR based on the changes with custom instructions.Set the |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/query-core/src/__tests__/queryObserver.test.tsx (1)
896-920: Consider using a more precise assertion for consistency.The test correctly verifies that refetch intervals work on the server when
refetchIntervalOnServer: true. However, line 916 usestoBeGreaterThan(1)which is less precise than similar tests in this file (e.g., lines 750-751 use exacttoBe(2)).With a 30ms wait and 10ms interval, the expected count should be 4 (1 initial + 3 interval fetches).
🔧 Optional: Use exact count for consistency
// Should have the initial fetch plus refetches from the interval - expect(count).toBeGreaterThan(1) + expect(count).toBe(4)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-core/src/__tests__/queryObserver.test.tsx` around lines 896 - 920, The assertion in the test for QueryObserver ('should refetch on server when refetchIntervalOnServer is true') is imprecise: replace the loose expect(count).toBeGreaterThan(1) with an exact expectation matching the configured timings—given refetchInterval: 10 and awaiting vi.advanceTimersByTimeAsync(30) (1 initial fetch + 3 interval fetches), assert expect(count).toBe(4); update the assertion in the test that uses the count variable and the observer/QueryObserver setup accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/query-core/src/__tests__/queryObserver.test.tsx`:
- Around line 896-920: The assertion in the test for QueryObserver ('should
refetch on server when refetchIntervalOnServer is true') is imprecise: replace
the loose expect(count).toBeGreaterThan(1) with an exact expectation matching
the configured timings—given refetchInterval: 10 and awaiting
vi.advanceTimersByTimeAsync(30) (1 initial fetch + 3 interval fetches), assert
expect(count).toBe(4); update the assertion in the test that uses the count
variable and the observer/QueryObserver setup accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8ced1190-c84a-404e-96e3-d4fe6d2ea623
📒 Files selected for processing (3)
packages/query-core/src/__tests__/queryObserver.test.tsxpackages/query-core/src/queryObserver.tspackages/query-core/src/types.ts
Summary
refetchIntervalOnServeroption toQueryObserverOptionsthat allowsrefetchIntervalto remain active in server environmentsfalse, preserving existing behavior whererefetchIntervalis disabled whenisServeris trueMotivation
@tanstack/query-coredefinesisServer = typeof window === 'undefined' || 'Deno' in globalThisand uses it inQueryObserver#updateRefetchIntervalto unconditionally disablerefetchIntervalon the server. This makes sense for SSR, where polling during server-side rendering is wasteful.However, there are legitimate server-side use cases where polling is essential. For example, long-running daemon processes and background workers that use TanStack Query (often via TanStack DB query collections) for real-time data synchronization rely on
refetchIntervalas a polling-based fallback when primary sync channels (like WebSockets or Electric SQL) are unavailable.Currently, the only workaround is to monkey-patch the
isServerexport (as the test utils already do viasetIsServer), which is fragile and not a supported API.API Design
This follows the exact same pattern as the existing
refetchIntervalInBackgroundoption:It can also be set as a default for all queries via
QueryClient:Changes
packages/query-core/src/types.tsrefetchIntervalOnServer?: booleantoQueryObserverOptionswith JSDocpackages/query-core/src/queryObserver.tsthis.options.refetchIntervalOnServerbefore skipping interval setup on serverpackages/query-core/src/__tests__/queryObserver.test.tsxTest plan
should not refetch on server by default-- verifies default behavior is unchangedshould refetch on server when refetchIntervalOnServer is true-- verifies opt-in works🤖 Generated with Claude Code
Summary by CodeRabbit
refetchIntervalOnServerconfiguration option to control server-side query refetch behavior. By default, refetch intervals are now disabled on servers; enable this option to allow interval-based refetches in server environments.