diff --git a/docs/start/config.json b/docs/start/config.json index 02a44b30ac..e95d0257d7 100644 --- a/docs/start/config.json +++ b/docs/start/config.json @@ -41,6 +41,10 @@ "label": "Static Server Functions", "to": "framework/react/static-server-functions" }, + { + "label": "Environment Functions", + "to": "framework/react/environment-functions" + }, { "label": "Middleware", "to": "framework/react/middleware" @@ -122,6 +126,10 @@ "label": "Static Server Functions", "to": "framework/solid/static-server-functions" }, + { + "label": "Environment Functions", + "to": "framework/solid/environment-functions" + }, { "label": "Middleware", "to": "framework/solid/middleware" diff --git a/docs/start/framework/react/environment-functions.md b/docs/start/framework/react/environment-functions.md new file mode 100644 index 0000000000..39d51890d6 --- /dev/null +++ b/docs/start/framework/react/environment-functions.md @@ -0,0 +1,124 @@ +--- +id: environment-functions +title: Environment Functions +--- + +## What Are Environment Functions? + +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: + +- `createIsomorphicFn`: Compose a single function that adapts to both client and server environments. +- `serverOnly`: Ensures a function can only run on the server. +- `clientOnly`: Ensures a function can only run on the client. + +--- + +## Isomorphic 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. + +### Complete Implementation + +```tsx +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'`. +``` + +### Partial Implementation (Server) + +Here is an example of `createIsomorphicFn()` with only server implementation: + +```tsx +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`) +``` + +### Partial Implementation (Client) + +Here is an example of `createIsomorphicFn()` with only client implementation: + +```tsx +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'`. +``` + +### No Implementation + +Here is an example of `createIsomorphicFn()` without any environment specific implementation: + +```tsx +import { createIsomorphicFn } from '@tanstack/react-start'; + +const noImplementation = createIsomorphicFn() + +const noop = noImplementation() +// ℹ️ On both **client** and **server**, it is no-op (returns `undefined`) +``` + +#### What is a no-op? + +A no-op (short for "no operation") is a function that does nothing when executed - it simply returns `undefined` without performing any operations. + +```tsx +// basic no-op implementation +function noop() {} +``` + +--- + +## `env`Only Functions + +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. + +### `serverOnly` + +```tsx +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!" +``` + +### `clientOnly` + +```tsx +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. + +## Tree Shaking + +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. + diff --git a/docs/start/framework/react/server-functions.md b/docs/start/framework/react/server-functions.md index 2c6a075b35..1c129d12dc 100644 --- a/docs/start/framework/react/server-functions.md +++ b/docs/start/framework/react/server-functions.md @@ -97,9 +97,6 @@ response?: 'data' | 'full' | 'raw' - From client-side code - From other server functions -> [!WARNING] -> 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. - ## Accepting Parameters Server functions accept a single parameter, which can be a variety of types: diff --git a/docs/start/framework/solid/environment-functions.md b/docs/start/framework/solid/environment-functions.md new file mode 100644 index 0000000000..63a0d9ad22 --- /dev/null +++ b/docs/start/framework/solid/environment-functions.md @@ -0,0 +1,4 @@ +--- +ref: docs/start/framework/react/environment-functions.md +replace: { 'react': 'solid' } +---