From 7e0173b7d246c22cb87e88c63a413b955b3769f0 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 13:24:12 -0600 Subject: [PATCH 01/18] docs: clarify functionality of $env/dynamic/private --- .../types/synthetic/$env+dynamic+private.md | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index 173419b6788d..b8e8d9c50c4c 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -1,10 +1,22 @@ -This module provides access to runtime environment variables, as defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) _and do_ start with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (if configured). +This module provides access to environment variables set _dynamically_ at runtime, as defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. -This module cannot be imported into client-side code. +This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) _and do_ start with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (if configured). + +This module (and `$env/static/private`) cannot be imported into client-side code. + +> [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. + +For example, suppose the runtime environment variables were set like this: + +```env +PUBLIC_EXAMPLE_VARIABLE=foo +OTHER_VARIABLE=bar +``` + +If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): ```ts import { env } from '$env/dynamic/private'; -console.log(env.DEPLOYMENT_SPECIFIC_VARIABLE); +console.log(env.PUBLIC_EXAMPLE_VARIABLE); // => undefined +console.log(env.OTHER_VARIABLE); // => "bar" ``` - -> [!NOTE] In `dev`, `$env/dynamic` always includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. From f98e2583338826163880bfe49e794ebe377ea9c7 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 13:40:52 -0600 Subject: [PATCH 02/18] docs: better clarity across public/private Clarify the purpose and access restrictions of the module. --- packages/kit/src/types/synthetic/$env+dynamic+private.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index b8e8d9c50c4c..975b73405ff2 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -1,8 +1,11 @@ -This module provides access to environment variables set _dynamically_ at runtime, as defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. +Similar to [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public), this module provides access to environment variables set _dynamically_ at runtime, but that are limited to _private_ access. -This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) _and do_ start with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (if configured). +Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. -This module (and `$env/static/private`) cannot be imported into client-side code. +**_Private_ access:** +- This module (and [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private)) cannot be imported into client-side code. +- Variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are **excluded**. +- Variables that begin with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to allow-all) are **included**. > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. From ff83cb5d6e266b18358c86101fc3e9f984472628 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 13:42:20 -0600 Subject: [PATCH 03/18] docs: clarify functionality of $env/dynamic/public --- .../types/synthetic/$env+dynamic+public.md | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+public.md b/packages/kit/src/types/synthetic/$env+dynamic+public.md index d01ec645e9f1..0d36b04bf099 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+public.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+public.md @@ -1,8 +1,24 @@ -Similar to [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private), but only includes variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`), and can therefore safely be exposed to client-side code. +Similar to [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private), this module provides access to environment variables set _dynamically_ at runtime, but that are _publicly_ accessible. -Note that public dynamic environment variables must all be sent from the server to the client, causing larger network requests — when possible, use `$env/static/public` instead. +Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. + +**_Public_ access:** +- This module (and [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public)) _can_ be imported into client-side code. +- **Only** variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are included. + +> [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. + +For example, suppose the runtime environment variables were set like this: + +```env +PUBLIC_EXAMPLE_VARIABLE=foo +OTHER_VARIABLE=bar +``` + +If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): ```ts import { env } from '$env/dynamic/public'; -console.log(env.PUBLIC_DEPLOYMENT_SPECIFIC_VARIABLE); +console.log(env.PUBLIC_EXAMPLE_VARIABLE); // => "foo" +console.log(env.OTHER_VARIABLE); // => undefined ``` From e596c174fa76d5ca041aca64a5965f11007c7cff Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:01:52 -0600 Subject: [PATCH 04/18] docs: finish organizing for dynamic/private --- .../types/synthetic/$env+dynamic+private.md | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index 975b73405ff2..53ff78b604db 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -1,4 +1,9 @@ -Similar to [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public), this module provides access to environment variables set _dynamically_ at runtime, but that are limited to _private_ access. +This module provides access to environment variables set _dynamically_ at runtime and that are limited to _private_ access. + +| Access | Runtime | Buildtime | +|-|-|-| +| Private | `$env/dynamic/private` | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. @@ -9,17 +14,27 @@ Runtime environment variables are defined by the platform you're running on. For > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. +> [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: +> ``` +> MY_FEATURE_FLAG= +> ``` +> You can override `.env` values from the command line like so: +> ```sh +> MY_FEATURE_FLAG="enabled" npm run dev +> ``` + For example, suppose the runtime environment variables were set like this: ```env -PUBLIC_EXAMPLE_VARIABLE=foo -OTHER_VARIABLE=bar +ENVIRONMENT=production +PUBLIC_BASE_URL=http://site.com ``` If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): ```ts import { env } from '$env/dynamic/private'; -console.log(env.PUBLIC_EXAMPLE_VARIABLE); // => undefined -console.log(env.OTHER_VARIABLE); // => "bar" + +console.log(env.ENVIRONMENT); // => "production" +console.log(env.PUBLIC_BASE_URL); // => undefined ``` From 2a8d8b21acef3316fcd5bded9c27ef12c19322fc Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:02:15 -0600 Subject: [PATCH 05/18] docs: change table layout slightly --- packages/kit/src/types/synthetic/$env+dynamic+private.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index 53ff78b604db..8b3fbacc91c3 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -1,6 +1,6 @@ This module provides access to environment variables set _dynamically_ at runtime and that are limited to _private_ access. -| Access | Runtime | Buildtime | +| | Runtime | Buildtime | |-|-|-| | Private | `$env/dynamic/private` | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | | Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | From a7c4c7cd8c7a4a3073bf85697e881ffa0b0e73ea Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:03:26 -0600 Subject: [PATCH 06/18] docs: finish organizing for dynamic/public Clarified the description and usage of the $env/dynamic/public module, including details on public access and environment variable handling. --- .../types/synthetic/$env+dynamic+public.md | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+public.md b/packages/kit/src/types/synthetic/$env+dynamic+public.md index 0d36b04bf099..bf5fb4e0218a 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+public.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+public.md @@ -1,4 +1,9 @@ -Similar to [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private), this module provides access to environment variables set _dynamically_ at runtime, but that are _publicly_ accessible. +This module provides access to environment variables set _dynamically_ at runtime and that are _publicly_ accessible. + +| | Runtime | Buildtime | +|-|-|-| +| Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | +| Public | `$env/dynamic/public` | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. @@ -8,17 +13,27 @@ Runtime environment variables are defined by the platform you're running on. For > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. +> [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: +> ``` +> MY_FEATURE_FLAG= +> ``` +> You can override `.env` values from the command line like so: +> ```sh +> MY_FEATURE_FLAG="enabled" npm run dev +> ``` + For example, suppose the runtime environment variables were set like this: ```env -PUBLIC_EXAMPLE_VARIABLE=foo -OTHER_VARIABLE=bar +ENVIRONMENT=production +PUBLIC_BASE_URL=http://site.com ``` If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): ```ts import { env } from '$env/dynamic/public'; -console.log(env.PUBLIC_EXAMPLE_VARIABLE); // => "foo" -console.log(env.OTHER_VARIABLE); // => undefined + +console.log(env.ENVIRONMENT); // => undefined +console.log(env.PUBLIC_BASE_URL); // => "http://site.com" ``` From 8a772c2fb32f307f95793439a38b7170926435dd Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:05:05 -0600 Subject: [PATCH 07/18] docs: specify code block syntax Clarify notes on environment variable handling in dev and prod. --- packages/kit/src/types/synthetic/$env+dynamic+private.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index 8b3fbacc91c3..fcd87cf89dc0 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -15,7 +15,7 @@ Runtime environment variables are defined by the platform you're running on. For > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. > [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: -> ``` +> ```env > MY_FEATURE_FLAG= > ``` > You can override `.env` values from the command line like so: From 3d0d9567836bede5fba1069415096d2862bedfa3 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:05:27 -0600 Subject: [PATCH 08/18] docs: specify code block syntax --- packages/kit/src/types/synthetic/$env+dynamic+public.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+public.md b/packages/kit/src/types/synthetic/$env+dynamic+public.md index bf5fb4e0218a..245f5d1e9e87 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+public.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+public.md @@ -14,7 +14,7 @@ Runtime environment variables are defined by the platform you're running on. For > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. > [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: -> ``` +> ```env > MY_FEATURE_FLAG= > ``` > You can override `.env` values from the command line like so: From f7e28fe8c3437a60c16b635cb825dff0a9324f7a Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:06:44 -0600 Subject: [PATCH 09/18] docs: finish organizing for static/private --- .../types/synthetic/$env+static+private.md | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+static+private.md b/packages/kit/src/types/synthetic/$env+static+private.md index bf3599977eb3..9cc9b7a91ae4 100644 --- a/packages/kit/src/types/synthetic/$env+static+private.md +++ b/packages/kit/src/types/synthetic/$env+static+private.md @@ -1,19 +1,29 @@ -Environment variables [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env`. Like [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private), this module cannot be imported into client-side code. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) _and do_ start with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (if configured). +This module provides access to environment variables that are injected _statically_ into your bundle at buildtime and are limited to _private_ access. -_Unlike_ [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private), the values exported from this module are statically injected into your bundle at build time, enabling optimisations like dead code elimination. +| | Runtime | Buildtime | +|-|-|-| +| Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | `$env/static/private` | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | -```ts -import { API_KEY } from '$env/static/private'; -``` +Environment variables are [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env` at build time and then statically injected into your bundle at build time, enabling optimisations like dead code elimination. -Note that all environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: +**_Private_ access:** +- This module (and [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private)) cannot be imported into client-side code. +- Variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are **excluded**. +- Variables that begin with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to allow-all) are **included**. +For example, suppose the environment variables were set like this during build: + +```env +ENVIRONMENT=production +PUBLIC_BASE_URL=http://site.com ``` -MY_FEATURE_FLAG="" -``` -You can override `.env` values from the command line like so: +If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): + +```ts +import { ENVIRONMENT, PUBLIC_BASE_URL } from '$env/static/private'; -```sh -MY_FEATURE_FLAG="enabled" npm run dev +console.log(ENVIRONMENT); // => "production" +console.log(PUBLIC_BASE_URL); // => undefined ``` From 1b367d58dcc580029f5209b45f81db33f0cc75dd Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:08:35 -0600 Subject: [PATCH 10/18] docs: add note about build errors --- packages/kit/src/types/synthetic/$env+static+private.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+static+private.md b/packages/kit/src/types/synthetic/$env+static+private.md index 9cc9b7a91ae4..6f11a1e09214 100644 --- a/packages/kit/src/types/synthetic/$env+static+private.md +++ b/packages/kit/src/types/synthetic/$env+static+private.md @@ -25,5 +25,5 @@ If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (th import { ENVIRONMENT, PUBLIC_BASE_URL } from '$env/static/private'; console.log(ENVIRONMENT); // => "production" -console.log(PUBLIC_BASE_URL); // => undefined +console.log(PUBLIC_BASE_URL); // => undefined, throws error during build ``` From f02b474747f61fd58ec64051aa36c6fb8a90ea6f Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:09:52 -0600 Subject: [PATCH 11/18] docs: finish organizing for static/public --- .../src/types/synthetic/$env+static+public.md | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+static+public.md b/packages/kit/src/types/synthetic/$env+static+public.md index 70a98b458f78..33c6619267f6 100644 --- a/packages/kit/src/types/synthetic/$env+static+public.md +++ b/packages/kit/src/types/synthetic/$env+static+public.md @@ -1,7 +1,28 @@ -Similar to [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private), except that it only includes environment variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`), and can therefore safely be exposed to client-side code. +This module provides access to environment variables that are injected _statically_ into your bundle at buildtime and are _publicly_ accessible. -Values are replaced statically at build time. +| | Runtime | Buildtime | +|-|-|-| +| Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | `$env/static/public` | + +Environment variables are [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env` at build time and then statically injected into your bundle at build time, enabling optimisations like dead code elimination. + +**_Public_ access:** +- This module (and [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public)) _can_ be imported into client-side code. +- **Only** variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are included. + +For example, suppose the environment variables were set like this during build: + +```env +ENVIRONMENT=production +PUBLIC_BASE_URL=http://site.com +``` + +If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): ```ts -import { PUBLIC_BASE_URL } from '$env/static/public'; +import { ENVIRONMENT, PUBLIC_BASE_URL } from '$env/static/public'; + +console.log(ENVIRONMENT); // => undefined, throws error during build +console.log(PUBLIC_BASE_URL); // => "http://site.com" ``` From de77e9e43c8594e5160e05270f29b76777a82b20 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:32:05 -0600 Subject: [PATCH 12/18] docs: clarify buildtime versus runtime variables --- packages/kit/src/types/synthetic/$env+static+public.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+static+public.md b/packages/kit/src/types/synthetic/$env+static+public.md index 33c6619267f6..730ef2903b10 100644 --- a/packages/kit/src/types/synthetic/$env+static+public.md +++ b/packages/kit/src/types/synthetic/$env+static+public.md @@ -18,7 +18,7 @@ ENVIRONMENT=production PUBLIC_BASE_URL=http://site.com ``` -If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): +Assuming the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour), this is what would happen at runtime, even if the environment variables at runtime are different: ```ts import { ENVIRONMENT, PUBLIC_BASE_URL } from '$env/static/public'; From d2b46d8cbd29138d0daf57cbed8c8f989011e593 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:32:21 -0600 Subject: [PATCH 13/18] docs: clarify buildtime versus runtime variables --- packages/kit/src/types/synthetic/$env+static+private.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/types/synthetic/$env+static+private.md b/packages/kit/src/types/synthetic/$env+static+private.md index 6f11a1e09214..bb8ea2d8c57f 100644 --- a/packages/kit/src/types/synthetic/$env+static+private.md +++ b/packages/kit/src/types/synthetic/$env+static+private.md @@ -19,7 +19,7 @@ ENVIRONMENT=production PUBLIC_BASE_URL=http://site.com ``` -If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): +Assuming the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour), this is what would happen at runtime, even if the environment variables at runtime are different: ```ts import { ENVIRONMENT, PUBLIC_BASE_URL } from '$env/static/private'; From ec57083c06ae483437e89aa2a09382705ea046e9 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:33:22 -0600 Subject: [PATCH 14/18] docs: clarify buildtime versus runtime variables --- .../types/synthetic/$env+dynamic+public.md | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+public.md b/packages/kit/src/types/synthetic/$env+dynamic+public.md index 245f5d1e9e87..2fad47b5c281 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+public.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+public.md @@ -22,18 +22,34 @@ Runtime environment variables are defined by the platform you're running on. For > MY_FEATURE_FLAG="enabled" npm run dev > ``` -For example, suppose the runtime environment variables were set like this: +For example, suppose the buildtime environment variables were set like this: ```env ENVIRONMENT=production PUBLIC_BASE_URL=http://site.com +PUBLIC_VERSION=1 ``` -If the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour): +And then suppose at runtime the environment variables were set like this: + +```env +ENVIRONMENT=production +PUBLIC_BASE_URL=http://not-the-same-site.com +PUBLIC_VERSION=9001 +``` + +Assuming the `publicPrefix` is set to `PUBLIC_` and the `privatePrefix` is not set (the default behaviour), this is what would happen at runtime: ```ts import { env } from '$env/dynamic/public'; -console.log(env.ENVIRONMENT); // => undefined +console.log(env.ENVIRONMENT); // => undefined, not public +console.log(env.PUBLIC_BASE_URL); // => "http://not-the-same-site.com" +console.log(env.PUBLIC_VERSION); // => "9001" + +import { ENVIRONMENT, PUBLIC_BASE_URL, PUBLIC_VERSION } from '$env/static/public'; + +console.log(env.ENVIRONMENT); // => undefined, throws error during build console.log(env.PUBLIC_BASE_URL); // => "http://site.com" +console.log(env.PUBLIC_VERSION); // => "1" ``` From a7237f2a9bacf6d1693aace3b1aa8c0f4c898127 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:52:33 -0600 Subject: [PATCH 15/18] docs: apply prettier --- .../kit/src/types/synthetic/$env+dynamic+private.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+private.md b/packages/kit/src/types/synthetic/$env+dynamic+private.md index fcd87cf89dc0..925049df21a4 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+private.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+private.md @@ -1,13 +1,14 @@ This module provides access to environment variables set _dynamically_ at runtime and that are limited to _private_ access. -| | Runtime | Buildtime | -|-|-|-| -| Private | `$env/dynamic/private` | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | -| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | +| | Runtime | Buildtime | +| ------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------ | +| Private | `$env/dynamic/private` | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. **_Private_ access:** + - This module (and [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private)) cannot be imported into client-side code. - Variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are **excluded**. - Variables that begin with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to allow-all) are **included**. @@ -15,10 +16,13 @@ Runtime environment variables are defined by the platform you're running on. For > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. > [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: +> > ```env > MY_FEATURE_FLAG= > ``` +> > You can override `.env` values from the command line like so: +> > ```sh > MY_FEATURE_FLAG="enabled" npm run dev > ``` From 351d20aa514a744af0f621951b5e4813586ff5f8 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:52:53 -0600 Subject: [PATCH 16/18] docs: apply prettier --- .../kit/src/types/synthetic/$env+dynamic+public.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+dynamic+public.md b/packages/kit/src/types/synthetic/$env+dynamic+public.md index 2fad47b5c281..8b52189fb047 100644 --- a/packages/kit/src/types/synthetic/$env+dynamic+public.md +++ b/packages/kit/src/types/synthetic/$env+dynamic+public.md @@ -1,23 +1,27 @@ This module provides access to environment variables set _dynamically_ at runtime and that are _publicly_ accessible. -| | Runtime | Buildtime | -|-|-|-| +| | Runtime | Buildtime | +| ------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------ | | Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | -| Public | `$env/dynamic/public` | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | +| Public | `$env/dynamic/public` | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | Runtime environment variables are defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) (or running [`vite preview`](https://svelte.dev/docs/kit/cli)), this is equivalent to `process.env`. **_Public_ access:** + - This module (and [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public)) _can_ be imported into client-side code. - **Only** variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are included. > [!NOTE] In `dev`, `$env/dynamic` includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter. > [!NOTE] To get correct types, environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed: +> > ```env > MY_FEATURE_FLAG= > ``` +> > You can override `.env` values from the command line like so: +> > ```sh > MY_FEATURE_FLAG="enabled" npm run dev > ``` From 2ad94d43dadc508ca0d597ab28a0709e9577d0ee Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:53:15 -0600 Subject: [PATCH 17/18] docs: apply prettier --- packages/kit/src/types/synthetic/$env+static+private.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+static+private.md b/packages/kit/src/types/synthetic/$env+static+private.md index bb8ea2d8c57f..85a70374c73a 100644 --- a/packages/kit/src/types/synthetic/$env+static+private.md +++ b/packages/kit/src/types/synthetic/$env+static+private.md @@ -1,13 +1,14 @@ This module provides access to environment variables that are injected _statically_ into your bundle at buildtime and are limited to _private_ access. -| | Runtime | Buildtime | -|-|-|-| -| Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | `$env/static/private` | -| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | +| | Runtime | Buildtime | +| ------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------- | +| Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | `$env/static/private` | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | [`$env/static/public`](https://svelte.dev/docs/kit/$env-static-public) | Environment variables are [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env` at build time and then statically injected into your bundle at build time, enabling optimisations like dead code elimination. **_Private_ access:** + - This module (and [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private)) cannot be imported into client-side code. - Variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are **excluded**. - Variables that begin with [`config.kit.env.privatePrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to allow-all) are **included**. From 0527780fb52cd22803b1be3c1d58404f356f603d Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Fri, 19 Dec 2025 15:53:36 -0600 Subject: [PATCH 18/18] docs: apply prettier --- packages/kit/src/types/synthetic/$env+static+public.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/types/synthetic/$env+static+public.md b/packages/kit/src/types/synthetic/$env+static+public.md index 730ef2903b10..11bdf9ae2426 100644 --- a/packages/kit/src/types/synthetic/$env+static+public.md +++ b/packages/kit/src/types/synthetic/$env+static+public.md @@ -1,13 +1,14 @@ This module provides access to environment variables that are injected _statically_ into your bundle at buildtime and are _publicly_ accessible. -| | Runtime | Buildtime | -|-|-|-| +| | Runtime | Buildtime | +| ------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------ | | Private | [`$env/dynamic/private`](https://svelte.dev/docs/kit/$env-dynamic-private) | [`$env/static/private`](https://svelte.dev/docs/kit/$env-static-private) | -| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | `$env/static/public` | +| Public | [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public) | `$env/static/public` | Environment variables are [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env` at build time and then statically injected into your bundle at build time, enabling optimisations like dead code elimination. **_Public_ access:** + - This module (and [`$env/dynamic/public`](https://svelte.dev/docs/kit/$env-dynamic-public)) _can_ be imported into client-side code. - **Only** variables that begin with [`config.kit.env.publicPrefix`](https://svelte.dev/docs/kit/configuration#env) (which defaults to `PUBLIC_`) are included.