Open History to see the last commit
1import { isServer as defaultIsServer } from './utils'
2β
3/**
4 * A function that returns whether the code is running on the server.
5 */
6export type IsServerValue = () => boolean
7β
8let isServerFn: IsServerValue = () => defaultIsServer
9β
10/**
11 * Returns whether the current runtime should be treated as a server environment.
12 * @returns `true` if the runtime is treated as a server.
13 */
14export const isServer = (): boolean => isServerFn()
15β
16/**
17 * Manages how TanStack Query detects whether the current runtime should be treated as
18 * server-side, which disables scheduling refetch timers and changes the default `retry` count
19 * and `gcTime`. By default, the detection treats a missing `window` (or the presence of a
20 * `Deno` global) as server.
21 *
22 * Override this for runtimes where that default detection would give the wrong answer β for
23 * example, a Service Worker, where `window` is undefined even though the environment should
24 * behave like a client.
25 * @example
26 * ```ts
27 * import { environmentManager } from '@tanstack/query-core'
28 *
29 * environmentManager.setIsServer(() => false)
30 * ```
31 */
32export const environmentManager = {
33 isServer,
34 /**
35 * Overrides the server check globally.
36 * @param isServerValue - A function that returns whether the runtime should be treated as a
37 * server.
38 */
39 setIsServer(isServerValue: IsServerValue): void {
40 isServerFn = isServerValue
41 },
42}