Commit 1c7f017
docs: 📚 clarify env() works in all contexts including middleware
Fixes confusion about when to use env() vs process.env
## Changes
- Update middleware example to use env() instead of process.env
- Add clear note that env() works in all Next.js contexts
- Improve "Server vs Client Environment Access" section with better clarity
- Update security example to consistently use env()
- Add recommendation to use env() everywhere for consistency
## Rationale
The env() function is designed with proper error handling to work in:
- Server components ✅
- Client components ✅
- API routes ✅
- Middleware ✅ (gracefully falls back when headers() unavailable)
Using env() everywhere provides:
- Consistent API across the application
- Better error messages when accessing private vars in browser
- Type-safe environment variable access
- Future-proof code
Users should not need to think about process.env vs env() - just use
env() everywhere and it will work correctly!
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 2177927 commit 1c7f017
1 file changed
+26
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| 134 | + | |
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
138 | | - | |
139 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
140 | 142 | | |
141 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
142 | 148 | | |
143 | 149 | | |
144 | 150 | | |
| |||
147 | 153 | | |
148 | 154 | | |
149 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
150 | 159 | | |
151 | 160 | | |
152 | 161 | | |
| |||
250 | 259 | | |
251 | 260 | | |
252 | 261 | | |
253 | | - | |
254 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
255 | 265 | | |
256 | 266 | | |
257 | 267 | | |
258 | | - | |
259 | | - | |
| 268 | + | |
| 269 | + | |
260 | 270 | | |
261 | 271 | | |
262 | 272 | | |
| |||
362 | 372 | | |
363 | 373 | | |
364 | 374 | | |
365 | | - | |
366 | | - | |
367 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
368 | 385 | | |
369 | 386 | | |
370 | 387 | | |
| |||
0 commit comments