Add native env var support: bundle-bound (baked at build) vs backend-only (runtime)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Tranquilla
- Stack tecnologico
- python
- Ambito
- build-system, full-stack
Direzione di ricerca
Start with the minimal repro in config.py and pages/some_page.py, then run reflex export and inspect the generated .web/build/client/*.js bundle. Define how FrontendBundleEnv and BackendEnv behave during export, including cached accessors and failure timing; done means bundle-safe values are embedded while backend-only reads fail loudly without requiring secret placeholders.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
Reflex compiles Python page code into a static JS bundle at reflex export. Any os.environ read (or pydantic_settings.BaseSettings() instantiation) that happens during page evaluation gets its return value baked into the static bundle — the value the env had at build time becomes a string literal in .web/build/client/*.js.
This is structurally inherent to the architecture: the same Python module runs at both build (export) and runtime (server), and there's nothing in the framework that distinguishes "values safe to embed in the browser bundle" from "values that must stay backend-only."
Minimal repro
# config.py
class Settings(BaseSettings):
public_api_url: str
db_password: str
# pages/some_page.py
def _endpoint_card() -> rx.Component:
return rx.code(_api_url()) # called during page evaluation
def _api_url() -> str:
return get_settings().public_api_url
get_settings() reads env vars during page evaluation. In a typical build pipeline, the real per-deployment values aren't set at build time (they come from runtime container env or secret manager), so a placeholder/dummy gets baked into the bundle. The wrong value silently ships to every browser, with no warning, no compile error, no test failure.
Worse, the same Settings() call validates all fields — so a build that has no business reading db_password is forced to provide it just to instantiate the class. Teams end up sprinkling dummy db_password=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx values into Dockerfiles, which is a code smell pointing at exactly this design gap.
Why it's a class of bug, not a one-off
- Any third-party library that reads env at import time has the same exposure.
- Any helper that lazily reads config and gets called during page evaluation has the same exposure.
- Refactors that move a backend-only call into a render path silently introduce leaks.
- Adding new env vars is a judgment call ("will this end up in the bundle?") that's invisible to the type system.
The mental model "I'm just reading an env var" doesn't match the reality "this might end up in a CDN-cached JS file forever."
Proposed solution
Make the bundle/backend split a first-class concept, the way Next.js, Nuxt, and SvelteKit do — two pydantic-style base classes provided by Reflex:
rx.FrontendBundleEnv— values are baked into the JS bundle atreflex export. Browser-visible. Safe to read anywhere.rx.BackendEnv— values stay backend-only. Instantiation raises duringreflex export, so accidental reads at page-evaluation time fail loud with a useful traceback instead of silently leaking into the static bundle.
The choice between "frontend bundle" and "backend" is a deployment-level decision that doesn't belong in scattered helper logic — it belongs on the env var itself. Putting it in the type lets every reader see, at the call site, which side of the split they're on.
A concrete implementation sketch (~30 lines, no new dependencies, including .get() cached-singleton accessors and the optional bundle-scan defense in depth) is in a follow-up comment below.
Prior art
- Next.js:
NEXT_PUBLIC_*prefix → bundled; everything else → server-only. Compile-time check. - Nuxt:
runtimeConfig.publicvsruntimeConfig. - SvelteKit:
$env/static/public/$env/static/private/$env/dynamic/public/$env/dynamic/private— the import path itself encodes the classification.
All three solve the same structural problem the same way: make "is this safe to ship to the browser?" a property of the variable, not a judgment call at every read site.
Workaround until upstream support exists
The same shape can be built manually today:
- Two
BaseSettingssubclasses (e.g.FrontendBundleEnv,BackendEnv) - A
BUILDING_FRONTEND_BUNDLE=1env set in the Dockerfile for thereflex exportstep get_backend_env()raises if that flag is set- View code reads
get_frontend_bundle_env()directly at module load (no state var) since values are constant per-bundle
It works, but every Reflex user with a real deployment will hit this same shape eventually. Built-in support would catch the bug at framework level rather than relying on each team to reinvent it.
- Lingua principale
- Python
- Stelle
- 29k
- Fork
- 1.8k
- Merge medio
- 3g 5h
- PR unite (30g)
- 182
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di reflex-dev/reflex
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
reflex-dev/reflex#8026 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Kept env.py failure: the "move it aside" hint is dropped and a full traceback is logged when alembic.ini uses a relative script_locationForse già presa @MaiAditya l’ha presa 13 giorni fa. Apertabug release-train
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
reflex-dev/reflex#7856 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
0.9.12 changelog follow-ups: the #7132 "_get_was_touched" entry describes unreachable behaviour, #7115's hasattr/getattr change is undocumented, #7131's wordingForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertarelease-train
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
reflex-dev/reflex#7260 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
ClientStateVar: a component that mounts while the value is non-default never re-renders on the push back to the defaultForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
reflex-dev/reflex#6823 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation upstream
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
reflex-dev/reflex#6436 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di reflex-dev/reflex
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
superlinked/sie#1033 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
apache/ponymail-foal#365 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
NousResearch/hermes-labs-plugins#116 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
gcivil-nyu-org/team-3-fall26#192 ·
I maintainer di solito rispondono entro 1 giorno
-
[Study plan] Study sessions can be rescheduled to any length (e.g. 72 hours) with no length checkAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
gcivil-nyu-org/team-2-fall26#159 ·
I maintainer di solito rispondono entro 1 giorno