Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

environmentManager.ts

44 lines1.4 KB

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