Skip to content
Source
Sign inSign up
TanStack/query

Tanstack Query refetch does not work in certain environments (vscode extension, chrome extension) #10139

ClosedJustinGrote(GitHub profile, opens in a new tab) opened this issue 5 comments

Describe the bug

Issue

This isServer check prevents the queries from running in these environments, mistakenly seeing them as servers anywhere globalThis.window is undefined.

https://github.com/TanStack/query/blob/984bfa6f15271c0ba7c750d35daff2f6fb596667/packages/query-core/src/utils.ts#L89

Related Discussion

Workarounds

  • Use queryClient.invalidateQueries in a setInterval
  • [Maybe?] Put a "dummy" item into globalthis.window so that it isn't seen as a server. WARNING: This could have major unexpected side effects.

Proposed Fix

(by @TkDodo) Decouple setInterval calls from isServer checks. None if this code runs on the server anyways, because subscriptions are only created on the client (they run in effects / useSyncExternalStore, so not on the server)

Your minimal, reproducible example

See above

Steps to reproduce

See above

Expected behavior

See above

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Chrome extensions and vscode extensions

Tanstack Query adapter

vanilla

TanStack Query version

v5.90.20

TypeScript version

v5.8.3

Additional context

No response

  1. This pnpm hotpatch removing the isServer checks worked for me, for potential future pull requests

    diff --git a/build/modern/queryObserver.cjs b/build/modern/queryObserver.cjs
    index 17db8270b9228011d5b3ce591f5ed3ad905ba2a0..af79f94c363f2285368b0191ae1c33dfcf7b5caa 100644
    --- a/build/modern/queryObserver.cjs
    +++ b/build/modern/queryObserver.cjs
    @@ -206,7 +206,7 @@ var QueryObserver = class extends import_subscribable.Subscribable {
           this.options.staleTime,
           this.#currentQuery
         );
    -    if (import_utils.isServer || this.#currentResult.isStale || !(0, import_utils.isValidTimeout)(staleTime)) {
    +    if (this.#currentResult.isStale || !(0, import_utils.isValidTimeout)(staleTime)) {
           return;
         }
         const time = (0, import_utils.timeUntilStale)(this.#currentResult.dataUpdatedAt, staleTime);
    @@ -223,7 +223,7 @@ var QueryObserver = class extends import_subscribable.Subscribable {
       #updateRefetchInterval(nextInterval) {
         this.#clearRefetchInterval();
         this.#currentRefetchInterval = nextInterval;
    -    if (import_utils.isServer || (0, import_utils.resolveEnabled)(this.options.enabled, this.#currentQuery) === false || !(0, import_utils.isValidTimeout)(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
    +    if ((0, import_utils.resolveEnabled)(this.options.enabled, this.#currentQuery) === false || !(0, import_utils.isValidTimeout)(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
           return;
         }
         this.#refetchIntervalId = import_timeoutManager.timeoutManager.setInterval(() => {
    diff --git a/build/modern/queryObserver.js b/build/modern/queryObserver.js
    index 279baf4369b8197972b6ae3fcd021a2b53d15dc1..343d93f83e864441ba08716a7ce6fa0a798afcb4 100644
    --- a/build/modern/queryObserver.js
    +++ b/build/modern/queryObserver.js
    @@ -191,7 +191,7 @@ var QueryObserver = class extends Subscribable {
           this.options.staleTime,
           this.#currentQuery
         );
    -    if (isServer || this.#currentResult.isStale || !isValidTimeout(staleTime)) {
    +    if (this.#currentResult.isStale || !isValidTimeout(staleTime)) {
           return;
         }
         const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime);
    @@ -208,7 +208,7 @@ var QueryObserver = class extends Subscribable {
       #updateRefetchInterval(nextInterval) {
         this.#clearRefetchInterval();
         this.#currentRefetchInterval = nextInterval;
    -    if (isServer || resolveEnabled(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
    +    if (resolveEnabled(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
           return;
         }
         this.#refetchIntervalId = timeoutManager.setInterval(() => {
    diff --git a/build/modern/removable.js b/build/modern/removable.js
    index 8eb8f6c65996336a5dad48c7b6b36b9d1a4ef3d6..b04a9b21499525e3299d31f35f321d1d2fd01ea3 100644
    --- a/build/modern/removable.js
    +++ b/build/modern/removable.js
    @@ -17,7 +17,7 @@ var Removable = class {
       updateGcTime(newGcTime) {
         this.gcTime = Math.max(
           this.gcTime || 0,
    -      newGcTime ?? (isServer ? Infinity : 5 * 60 * 1e3)
    +      newGcTime ?? (5 * 60 * 1e3)
         );
       }
       clearGcTimeout() {
    diff --git a/build/modern/retryer.js b/build/modern/retryer.js
    index 9aec541d35267a74756f11968a3ca7ca88ff636d..227bb46c20273116885f0497c39f379bde65ed3a 100644
    --- a/build/modern/retryer.js
    +++ b/build/modern/retryer.js
    @@ -82,7 +82,7 @@ function createRetryer(config) {
           if (isResolved()) {
             return;
           }
    -      const retry = config.retry ?? (isServer ? 0 : 3);
    +      const retry = config.retry ?? 3;
           const retryDelay = config.retryDelay ?? defaultRetryDelay;
           const delay = typeof retryDelay === "function" ? retryDelay(failureCount, error) : retryDelay;
           const shouldRetry = retry === true || typeof retry === "number" && failureCount < retry || typeof retry === "function" && retry(failureCount, error);
    
  2. I’m rather thinking that isServer should return false for these environments, as they aren’t a sever? What’s the canonical way to detect these environments ?

  3. That's probably going to be pretty specific, for instance you can check for chrome.runtime.id global, or the presense of the vscode global, but I don't know of any universal way to detect all of these environments. Perhaps just enable a config switch to override it?

  4. @JustinGrote can you try out the preview build from this PR:

  5. there’s also this alternative approach with an environmentManager:

  6. TkDodo added labelspackage: query-core

  7. TkDodo closed this

Sign in to join this conversation.