Add native env var support: bundle-bound (baked at build) vs backend-only (runtime)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- python
- Lĩnh vực
- build-system, full-stack
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 29k
- Fork
- 1.8k
- Merge trung bình
- 3 ngày 5 giờ
- Pull request đã merge (30 ngày)
- 182
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của reflex-dev/reflex
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
reflex-dev/reflex#8026 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Kept env.py failure: the "move it aside" hint is dropped and a full traceback is logged when alembic.ini uses a relative script_locationCó thể đã có người làm @MaiAditya đã nhận 13 ngày trước. Đang mởbug release-train
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
reflex-dev/reflex#7856 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
0.9.12 changelog follow-ups: the #7132 "_get_was_touched" entry describes unreachable behaviour, #7115's hasattr/getattr change is undocumented, #7131's wordingCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởrelease-train
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
reflex-dev/reflex#7260 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ClientStateVar: a component that mounts while the value is non-default never re-renders on the push back to the defaultCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
reflex-dev/reflex#6823 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation upstream
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
reflex-dev/reflex#6436 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của reflex-dev/reflex
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
superlinked/sie#1033 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
apache/ponymail-foal#365 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
NousResearch/hermes-labs-plugins#116 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
gcivil-nyu-org/team-3-fall26#192 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Study plan] Study sessions can be rescheduled to any length (e.g. 72 hours) with no length checkĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
gcivil-nyu-org/team-2-fall26#159 ·
Maintainer thường phản hồi trong vòng 1 ngày