-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: Runtime SVELTE_PUBLIC_* env variables #4293
feat: Runtime SVELTE_PUBLIC_* env variables #4293
Conversation
🦋 Changeset detectedLatest commit: b5cc17e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Does I'm -1 on doing something like this. If people want to expose runtime variables to the client (of whichever kind are supported in their deployment environment), they can just do so manually in |
This scenario and how it can be achieved is mentioned in the faq: https://kit.svelte.dev/faq#env-vars first class support for it in kit - and if that is something we want to add at all - should have been discussed in a feature-request first. |
Agree with the If a hacker can modify the env variables to generate executable javascript you are already lost, but thanks for the pointer to the I didn't consider getSession as I made assumptions it was like something PHP's
A slight advantage of this PR over getSession is that you can use Feel free to treat my PR's as a feature-requests. it just has an example implementation as a bonus. |
Thanks for the PR — we definitely do need to come up with a consistent/abstracted approach to dealing with environment variables, since
And all this would ideally
Aside: I've always found I'm not wholly convinced that import { env } from '$app/env';
import { create_api } from '$lib/db';
export const api = create_api(env.PUBLIC_BASE_URL); That said, it's not wholly clear to me what kinds of things would sit in that top right quadrant. Isn't that sort of thing generally known at build time? I guess I should move this comment into a new issue. |
I quickly ran into the I could use dependency injection and pass the $session.ENV as a parameter to the functions, but in my opinion that creates unnecessary boilerplate code. So i'll keep using the workaround with the handle Hook, which i'll share here: src/app.html
Into
src/hooks.js import dotenv from "dotenv";
dotenv.config();
const envScript = `<script type="svelte/env">${JSON.stringify(
Object.fromEntries(
Object.entries(process.env).filter(([key]) =>
key.startsWith("SVELTE_PUBLIC_")
)
)
)}</script>`;
export async function handle({ event, resolve }) {
const response = await resolve(event);
const body = await response.text();
return new Response(
body.replace('<script type="svelte/env"></script>', envScript),
response
);
} src/lib/env.js let env = {};
if (typeof process === "object" && typeof process.env === "object") {
env = process.env;
} else if (typeof document !== "undefined") {
const el = document.querySelector('script[type="svelte/env"]');
if (el) {
env = JSON.parse(el.textContent);
}
}
export default env; |
@bfanger I just came across your proposed solution here while looking for the best way to deal with this issue. I found though that the first time my app runs, it fails as the variables are only loaded after the actual path resolves. these values become available only after a reload is done. |
Using environment variables in SvelteKit is possible via vite's
VITE_*
variables.The downside is that these are compile-time environment variables, the values are injected into the javascript files during the dev and build steps.
This PR adds runtime environment variables, this allow you to change the variable in the environment without needing to recompile.
Use-cases:
Usage example:
How it works
At server startup a
<script type="svelte/env">
tag is generated containing values fromprocess.env
that start withSVELTE_PUBLIC_
.When importing the
$app/env
theenv
export is populated with values fromprocess.env
on the server and on the client it uses the values from the 'svelte/env' tag.Possible future improvements:
node -r dotenv/config node_modules/.bin/svelte-kit dev
)SVELTE_PUBLIC_
prefix/filter in the svelte.configprocess.env
is a NodeJS api)SVELTE_PUBLIC_
prefix naming inspiration came from NEXT_PUBLIC_, but these are also compile-time variables (via webpack)Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0Related #3030
Alternative implementation using hooks.ts