Add native env var support: bundle-bound (baked at build) vs backend-only (runtime)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- python
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 29k
- フォーク
- 1.8k
- 平均マージ
- 3日 5時間
- マージ済み PR(30日)
- 182
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
reflex-dev/reflex のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
reflex-dev/reflex#8026 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Kept env.py failure: the "move it aside" hint is dropped and a full traceback is logged when alembic.ini uses a relative script_location対応中かも @MaiAditya が 13 日前に担当しました。 オープンbug release-train
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
reflex-dev/reflex#7856 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
0.9.12 changelog follow-ups: the #7132 "_get_was_touched" entry describes unreachable behaviour, #7115's hasattr/getattr change is undocumented, #7131's wording再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンrelease-train
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
reflex-dev/reflex#7260 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
ClientStateVar: a component that mounts while the value is non-default never re-renders on the push back to the default再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
reflex-dev/reflex#6823 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation upstream
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
reflex-dev/reflex#6436 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
reflex-dev/reflex の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
superlinked/sie#1033 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
apache/ponymail-foal#365 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
NousResearch/hermes-labs-plugins#116 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
gcivil-nyu-org/team-3-fall26#192 ·
メンテナーはふだん 1 日以内に返信
-
[Study plan] Study sessions can be rescheduled to any length (e.g. 72 hours) with no length checkオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
gcivil-nyu-org/team-2-fall26#159 ·
メンテナーはふだん 1 日以内に返信