diff --git a/docs/src/app/docs/customization/page.mdx b/docs/src/app/docs/customization/page.mdx index 7dfac5e1..3ef04187 100644 --- a/docs/src/app/docs/customization/page.mdx +++ b/docs/src/app/docs/customization/page.mdx @@ -62,6 +62,8 @@ export const env = createEnv({ }); ``` +If you want to reuse above `isServer` check in multiple places, you can use the [`shared` section](./recipes#isserver). + ## Treat empty strings as undefined By default, T3 Env will feed the environment variables directly to the Zod validator. This means that if you have an empty string for a value that is supposed to be a number (e.g. `PORT=` in a ".env" file), Zod will flag diff --git a/docs/src/app/docs/recipes/page.mdx b/docs/src/app/docs/recipes/page.mdx index 1e23f95d..a2abd1ff 100644 --- a/docs/src/app/docs/recipes/page.mdx +++ b/docs/src/app/docs/recipes/page.mdx @@ -82,3 +82,22 @@ const config: StorybookConfig = { export default config; ``` + +## isServer + +Determining whether the code is running on the server or client is a common requirement. +You can use the `shared` section to define an environment variable that's accessible in both contexts: + +```ts +export const env = createEnv({ + shared: { + IS_SERVER: z.boolean().default(false), + }, + runtimeEnv: { + IS_SERVER: typeof window === "undefined", + } +}) +``` + +Make sure to read the [customization](./customization#tell-when-we're-in-a-server-context) docs to learn how to override the `isServer` value used by the library itself. + \ No newline at end of file