Capítulo 59 de 80

Chapter 59: environmentManager

Core Idea

Controls how TanStack Query decides "is this a server or browser runtime" — override it only for non-standard runtimes (extension workers, edge functions) where the default check would misclassify the environment.

Key Concepts

  • isServer(): returns whether the current runtime is currently treated as server-side — by default, delegates to query-core's exported isServer utility.
  • setIsServer(fn): replaces the detection function entirely with a custom () => boolean — e.g. () => typeof window === 'undefined' && !('chrome' in globalThis) to distinguish a real server from a browser-extension worker (which also lacks window but shouldn't be treated as "server").
  • Restoring default behavior: environmentManager.setIsServer(() => isServer) re-establishes the built-in check.

Code Examples

environmentManager.setIsServer(() => typeof window === 'undefined' && !('chrome' in globalThis))
  • What it demonstrates: excluding extension-worker runtimes (no window, but has chrome) from being misclassified as server environments.

Key Takeaways

  1. Only override this for genuinely non-standard runtimes — the default detection is correct for normal SSR/browser setups.
  2. This affects behavior throughout the library that branches on server-vs-client (e.g. default gcTime, getQueryClient() patterns in Advanced Server Rendering).

Connects To

  • Server Rendering & Hydration: the server/client branching this manager underlies.
  • Advanced Server Rendering: environmentManager.isServer() used directly in the getQueryClient() pattern.