Skip to content

🍕 Hydrate process.env before Vite universal modules evaluate#246

Merged
jjpaulino merged 1 commit into
masterfrom
jordan/fix-vite-env-hydration-order
Apr 30, 2026
Merged

🍕 Hydrate process.env before Vite universal modules evaluate#246
jjpaulino merged 1 commit into
masterfrom
jordan/fix-vite-env-hydration-order

Conversation

@jjpaulino
Copy link
Copy Markdown
Member

Summary

  • Add a generated runtime module: .clay/_env-init.js.
  • Import it first in both generated Vite entries:
    • .clay/vite-bootstrap.js (view mode)
    • .clay/vite-kiln-edit-init.js (edit mode)
  • Runtime hydration source priority:
    1. window.kiln.preloadData._envVars (when present)
    2. existing window.process.env

Why

Some universal modules cache env values at top-level (e.g. const AGORA_HOST = process.env.AGORA_HOST). Under ESM/Vite, module evaluation timing can happen before late env injection, causing those constants to permanently capture undefined/null even though values appear later on window.process.env.

By importing _env-init.js first, window.process.env is hydrated before component/model modules execute.

Scope

  • claycli-only change (no site code edits required)
  • keeps existing client-env collection and amphora-html runtime injection behavior

Test plan

  • npm run lint
  • npm test (403 passing)

Made with Cursor

Generate a shared .clay/_env-init.js runtime module and import it first
from both Vite entries (view bootstrap and kiln-edit init). This
hydrates window.process.env from the earliest available runtime source
(window.kiln.preloadData._envVars when present, otherwise existing
window.process.env) before component/model modules execute.

Fixes timing-sensitive cases where top-level universal constants like
`const AGORA_HOST = process.env.AGORA_HOST` were evaluated before env
injection and permanently captured undefined/null values.

Made-with: Cursor
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 63.968% (-0.09%) from 64.054% — jordan/fix-vite-env-hydration-order into master

@jjpaulino jjpaulino self-assigned this Apr 29, 2026
@jjpaulino jjpaulino merged commit 966fc16 into master Apr 30, 2026
6 checks passed
@jjpaulino jjpaulino deleted the jordan/fix-vite-env-hydration-order branch May 1, 2026 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants