What's the difference between "dynamic" and "static" env variables in SvelteKit? #6718
Replies: 3 comments 4 replies
-
$env/dynamic/private
$env/static/private
$env/dynamic/public
$env/static/public
|
Beta Was this translation helpful? Give feedback.
-
Static variables get replaced at build time and dynamic variables get replaced at runtime. Static variables allow compile-time computations which can give better performance - e.g. if they're used in an |
Beta Was this translation helpful? Give feedback.
-
I know this is an old topic but I was hoping someone could clear this up for me. I'm still confused over the documentation (and replies here even) about preferring A direct example from https://learn.svelte.dev/tutorial/env-static-private <script>
import { PASSPHRASE } from '$env/static/private';
export let form;
</script> From https://learn.svelte.dev/tutorial/env-dynamic-public
Isn't it an accepted practice to keep secrets out of builds such as docker images? You wouldn't go committing unencrypted secrets to your Github repos, so why would you have them statically injected into your code at build time? In this containerized era builds are commonly done in CI/CD pipelines and then shipped off as images to the deployment platforms, hence the whole industry around providing secure storage of secrets (Vault, SOPS, etc.) aimed at injecting them at run time... Additional:
To put it simply, I believe |
Beta Was this translation helpful? Give feedback.
-
I can't quite figure this out, I wish the documentation made this a little more clear.
I'm struggling to understand the difference between "dynamic" and "static" environment variables in SvelteKit (imported from
$env/dynamic/...
and$env/static/...
respectively) and more importantly under which circumstances I should prefer one over the other.So for example, my SvelteKit app acts as my frontend and then I also have a backend server (written in a different programming language) acting as the API which the frontend talks to. Now, I'd like to use the
handleFetch
hook to replace the public URL of my API with a local one whenever a server-sidefetch
request is being sent to the API (in order to hit it directly and bypass any proxies, as per the example here) so I need to have two environment variablesPUBLIC_API_URL
andLOCAL_API_URL
— which would obviously have different values in production and development — but I'm not sure if I'm supposed to use$env/dynamic
or$env/static
in a case like this.I'd appreciate it someone enlightens me here.
Beta Was this translation helpful? Give feedback.
All reactions