Commit 60eed0e
committed
feat(server): WEBJS_PUBLIC_* env vars accessible as process.env.* in browser
Adds an SSR-injected inline script that defines window.process.env
with all server-side env vars whose name starts with WEBJS_PUBLIC_,
plus NODE_ENV based on dev/prod mode. Counterpart of Next.js's
NEXT_PUBLIC_ convention, without a build step.
Two consequences:
* App code can write process.env.WEBJS_PUBLIC_API_URL in components
and the value is available at runtime in the browser. No props
threading required for things like Stripe publishable keys,
analytics IDs, Sentry DSN.
* Vendor bundles that probe process.env.NODE_ENV (lit, react, etc.)
no longer throw ReferenceError in the browser. Fixes a latent bug.
Security: only WEBJS_PUBLIC_* prefixed vars cross the wire. Other
server env vars stay on the server. Values are JSON-encoded and
'</' sequences are escaped so a value containing '</script>'
cannot terminate the inline script tag.
The shim emits before importMapTag() so it runs before any vendor
bundle or user module executes. CSP nonces, when present on the
request, propagate to the shim script tag.1 parent ac46664 commit 60eed0e
1 file changed
Lines changed: 40 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
604 | 643 | | |
605 | 644 | | |
606 | 645 | | |
| |||
973 | 1012 | | |
974 | 1013 | | |
975 | 1014 | | |
| 1015 | + | |
976 | 1016 | | |
977 | 1017 | | |
978 | 1018 | | |
| |||
0 commit comments