Skip to content

Commit a992ec1

Browse files
florian-lefebvreArmandPhilippotsarah11918yanthomasdevlouisescher
authored
feat: experimental static import.meta.env (#12105)
Co-authored-by: Armand Philippot <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Yan <[email protected]> Co-authored-by: louisescher <[email protected]>
1 parent 222226f commit a992ec1

File tree

5 files changed

+89
-151
lines changed

5 files changed

+89
-151
lines changed

astro.sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export const sidebar = [
149149
'reference/experimental-flags/content-intellisense',
150150
'reference/experimental-flags/preserve-scripts-order',
151151
'reference/experimental-flags/heading-id-compat',
152-
'reference/experimental-flags/raw-env-values',
152+
'reference/experimental-flags/static-import-meta-env',
153153
'reference/experimental-flags/chrome-devtools-workspace',
154154
],
155155
}),

src/content/docs/en/reference/experimental-flags/raw-env-values.mdx

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: Experimental private meta environment variables inlining
3+
sidebar:
4+
label: Private meta environment variables inlining
5+
i18nReady: true
6+
---
7+
8+
import Since from '~/components/Since.astro'
9+
10+
<p>
11+
12+
**Type:** `boolean`<br />
13+
**Default:** `false`<br />
14+
<Since v="5.13.0" />
15+
</p>
16+
17+
:::tip[Astro 6.0 preview]
18+
The behavior enabled by this feature will become the default behavior in Astro 6.0.
19+
20+
You may wish to add this flag whenever it is convenient to do so. You can start enjoying the benefits sooner and will avoid the need to update your project code for the next major Astro version.
21+
:::
22+
23+
Astro allows you to configure a [type-safe schema for your environment variables](/en/guides/environment-variables/#type-safe-environment-variables), and converts variables imported via `astro:env` into the expected type. This is the recommended way to use environment variables in Astro, as it allows you to easily see and manage whether your variables are public or secret, available on the client or only on the server at build time, and the data type of your values.
24+
25+
However, you can still access your environment variables through `process.env` as well as `import.meta.env` directly if needed. This was the only way to use environment variables in Astro before `astro:env` was added in Astro 5.0, and its handling of `import.meta.env` includes some logic that was intended for earlier versions of Astro that is no longer necessary.
26+
27+
The `experimental.staticImportMetaEnv` flag updates the behavior when accessing `import.meta.env` directly to align with [Vite's handling of environment variables](https://vite.dev/guide/env-and-mode.html#env-variables) and ensures that `import.meta.env` values are always inlined.
28+
29+
Currently, non-public environment variables are replaced by a reference to `process.env`. Additionally, Astro may also convert the value type of your environment variables used through `import.meta.env`, which can prevent access to some values such as the strings `"true"` (which is converted to a boolean value), and `"1"` (which is converted to a number).
30+
31+
The `experimental.staticImportMetaEnv` flag simplifies Astro's default behavior, making it easier to understand and use. Astro will no longer replace any `import.meta.env` environment variables with a `process.env` call, nor will it coerce values.
32+
33+
To enable this feature, add the experimental flag in your Astro config:
34+
35+
```js title="astro.config.mjs" ins={4-6}
36+
import { defineConfig } from "astro/config"
37+
38+
export default defineConfig({
39+
experimental: {
40+
staticImportMetaEnv: true,
41+
}
42+
})
43+
```
44+
45+
## Usage
46+
47+
Enabling this experimental flag will no longer convert string values into booleans or numbers, nor turn `import.meta.env` values into `process.env` calls. This aligns `import.meta.env`'s behavior in Astro with [Vite](https://vite.dev/guide/env-and-mode.html#env-variables).
48+
49+
In a future major version, Astro will switch to this behavior by default, but you can opt in to the future behavior early using the `experimental.staticImportMetaEnv` flag and, if necessary, [updating your project](#updating-your-project) accordingly.
50+
51+
### Updating your project
52+
53+
If you were relying on coercion, you may need to update your project code to apply it manually:
54+
55+
```ts title="src/components/MyComponent.astro" del={1} ins={2}
56+
const enabled: boolean = import.meta.env.ENABLED;
57+
const enabled: boolean = import.meta.env.ENABLED === "true";
58+
```
59+
60+
If you were relying on the transformation into `process.env`, you may need to update your project code to apply it manually:
61+
62+
```ts title="src/components/MyComponent.astro" del={1} ins={2}
63+
const enabled: boolean = import.meta.env.DB_PASSWORD;
64+
const enabled: boolean = process.env.DB_PASSWORD;
65+
```
66+
67+
You may also need to update types:
68+
69+
```ts title="src/env.d.ts" del={3-4} ins={5,12-16}
70+
interface ImportMetaEnv {
71+
readonly PUBLIC_POKEAPI: string;
72+
readonly DB_PASSWORD: string;
73+
readonly ENABLED: boolean;
74+
readonly ENABLED: string;
75+
}
76+
77+
interface ImportMeta {
78+
readonly env: ImportMetaEnv;
79+
}
80+
81+
namespace NodeJS {
82+
interface ProcessEnv {
83+
DB_PASSWORD: string;
84+
}
85+
}
86+
```
87+
88+
If you need more control over environment variables in Astro, we recommend you use [`astro:env`](/en/guides/environment-variables/).

src/content/docs/fr/reference/experimental-flags/raw-env-values.mdx

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/content/docs/ko/reference/experimental-flags/raw-env-values.mdx

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)