You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With discussions with @schiller-manuel and the amount of time this issue
has been brought up on Discord, I feel adding precisions and more
explicit examples on how to deal with runtime env variables on the
client could help.
---------
Co-authored-by: Tanner Linsley <[email protected]>
Copy file name to clipboardExpand all lines: docs/router/framework/react/how-to/use-environment-variables.md
+60-7Lines changed: 60 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -514,22 +514,75 @@ module.exports = {
514
514
515
515
**Solutions**:
516
516
517
-
1.**Add correct prefix**: Use `VITE_MY_VARIABLE` for Vite, `PUBLIC_MY_VARIABLE` for Rspack
518
-
2.**Restart dev server**: Environment changes require restart
519
-
3.**Check file location**: `.env` must be in project root
520
-
4.**Verify bundler configuration**: Ensure variables are properly injected
517
+
1.**Add correct prefix**: Use `VITE_` for Vite, `PUBLIC_` for Rspack.
518
+
Vite's default prefix may be changed in the config:
519
+
```ts
520
+
// vite.config.ts
521
+
exportconst config = {
522
+
// ...rest of your config
523
+
envPrefix: 'MYPREFIX_'// this means `MYPREFIX_MY_VARIABLE` is the new correct way
524
+
}
525
+
```
526
+
3.**Restart development server** after adding new variables
527
+
4.**Check file location**: `.env` file must be in project root
528
+
5.**Verify bundler configuration**: Ensure variables are properly injected
529
+
6.**Verify variable**:
530
+
-**In dev**: is in correct `.env` file or environment
531
+
-**For prod**: is in correct `.env` file or current environment ***at bundle time***. That's right, `VITE_`/`PUBLIC_`-prefixed variables are replaced in a macro-like fashion at bundle time, and will *never* be read at runtime on your server. This is a common mistake, so make sure this is not your case.
521
532
522
533
**Example**:
523
534
524
535
```bash
525
536
# ❌ Won't work (no prefix)
526
-
API_URL=https://api.example.com
537
+
API_KEY=abc123
527
538
528
539
# ✅ Works with Vite
529
-
VITE_API_URL=https://api.example.com
540
+
VITE_API_KEY=abc123
530
541
531
542
# ✅ Works with Rspack
532
-
PUBLIC_API_URL=https://api.example.com
543
+
PUBLIC_API_KEY=abc123
544
+
545
+
# ❌ Won't bundle the variable (assuming it is not set in the environment of the build)
546
+
npm run build
547
+
548
+
# ✅ Works with Vite and will bundle the variable for production
549
+
VITE_API_KEY=abc123 npm run build
550
+
551
+
# ✅ Works with Rspack and will bundle the variable for production
552
+
PUBLIC_API_KEY=abc123 npm run build
553
+
```
554
+
555
+
### Runtime Client Environment Variables at Runtime in Production
556
+
557
+
**Problem**: If `VITE_`/`PUBLIC_` variables are replaced at bundle time only, how to make runtime variables available on the client ?
558
+
559
+
**Solutions**:
560
+
561
+
Pass variables from the server down to the client:
562
+
1. Add your variable to the correct `env.` file
563
+
2. Create an endpoint on your server to read the value from the client
564
+
565
+
**Example**:
566
+
567
+
You may use your prefered backend framework/libray, but here it is using Tanstack Start server functions:
-**Forprod**: isincorrect`.env`fileorcurrentenvironment***atbundletime***. That's right, `VITE_`-prefixed variables are replaced in a macro-like fashion at bundle time, and will *never* be read at runtime on your server. This is a common mistake, so make sure this is not your case.
379
390
380
391
**Example**:
381
392
@@ -385,6 +396,45 @@ API_KEY=abc123
385
396
386
397
# ✅ Works in client code
387
398
VITE_API_KEY=abc123
399
+
400
+
# ❌ Won't bundle the variable (assuming it is not set in the environment of the build)
401
+
npm run build
402
+
403
+
# ✅ Works in client code and will bundle the variable for production
0 commit comments