-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: explicit environment variables #15934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
031fda6
first draft
Rich-Harris 455baaa
evaluate env file
Rich-Harris 4a1e2bf
tweak
Rich-Harris ff05b96
warn if using src/env without the experimental flag
Rich-Harris 3c75dcf
regenerate
Rich-Harris f46f724
adjust test
Rich-Harris 70cb5b4
fix
Rich-Harris d4a527e
fix
Rich-Harris f0ec4ad
fix
Rich-Harris 415a4e4
fix
Rich-Harris f108206
unused
Rich-Harris cc75201
unused
Rich-Harris d2b2eb0
revert
Rich-Harris 36b62ff
revert
Rich-Harris 4dc66a5
fix
Rich-Harris 069ec47
fixes
Rich-Harris 5b8a007
tweak
Rich-Harris c93fb59
fix
Rich-Harris 6bfa006
add a TODO
Rich-Harris ae092dc
fix
Rich-Harris f902326
temp
Rich-Harris fecf3ce
revert
Rich-Harris 84e0110
revert
Rich-Harris 0956317
add TODO
Rich-Harris d4a590c
fix
Rich-Harris f1e28f3
fix
Rich-Harris e979fab
fix
Rich-Harris 74ea8da
unused
Rich-Harris 4b39d5e
use dev server to load src/env
Rich-Harris 3ba0181
simplify
Rich-Harris 7731ae3
more
Rich-Harris 2b25c57
generics
Rich-Harris 0c5b63b
fix
Rich-Harris 22f3d99
changeset
Rich-Harris 136dd9b
lol we dont need this any more
Rich-Harris 5d48d5a
oops
Rich-Harris 84d5f38
prettier
Rich-Harris 2b12cac
fix
Rich-Harris d5ced50
prettier
Rich-Harris cec84e1
remove default property, we decided against that
Rich-Harris 57a4614
fix
Rich-Harris 30e4211
make client-side dead-code-elimination work
Rich-Harris 16438f9
omit static vars from rendered env
Rich-Harris f3306bd
update docs
Rich-Harris 34f946c
fix
Rich-Harris db79793
fix generateEnvModule
Rich-Harris de517ff
lint
Rich-Harris 6a7a0eb
testing a hypothesis
Rich-Harris 0a12ee1
gah
Rich-Harris 0c9203e
what the actual hell
Rich-Harris 01ae2cc
please continue to work
Rich-Harris e5dbb29
docs
Rich-Harris 1f78e97
fix
Rich-Harris 3087c4c
tweak
Rich-Harris b0b6008
more tweakage
Rich-Harris 8f6133a
oops revert
Rich-Harris e6cb05d
fix
Rich-Harris dd68c21
Update packages/kit/src/exports/internal/env.js
teemingc 2c3eab3
Update documentation/docs/10-getting-started/30-project-structure.md
dummdidumm 440cf48
don't create /... modules if explicit env vars are enabled
Rich-Harris 6c1deb5
add note
Rich-Harris 110d1f2
validate everything at once
Rich-Harris de5fe51
Update packages/kit/src/exports/internal/env.js
Rich-Harris 15dd8f5
fix
Rich-Harris 54d7b95
Merge branch 'explicit-env-vars' of github.com:sveltejs/kit into expl…
Rich-Harris eb8bb9f
hide stack trace
Rich-Harris e77c40a
make server-side treeshaking work
Rich-Harris 7adef36
looks like we can revert this
Rich-Harris 70dd946
validate -> schema
Rich-Harris 1278393
regenerate
Rich-Harris 09f2282
Update packages/kit/src/core/env.js
Rich-Harris 392a553
allow $app/env, $lib, and user aliases in src/env
Rich-Harris dd1b716
oops need mocks
Rich-Harris 403b5fc
unused
Rich-Harris 1702214
fix
Rich-Harris a1692f3
small bit of polish
Rich-Harris ab0db74
merge main
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': minor | ||
| --- | ||
|
|
||
| feat: explicit env vars |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
documentation/docs/20-core-concepts/70-environment-variables.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| --- | ||
| title: Environment variables | ||
| --- | ||
|
|
||
| Environment variables are values your app needs that exist separately from the app's source code. They allow you to use sensitive information like API keys and database credentials without storing them in version control. | ||
|
|
||
| During development, and at build time, variables defined in a `.env` or `.env.local` file will be added to the environment: | ||
|
|
||
| ```env | ||
| API_KEY=19f401ba-e8b0-48c4-8c77-b0ebb26d97fe | ||
| ``` | ||
|
|
||
| By default, every environment variable is implicitly available inside your app via the following modules: | ||
|
|
||
| - [`$env/static/private`]($env-static-private) | ||
| - [`$env/static/public`]($env-static-public) | ||
| - [`$env/dynamic/private`]($env-dynamic-private) | ||
| - [`$env/dynamic/public`]($env-dynamic-public) | ||
|
|
||
| ## Explicit environment variables | ||
|
|
||
| As of SvelteKit 2.62, you can opt into _explicit_ environment variables, in which case you instead import environment variables from these modules: | ||
|
|
||
| - [`$app/env/private`]($app-env-private) | ||
| - [`$app/env/public`]($app-env-public) | ||
|
|
||
| Additionally, the [`$app/environment`]($app-environment) module is renamed to [`$app/env`]($app-env). | ||
|
|
||
| > [!NOTE] Explicit environment variables will become the default in SvelteKit 3. The `$env/*` modules, along with `$app/environment`, will be removed. | ||
|
|
||
| ### Setup | ||
|
|
||
| To opt in, update your configuration... | ||
|
|
||
| ```js | ||
| /// file: svelte.config.js | ||
| export default { | ||
| kit: { | ||
| experimental: { | ||
| +++explicitEnvironmentVariables: true+++ | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ...and add a `src/env.ts` (or `src/env.js`) file that exports a `variables` object: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| // ... | ||
| }); | ||
| ``` | ||
|
|
||
| Each value in the object passed to [`defineEnvVars`](@sveltejs-kit-hooks#defineEnvVars) is an [`EnvVarConfig`](@sveltejs-kit#EnvVarConfig) object that configures the environment variable. | ||
|
|
||
| > [!NOTE] `defineEnvVars` returns its argument unaltered — it exists purely to help with type safety. | ||
|
|
||
| ### Private variables | ||
|
|
||
| By default, all variables are considered private. For example, you don't want to reveal your `API_KEY`: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| +++API_KEY: {}+++ | ||
| }); | ||
| ``` | ||
|
|
||
| > [!NOTE] Since no configuration is needed for this variable, we can use an empty object (`{}`). | ||
|
|
||
| Now that `API_KEY` is defined, it can be imported into app code via `$app/env/private`: | ||
|
|
||
| ```js | ||
| import { API_KEY } from '$app/env/private'; | ||
| ``` | ||
|
|
||
| The `$app/env/private` module cannot be imported into code that runs in the browser, so that you can't accidentally reveal your secrets in a JavaScript bundle. | ||
|
|
||
| ### Public variables | ||
|
|
||
| Some variables are perfectly safe — necessary, even — to expose to the browser. For these, we can specify `public: true`: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| GOOGLE_ANALYTICS_ID: { | ||
| +++public: true+++ | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| `GOOGLE_ANALYTICS_ID` can now be imported from `$app/env/public`, or used in your `app.html` template as `%sveltekit.env.GOOGLE_ANALYTICS_ID%`: | ||
|
|
||
| ```html | ||
| <!--- file: src/app.html --> | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="%sveltekit.assets%/favicon.png" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| %sveltekit.head% | ||
|
|
||
| +++ <script async src="https://www.googletagmanager.com/gtag/js?id=%sveltekit.env.GOOGLE_ANALYTICS_ID%"></script> | ||
| <script> | ||
| window.dataLayer ??= []; | ||
| function gtag(){dataLayer.push(arguments);} | ||
| gtag('js', new Date()); | ||
| gtag('config', '%sveltekit.env.GOOGLE_ANALYTICS_ID%'); | ||
| </script>+++ | ||
| </head> | ||
| <body data-sveltekit-preload-data="hover"> | ||
| <div style="display: contents">%sveltekit.body%</div> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| ### Validation | ||
|
|
||
| You can specify a [Standard Schema](https://standardschema.dev/) validator such as [Zod](https://zod.dev/) or [Valibot](https://valibot.dev/) to check that an environment variable value is correct: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
| +++import * as v from 'valibot';+++ | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| GOOGLE_ANALYTICS_ID: { | ||
| public: true, | ||
| +++validate: v.pipe(v.string(), v.regex(/G-[A-Z0-9]+/))+++ | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| If a value is invalid, the app will fail to start (or build). | ||
|
|
||
| You can use validators to make values optional, or transform them (such as turning a string into a boolean, or parsing JSON) — see your validation library's documentation to learn how. | ||
|
|
||
| ### Static variables | ||
|
|
||
| If a variable is configured with `static: true`, it will be inlined into your application code, enabling optimisations like dead-code elimination: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
| import * as v from 'valibot'; | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| SHOW_DEBUG_OVERLAY: { | ||
| public: true, | ||
| +++static: true,+++ | ||
|
|
||
| // coerce to true/false | ||
| validate: v.pipe( | ||
| v.optional(v.string(), ''), | ||
| v.transform((str) => str !== '') | ||
| ) | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| Because this variable is `static`, the `<DebugOverlay>` component shown here will be excluded from the JavaScript bundle unless `SHOW_DEBUG_OVERLAY` is truthy: | ||
|
|
||
| ```svelte | ||
| <script> | ||
| import { SHOW_DEBUG_OVERLAY } from '$app/env/public'; | ||
| import DebugOverlay from '$lib/components/DebugOverlay.svelte'; | ||
| </script> | ||
|
|
||
| {#if SHOW_DEBUG_OVERLAY} | ||
| <DebugOverlay /> | ||
| {/if} | ||
| ``` | ||
|
|
||
| But if the variable is set before building the app... | ||
|
|
||
| ```bash | ||
| SHOW_DEBUG_OVERLAY=true npm run build | ||
| ``` | ||
|
|
||
| ...then the component will be included and shown. | ||
|
|
||
| ### Documenting variables | ||
|
|
||
| You can document the purpose of an environment variable by adding a `description`: | ||
|
|
||
| ```ts | ||
| /// file: src/env.ts | ||
| import { defineEnvVars } from '@sveltejs/kit/hooks'; | ||
|
|
||
| export const variables = defineEnvVars({ | ||
| CACHE_TTL_SECONDS: { | ||
| description: 'How long to cache responses, in seconds' | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| Hovering over `CACHE_TTL_SECONDS` in your app code will show the description. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| title: $app/env | ||
| --- | ||
|
|
||
| > [!NOTE] This is an alias of [`$app/environment`]($app-environment), used when [explicit environment variables](environment-variables#Explicit-environment-variables) are enabled. | ||
|
|
||
| > MODULE: $app/env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| title: $app/env/private | ||
| --- | ||
|
|
||
| Private [environment variables](environment-variables) defined in `src/env.ts` (or `src/env.js`). | ||
|
|
||
| To use this module, you must enable the `experimental.explicitEnvironmentVariables` flag in your project configuration. | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| title: $app/env/public | ||
| --- | ||
|
|
||
| Public [environment variables](environment-variables) defined in `src/env.ts` (or `src/env.js`). | ||
|
|
||
| To use this module, you must enable the `experimental.explicitEnvironmentVariables` flag in your project configuration. | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.