Skip to content

Conversation

@max-programming
Copy link
Contributor

@max-programming max-programming commented Oct 22, 2025

This PR changes the example provided in the docs for environment variables validation with zod

The current example in the docs is error-prone because it validates both client and server variables in the same file and when imported in any client-side code, the server side validation fails because process.env does not exist on client-side

So I make the following changes

  1. Make use of the createIsomorphicFn to validate the variables in their respective environments
  2. Intersect client side variables with server schema using zod's .and method because client-side variables are available in the server environment
  3. Export a singular env object and typecast it to the server env schema type so that all variables are detected by TypeScript, just that in runtime if a server variable is used on client side, it'll fail. TypeScript cannot know which file is client-side code hence the typecast

Summary by CodeRabbit

  • New Features

    • Unified isomorphic environment handling: single, consistent env access across server and client via a new isomorphic getter.
  • Documentation

    • Updated environment variable guides for React and Solid to reflect the unified approach and new usage.

@github-actions github-actions bot added the documentation Everything documentation related label Oct 22, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 22, 2025

Walkthrough

Replaces separate server and client environment parsing/exports with a single isomorphic env export using a combined serverEnvSchema and clientEnvSchema via createIsomorphicFn/getEnv; removes serverEnv and clientEnv, adds env in both React and Solid docs/config examples.

Changes

Cohort / File(s) Summary
React docs & env config
docs/start/framework/react/guide/environment-variables.md, src/.../react/src/config/env.ts
Replace separate serverEnv/clientEnv with a unified isomorphic env using createIsomorphicFn + combined serverEnvSchemaclientEnvSchema; add getEnv() and export env. Remove previous serverEnv and clientEnv exports.
Solid docs & env config
docs/start/framework/solid/guide/environment-variables.md, src/.../solid/src/config/env.ts
Same change as React: introduce createIsomorphicFn/getEnv(), combine server/client schemas, export single env (typed via combined schema), remove serverEnv and clientEnv.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client (browser)
    participant Server as Server (node)
    participant EnvFn as getEnv()

    Note over EnvFn: Combined schema\n(serverEnvSchema ∧ clientEnvSchema)
    Client->>EnvFn: call getEnv() -> client branch
    EnvFn-->>Client: parse import.meta.env via clientEnvSchema\nreturn Env (client view)
    Server->>EnvFn: call getEnv() -> server branch
    EnvFn-->>Server: parse process.env via serverEnvSchema\nreturn Env (server view)
    Note right of EnvFn: Single exported `env` value\nbacked by isomorphic getter
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • schiller-manuel
  • birkskyum

Poem

🐰 I hopped through code and fields of text,

Unified envs — no more contexts vexed.
One getter now fetches both near and far,
A single export — neat as a star. ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "docs: fixed example of env variable validation with zod" accurately describes the main change in the changeset. The PR modifies documentation examples for React and Solid frameworks to demonstrate the correct pattern for environment variable validation with zod using isomorphic functions. The title clearly indicates this is a documentation update ("docs:") that fixes a specific aspect (environment variable validation), and it's specific enough that a developer scanning history would understand the primary change. The title is concise, descriptive, and directly related to the changes made.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2142ece and de0aabe.

📒 Files selected for processing (1)
  • docs/start/framework/solid/guide/environment-variables.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
docs/**/*.{md,mdx}

📄 CodeRabbit inference engine (AGENTS.md)

Use internal docs links relative to the docs/ folder (e.g., ./guide/data-loading)

Files:

  • docs/start/framework/solid/guide/environment-variables.md
docs/{router,start}/**

📄 CodeRabbit inference engine (AGENTS.md)

Place router docs under docs/router/ and start framework docs under docs/start/

Files:

  • docs/start/framework/solid/guide/environment-variables.md
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Preview
🔇 Additional comments (1)
docs/start/framework/solid/guide/environment-variables.md (1)

280-306: Isomorphic environment validation implementation is sound.

The runtime validation pattern correctly:

  1. Uses createIsomorphicFn to execute different logic on server vs client
  2. Combines serverEnvSchema and clientEnvSchema via .and() so TypeScript recognizes all variables
  3. Parses process.env on the server and import.meta.env on the client
  4. Exports a single env object with the full server schema type

The cast as Env on Line 305 is appropriate—TypeScript cannot determine the runtime environment, so the cast ensures type safety at the usage site while accepting that misuse (accessing server-only variables on client) will fail at runtime.

The z.url() validation on Line 286 for VITE_API_URL is a beneficial type-safety improvement over z.string().

This implementation aligns with the PR objectives and securely separates environment contexts.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Oct 22, 2025

View your CI Pipeline Execution ↗ for commit de0aabe

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-22 15:38:31 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 22, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5584

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5584

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5584

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5584

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5584

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5584

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5584

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5584

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5584

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5584

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5584

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5584

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5584

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5584

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5584

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5584

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5584

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5584

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5584

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5584

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5584

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5584

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5584

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@5584

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5584

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5584

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5584

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5584

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5584

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5584

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5584

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5584

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5584

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5584

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5584

commit: de0aabe

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eab8a99 and 2142ece.

📒 Files selected for processing (2)
  • docs/start/framework/react/guide/environment-variables.md (1 hunks)
  • docs/start/framework/solid/guide/environment-variables.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
docs/**/*.{md,mdx}

📄 CodeRabbit inference engine (AGENTS.md)

Use internal docs links relative to the docs/ folder (e.g., ./guide/data-loading)

Files:

  • docs/start/framework/solid/guide/environment-variables.md
  • docs/start/framework/react/guide/environment-variables.md
docs/{router,start}/**

📄 CodeRabbit inference engine (AGENTS.md)

Place router docs under docs/router/ and start framework docs under docs/start/

Files:

  • docs/start/framework/solid/guide/environment-variables.md
  • docs/start/framework/react/guide/environment-variables.md
🔇 Additional comments (3)
docs/start/framework/solid/guide/environment-variables.md (1)

284-306: Excellent refactoring of environment variable validation pattern.

The updated example correctly demonstrates:

  • Combining server and client schemas with .and() for unified type safety across environments
  • Using createIsomorphicFn() to provide environment-aware parsing (server via process.env, client via import.meta.env)
  • Exporting a single env object typed to the server schema, allowing TypeScript to surface all variables while maintaining runtime safety

This addresses the original issue where importing server-side validation on the client would fail due to missing process.env.

docs/start/framework/react/guide/environment-variables.md (2)

281-305: Implementation correctly addresses the PR objectives: isomorphic environment validation with unified type exports.

The refactored approach properly separates server and client validation contexts while maintaining a single export. The type casting to serverEnvSchema enables TypeScript to surface all variables, establishing a clear convention: TypeScript catches what it can, and runtime errors reveal actual access violations on the client.

However, ensure documentation or code comments clarify the intentional type/runtime safety tradeoff. Developers accessing env.DATABASE_URL on the client will see no TypeScript error but encounter undefined at runtime. Consider adding an inline comment at line 305 explaining this design choice, or document it in a dedicated section.

Does the codebase have existing patterns or conventions for documenting this kind of deliberate type/runtime mismatch elsewhere?


473-477: Verify related resources links follow the coding guidelines format.

The current links use ../ relative paths, but the coding guidelines specify: "Use internal docs links relative to the docs/ folder (e.g., ./guide/data-loading)". Confirm whether the current format is compliant or should be converted to the ./ notation recommended in the guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Everything documentation related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant