- Svelte - Home
- Svelte - Introduction
- Svelte - Installation
- Svelte - Reactivity
- Svelte - Props
- Svelte - Logic
- Svelte - Events
- Svelte - Bindings
- Svelte - Classes
- Svelte - Styles
- Svelte - Actions
- Svelte - Transitions
- Svelte - Advance Reactivity
- Svelte - Reusing Content
- Svelte - Motion
- Svelte - Advanced Binding
- Svelte - Advanced Transitions
- Svelte - Context API
- Svelte - Special Elements
- SvelteKit Basics
- SvelteKit - Introduction
- SvelteKit - Routing
- SvelteKit - Loading Data
- SvelteKit - Headers
- SvelteKit - Cookies
- SvelteKit - Shared Modules
- SvelteKit - Forms
- SvelteKit - API Routes
- SvelteKit - States
- SvelteKit - Errors
- SvelteKit - Redirects
- SvelteKit Advanced
- SvelteKit - Hooks
- SvelteKit - Page Options
- SvelteKit - Link Options
- SvelteKit - Advanced Routing
- SvelteKit - Advanced Loading
- SvelteKit - Environment Variables
- Svelte Resources
- svelte - Useful Resources
- svelte - Discussion
Sveltekit - Environmental Variables
Environmental Variables in SvelteKit are crucial for managing and storing sensitive information like API keys, database credentials, and other configuration settings. These variables can be categorized into private and public types, with further differences between static and dynamic values.
Static Private Variables
Static private environment variables are defined in a .env file and are available at build time. They can be accessed in server-side code using the $env/static/private module.
Syntax Example
For example, if you define a passphrase in your .env file:
PASSPHRASE="open sesame"
Then, you can import it in your server module like this:
import { PASSPHRASE } from '$env/static/private';
Dynamic Private Variables
Dynamic private variables are accessed using the $env/dynamic/private module and are not available until the application is running. This is especially helpful when you need to read values that may change or are determined at runtime.
Static Public Variables
In SvelteKit, the static public variables are special environmental variables that can be safely shown in the browser. These public variables start with the prefix PUBLIC_ to set them apart from private variables, which should not be shared.
Using Static Public Variables
To use the static public variables follow the steps mentioned below:
- Define Variables in .env File: Firstly, you have to define your public environment variables in a .env file like this:
PUBLIC_THEME_BACKGROUND="steelblue"
PUBLIC_THEME_FOREGROUND="bisque"
<script>
import {
PUBLIC_THEME_BACKGROUND,
PUBLIC_THEME_FOREGROUND
} from '$env/static/public';
</script>
Dynamic Public Variables
In SvelteKit, dynamic public variables are environment variables that can be accessed at runtime and are safe to expose to the client side. You can get these variables by importing them from $env/dynamic/public, so you can use them in your components.
Using Dynamic Public Variables
To use the Dynamic public variables follow the steps mentioned below:
- Define Variables: Firstly, you have to define your dynamic public variables in your environment configuration (e.g., .env file) in the following ways:
PUBLIC_THEME_BACKGROUND="steelblue"
PUBLIC_THEME_FOREGROUND="bisque"
<script>
import { env } from '$env/dynamic/public';
</script>
Example
The following example demonstrates the usage of dynamic public variables displaying Hello World on the browser with the background color steel blue and foreground color bisque.
<!--src/.env-->
PUBLIC_THEME_BACKGROUND="steelblue"
PUBLIC_THEME_FOREGROUND="bisque"
<!--src/routes/+page.svelte-->
<script>
import { env } from '$env/dynamic/public';
</script>
<main style:background={env.PUBLIC_THEME_BACKGROUND} style:color={env.PUBLIC_THEME_FOREGROUND}>
Hello, World!
</main>
Output