Skip to content

Commit bc5e78f

Browse files
authored
docs(start): environment functions for createIsomorphicFn and envOnly (#4806)
1 parent 3178314 commit bc5e78f

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

docs/start/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
"label": "Static Server Functions",
4242
"to": "framework/react/static-server-functions"
4343
},
44+
{
45+
"label": "Environment Functions",
46+
"to": "framework/react/environment-functions"
47+
},
4448
{
4549
"label": "Middleware",
4650
"to": "framework/react/middleware"
@@ -122,6 +126,10 @@
122126
"label": "Static Server Functions",
123127
"to": "framework/solid/static-server-functions"
124128
},
129+
{
130+
"label": "Environment Functions",
131+
"to": "framework/solid/environment-functions"
132+
},
125133
{
126134
"label": "Middleware",
127135
"to": "framework/solid/middleware"
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
id: environment-functions
3+
title: Environment Functions
4+
---
5+
6+
## What Are Environment Functions?
7+
8+
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.
9+
10+
Start provide three core environment functions:
11+
12+
- `createIsomorphicFn`: Compose a single function that adapts to both client and server environments.
13+
- `serverOnly`: Ensures a function can only run on the server.
14+
- `clientOnly`: Ensures a function can only run on the client.
15+
16+
---
17+
18+
## Isomorphic Functions
19+
20+
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.
21+
22+
### Complete Implementation
23+
24+
```tsx
25+
import { createIsomorphicFn } from '@tanstack/react-start'
26+
27+
const getEnv = createIsomorphicFn()
28+
.server(() => 'server')
29+
.client(() => 'client')
30+
31+
const env = getEnv()
32+
// ℹ️ On the **server**, it returns `'server'`.
33+
// ℹ️ On the **client**, it returns `'client'`.
34+
```
35+
36+
### Partial Implementation (Server)
37+
38+
Here is an example of `createIsomorphicFn()` with only server implementation:
39+
40+
```tsx
41+
import { createIsomorphicFn } from '@tanstack/react-start'
42+
43+
const serverImplementationOnly = createIsomorphicFn().server(() => 'server')
44+
45+
const server = serverImplementationOnly()
46+
// ℹ️ On the **server**, it returns `'server'`.
47+
// ℹ️ On the **client**, it is no-op (returns `undefined`)
48+
```
49+
50+
### Partial Implementation (Client)
51+
52+
Here is an example of `createIsomorphicFn()` with only client implementation:
53+
54+
```tsx
55+
import { createIsomorphicFn } from '@tanstack/react-start'
56+
57+
const clientImplementationOnly = createIsomorphicFn().client(() => 'client')
58+
59+
const client = clientImplementationOnly()
60+
// ℹ️ On the **server**, it is no-op (returns `undefined`)
61+
// ℹ️ On the **client**, it returns `'client'`.
62+
```
63+
64+
### No Implementation
65+
66+
Here is an example of `createIsomorphicFn()` without any environment specific implementation:
67+
68+
```tsx
69+
import { createIsomorphicFn } from '@tanstack/react-start'
70+
71+
const noImplementation = createIsomorphicFn()
72+
73+
const noop = noImplementation()
74+
// ℹ️ On both **client** and **server**, it is no-op (returns `undefined`)
75+
```
76+
77+
#### What is a no-op?
78+
79+
A no-op (short for "no operation") is a function that does nothing when executed - it simply returns `undefined` without performing any operations.
80+
81+
```tsx
82+
// basic no-op implementation
83+
function noop() {}
84+
```
85+
86+
---
87+
88+
## `env`Only Functions
89+
90+
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.
91+
92+
### `serverOnly`
93+
94+
```tsx
95+
import { serverOnly } from '@tanstack/react-start'
96+
97+
const foo = serverOnly(() => 'bar')
98+
99+
foo() // ✅ On server: returns "bar"
100+
// ❌ On client: throws "serverOnly() functions can only be called on the server!"
101+
```
102+
103+
### `clientOnly`
104+
105+
```tsx
106+
import { clientOnly } from '@tanstack/react-start'
107+
108+
const foo = clientOnly(() => 'bar')
109+
110+
foo() // ✅ On client: returns "bar"
111+
// ❌ On server: throws "clientOnly() functions can only be called on the client!"
112+
```
113+
114+
> [!NOTE]
115+
> These functions are useful for API access, filesystem reads, using browser APIs, or other operations that are invalid or insecure outside their intended environment.
116+
117+
## Tree Shaking
118+
119+
Environment functions are tree-shaken based on the environment for each bundle produced.
120+
121+
Functions created using `createIsomorphicFn()` are tree-shaken. All codes inside `.client()` are not included in server bundle, and vice-versa.
122+
123+
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.

docs/start/framework/react/server-functions.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ response?: 'data' | 'full' | 'raw'
9797
- From client-side code
9898
- From other server functions
9999

100-
> [!WARNING]
101-
> Server functions cannot be called from API Routes. If you need to share business logic between server functions and API Routes, extract the shared logic into utility functions that can be imported by both.
102-
103100
## Accepting Parameters
104101

105102
Server functions accept a single parameter, which can be a variety of types:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
ref: docs/start/framework/react/environment-functions.md
3+
replace: { 'react': 'solid' }
4+
---

0 commit comments

Comments
 (0)