Skip to content

Commit

Permalink
docs(recipe: expose isServer value to reuse across app (t3-oss#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Aug 13, 2024
1 parent b55ca31 commit ed08b87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/src/app/docs/customization/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions docs/src/app/docs/recipes/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit ed08b87

Please sign in to comment.