Skip to content

Commit dd59a60

Browse files
committed
docs: polish /env
1 parent 5ed2ca3 commit dd59a60

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

docs/pages/env/+Page.mdx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ DATABASE_URL=postgresql://database.example.com:5432
4343

4444
## Access
4545

46-
Use <ImportMeta prop="env"/> to access environment variables.
46+
Use <ImportMeta prop="env.SOME_ENV"/> to access environment variables.
4747

4848
```ts
4949
// /pages/movies/+data.ts
50+
// Environment: server
5051

5152
import type { PageContextServer } from 'vike/types'
5253

@@ -62,14 +63,13 @@ export async function data(pageContext: PageContextServer) {
6263
}
6364
```
6465

65-
> For server only code, you can access environment variables using [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs), but we generally recommend against this because it won't be <Link href="#static-replacement">statically replaced</Link>.
66+
`import.meta.env.PUBLIC_ENV__*` variables are <Link href="#define">also available on the client-side</Link>:
6667

6768
```jsx
6869
// components/ContactUs.jsx
70+
// Environment: client
6971

70-
// The UI component <ContactUs> is loaded on the client-side
71-
72-
// This environment variable is available on the client-side: it's prefixed with PUBLIC_ENV__
72+
// This environment variable is available on the client because it's prefixed with PUBLIC_ENV__
7373
const email = import.meta.env.PUBLIC_ENV__CONTACT_EMAIL
7474

7575
function ContactUs() {
@@ -81,12 +81,22 @@ function ContactUs() {
8181
> ```ts
8282
> // ✅ Works
8383
> import.meta.env.SOME_ENV
84-
> //Won't work
84+
> //Doesn't work
8585
> import.meta.env['SOME_ENV']
86-
> //Won't work
86+
> //Doesn't work
8787
> const { SOME_ENV } = import.meta.env
8888
> ```
8989
90+
> For server-only code, you can access environment variables using [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs), but we generally recommend `import.meta.env.SOME_ENV` instead because <Link href="#static-replacement">it's statically replaced for minimal bundle sizes</Link>.
91+
> ```ts
92+
> // ✅ Works, eveywhere and statically replaced (minimizing bundle sizes)
93+
> import.meta.env.SOME_ENV
94+
> // ⚠️ Works, but only on server-side and no static replacement
95+
> process.env.SOME_ENV
96+
> // ⚠️ Works, but only on server-side and no static replacement
97+
> const { SOME_ENV } = process.env
98+
> ```
99+
90100
91101
## TypeScript
92102
@@ -99,7 +109,9 @@ For improved DX, consider using [`zod`](https://github.com/colinhacks/zod), for
99109
100110
## Static replacement
101111
102-
For better [tree shaking](https://rollupjs.org/introduction/#tree-shaking) (aka dead code elimination), `import.meta.env.SOME_ENV` is statically replaced with its value. For example:
112+
`import.meta.env.SOME_ENV` is statically replaced with its value at build time, enabling [tree shaking](https://rollupjs.org/introduction/#tree-shaking) (dead code elimination).
113+
114+
For example:
103115
104116
```js
105117
// src/someFile.js [source code in your Git repository]
@@ -119,7 +131,7 @@ if (import.meta.env.DISABLE_TRACKING === 'true') {
119131
DISABLE_TRACKING='true'
120132
```
121133

122-
The file `src/someFile.js` is built to:
134+
After build, `src/someFile.js` becomes:
123135

124136
```js
125137
// dist/server/chunks/chunk-DdwTql6x.js [built code deployed to production]
@@ -128,14 +140,14 @@ console.log("value:", "true");
128140
console.log("Tracking is disabled");
129141
```
130142

131-
Note how `import()` is completely removed, resulting in a more lightweight production bundle.
143+
The `import()` call is completely removed, resulting in a more lightweight production bundle.
132144

133145

134146
## Config files
135147

136148
In config files, [<ImportMeta prop="env"/> isn't available](https://github.com/vikejs/vike/issues/1726#issuecomment-2208626928) (neither `vite.config.js` nor `+config.js`).
137149

138-
Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead of <ImportMeta prop="env"/>.
150+
Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead.
139151

140152

141153
## Public allowlist

0 commit comments

Comments
 (0)