Skip to content

Commit e3e6ab4

Browse files
[v6] stabilize experimental.staticImportMetaEnv (#12494)
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent 31685a2 commit e3e6ab4

File tree

5 files changed

+56
-266
lines changed

5 files changed

+56
-266
lines changed

astro.sidebar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ export const sidebar = [
148148
'reference/experimental-flags/client-prerender',
149149
'reference/experimental-flags/content-intellisense',
150150
'reference/experimental-flags/heading-id-compat',
151-
'reference/experimental-flags/static-import-meta-env',
152151
'reference/experimental-flags/chrome-devtools-workspace',
153152
],
154153
}),

src/content/docs/en/guides/upgrade-to/v6.mdx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,21 @@ The following experimental flags have been removed in Astro v6.0 and these featu
380380
Additionally, the following experimental flags have been removed and **are now the default or recommended behavior in Astro v6.0**.
381381

382382
- `experimental.preserveScriptOrder` (See below for breaking changes to [default `<script>` and `<style>` behavior](#changed-script-and-style-tags-are-rendered-in-the-order-they-are-defined).)
383+
- `experimental.staticImportMetaEnv` (See below for breaking changes to [`import.meta.env`](#changed-importmetaenv-values-are-always-inlined).)
383384

384385
The following experimental flags have been removed and **their corresponding features are not part of Astro v6.0**.
385386

386387
- `thing 2`
387388

388389
Remove these experimental flags from your Astro config if you were previously using them:
389390

390-
```js del={5} title="astro.config.mjs"
391+
```js del={5-6} title="astro.config.mjs"
391392
import { defineConfig } from 'astro/config';
392393

393394
export default defineConfig({
394395
experimental: {
395396
preserveScriptOrder: true,
397+
staticImportMetaEnv: true,
396398
},
397399
})
398400
```
@@ -498,6 +500,59 @@ Review your links to your custom endpoints that include a file extension in the
498500

499501
<ReadMore>Learn more about [custom endpoints](/en/guides/endpoints/).</ReadMore>
500502

503+
### Changed: `import.meta.env` values are always inlined
504+
505+
<SourcePR number="14485" title="feat: stabilize static import meta env"/>
506+
507+
In Astro 5.13, the `experimental.staticImportMetaEnv` was introduced to update 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.
508+
509+
In Astro 5.x, non-public environment variables were replaced by a reference to `process.env`. Additionally, Astro could also convert the value type of your environment variables used through `import.meta.env`, which could prevent access to some values such as the strings `"true"` (which was converted to a boolean value), and `"1"` (which was converted to a number).
510+
511+
Astro 6 removes this experimental flag and makes this the new default behavior in Astro: `import.meta.env` values are always inlined and never coerced.
512+
513+
#### What should I do?
514+
515+
If you were previously using this experimental feature, you must [remove this experimental flag from your configuration](#experimental-flags) as it no longer exists.
516+
517+
If you were relying on coercion, you may need to update your project code to apply it manually:
518+
519+
```ts title="src/components/MyComponent.astro" del={1} ins={2}
520+
const enabled: boolean = import.meta.env.ENABLED;
521+
const enabled: boolean = import.meta.env.ENABLED === "true";
522+
```
523+
524+
If you were relying on the transformation into `process.env`, you may need to update your project code to apply it manually:
525+
526+
```ts title="src/components/MyComponent.astro" del={1} ins={2}
527+
const enabled: boolean = import.meta.env.DB_PASSWORD;
528+
const enabled: boolean = process.env.DB_PASSWORD;
529+
```
530+
531+
You may also need to update types:
532+
533+
```ts title="src/env.d.ts" del={3-4} ins={5,12-16}
534+
interface ImportMetaEnv {
535+
readonly PUBLIC_POKEAPI: string;
536+
readonly DB_PASSWORD: string;
537+
readonly ENABLED: boolean;
538+
readonly ENABLED: string;
539+
}
540+
541+
interface ImportMeta {
542+
readonly env: ImportMetaEnv;
543+
}
544+
545+
namespace NodeJS {
546+
interface ProcessEnv {
547+
DB_PASSWORD: string;
548+
}
549+
}
550+
```
551+
552+
If you need more control over environment variables in Astro, we recommend you use `astro:env`.
553+
554+
<ReadMore>Learn more about [environment variables](/en/guides/environment-variables/) in Astro, including `astro:env`.</ReadMore>
555+
501556
## Community Resources
502557

503558
Know a good resource for Astro v5.0? [Edit this page](https://github.com/withastro/docs/edit/main/src/content/docs/en/guides/upgrade-to/v6.mdx) and add a link below!

src/content/docs/en/reference/experimental-flags/static-import-meta-env.mdx

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

src/content/docs/fr/reference/experimental-flags/static-import-meta-env.mdx

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

src/content/docs/ko/reference/experimental-flags/static-import-meta-env.mdx

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

0 commit comments

Comments
 (0)