Repository navigation
docs: add a how-to on environment variables in React views - #6709
Conversation
React views read environment variables through Vite, but no page explained how this works in a Vaadin project. Vaadin makes the frontend folder the root of Vite, so a .env file next to pom.xml is ignored, and import.meta.env fails the type check of the production build until the Vite client types are added. The new page covers where the .env files go, the VITE_ prefix, variables set in the build environment, the vite-env.d.ts declaration, the mode-specific files, and the envDir option for keeping the files in the project root. It also explains that the values are fixed at build time and visible in the browser, and points to Spring configuration and browser-callable services for secrets and per-deployment values. Fixes #1546 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Preview DeploymentThis PR has been deployed for preview. URL: https://docs-preview-pr-6709.fly.dev Changed pagesAdded content is highlighted in green; removed content is marked in red on each page.
Built from f7a9ec6 |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 1 comment.
| Finding | |
|---|---|
| Pointing secrets at application.properties contradicts two existing pages |
The section on values that differ between deployments suggested application.properties for secrets too, which contradicts the guidance to keep secrets out of the project files. Link to the Configure the Application guide, which covers passing secrets and per-environment settings from outside the build, instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Hi @peholmst and @peholmst, when i performed cherry-pick to this commit to v25.3, i have encountered the following issue. Can you take a look and pick it manually? |
Summary
React views read environment variables through Vite, but no page explained how that works in a Vaadin project. Two things trip people up, as in the issue:
src/main/frontend) the root of Vite, and Vite reads.envfiles from its root. A.envfile next topom.xmlis silently ignored.import.meta.envisn't typed in a Vaadin project, so the production build fails withProperty 'env' does not exist on type 'ImportMeta'until the Vite client types are added.What changed
New how-to page
building-apps/react/environment-variables.adoc(Use Environment Variables), listed in the React Views section overview. It covers:.envfiles go, and theVITE_prefixvite-env.d.tsdeclaration that makesimport.meta.envtype-check.env.developmentand.env.productionenvDiroption invite.config.tsfor keeping the files in the project rootVerification
Every statement was checked in a test application with a React view on 25.4.0-beta1, in both a production build (inspecting the generated bundle) and development mode (in the browser):
.envis ignored, the one insrc/main/frontendis read, and unprefixed variables are left out.env, and shell variables take precedence over bothvite-env.d.tsand passes with itenvDir: import.meta.dirnamemakes Vite read the root.env, and Vaadin keeps the customizedvite.config.ts.envfile changes.envalways reaches the bundleThe
.envlocation, the type error, the declaration fix andenvDirbehave the same on 25.3.5, hence thetarget/v25.3label. Vale reports nothing with.vale-pr.ini.Fixes #1546
🤖 Generated with Claude Code