Environment functions are utilities designed to define and control function execution based on the runtime environment—whether the code is running on the client or the server. These utilities help ensure that environment-specific logic is executed safely and intentionally, preventing runtime errors and improving maintainability in fullstack or isomorphic applications.
Start provide three core environment functions:
Use createIsomorphicFn() to define functions that behave differently depending on whether they are called on the client or the server. This is useful for safely sharing logic across environments while delegating environment-specific behavior to appropriate handlers.
import { createIsomorphicFn } from '@tanstack/react-start'
const getEnv = createIsomorphicFn()
.server(() => 'server')
.client(() => 'client')
const env = getEnv()
// ℹ️ On the **server**, it returns `'server'`.
// ℹ️ On the **client**, it returns `'client'`.
import { createIsomorphicFn } from '@tanstack/react-start'
const getEnv = createIsomorphicFn()
.server(() => 'server')
.client(() => 'client')
const env = getEnv()
// ℹ️ On the **server**, it returns `'server'`.
// ℹ️ On the **client**, it returns `'client'`.
Here is an example of createIsomorphicFn() with only server implementation:
import { createIsomorphicFn } from '@tanstack/react-start'
const serverImplementationOnly = createIsomorphicFn().server(() => 'server')
const server = serverImplementationOnly()
// ℹ️ On the **server**, it returns `'server'`.
// ℹ️ On the **client**, it is no-op (returns `undefined`)
import { createIsomorphicFn } from '@tanstack/react-start'
const serverImplementationOnly = createIsomorphicFn().server(() => 'server')
const server = serverImplementationOnly()
// ℹ️ On the **server**, it returns `'server'`.
// ℹ️ On the **client**, it is no-op (returns `undefined`)
Here is an example of createIsomorphicFn() with only client implementation:
import { createIsomorphicFn } from '@tanstack/react-start'
const clientImplementationOnly = createIsomorphicFn().client(() => 'client')
const client = clientImplementationOnly()
// ℹ️ On the **server**, it is no-op (returns `undefined`)
// ℹ️ On the **client**, it returns `'client'`.
import { createIsomorphicFn } from '@tanstack/react-start'
const clientImplementationOnly = createIsomorphicFn().client(() => 'client')
const client = clientImplementationOnly()
// ℹ️ On the **server**, it is no-op (returns `undefined`)
// ℹ️ On the **client**, it returns `'client'`.
Here is an example of createIsomorphicFn() without any environment specific implementation:
import { createIsomorphicFn } from '@tanstack/react-start'
const noImplementation = createIsomorphicFn()
const noop = noImplementation()
// ℹ️ On both **client** and **server**, it is no-op (returns `undefined`)
import { createIsomorphicFn } from '@tanstack/react-start'
const noImplementation = createIsomorphicFn()
const noop = noImplementation()
// ℹ️ On both **client** and **server**, it is no-op (returns `undefined`)
A no-op (short for "no operation") is a function that does nothing when executed - it simply returns undefined without performing any operations.
// basic no-op implementation
function noop() {}
// basic no-op implementation
function noop() {}
The serverOnly and clientOnly helpers enforce strict environment-bound execution. They ensure the decorated function is only callable in the correct runtime context. If misused, they throw descriptive runtime errors to prevent unintentional logic execution.
import { serverOnly } from '@tanstack/react-start'
const foo = serverOnly(() => 'bar')
foo() // ✅ On server: returns "bar"
// ❌ On client: throws "serverOnly() functions can only be called on the server!"
import { serverOnly } from '@tanstack/react-start'
const foo = serverOnly(() => 'bar')
foo() // ✅ On server: returns "bar"
// ❌ On client: throws "serverOnly() functions can only be called on the server!"
import { clientOnly } from '@tanstack/react-start'
const foo = clientOnly(() => 'bar')
foo() // ✅ On client: returns "bar"
// ❌ On server: throws "clientOnly() functions can only be called on the client!"
import { clientOnly } from '@tanstack/react-start'
const foo = clientOnly(() => 'bar')
foo() // ✅ On client: returns "bar"
// ❌ On server: throws "clientOnly() functions can only be called on the client!"
Note
These functions are useful for API access, filesystem reads, using browser APIs, or other operations that are invalid or insecure outside their intended environment.
Environment functions are tree-shaken based on the environment for each bundle produced.
Functions created using createIsomorphicFn() are tree-shaken. All codes inside .client() are not included in server bundle, and vice-versa.
On the server, implementation of clientOnly functions are replaced with a function that throws an Error. The reverse is true for serverOnly functions on the client.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.