Skip to content

feat: Runtime SVELTE_PUBLIC_* env variables #4293

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-apples-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add support for runtime environment variables (SVELTE*PUBLIC*\*)
17 changes: 17 additions & 0 deletions packages/kit/src/runtime/app/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,22 @@ export const mode = import.meta.env.MODE;
* @type {import('$app/env').amp}
*/
export const amp = !!import.meta.env.VITE_SVELTEKIT_AMP;
/**
* @type {import('$app/env').env}
*/
export const env = extractEnv();

export { prerendering } from '../env.js';

function extractEnv() {
if (typeof process === 'object' && typeof process.env === 'object') {
return process.env;
}
if (typeof document !== 'undefined') {
const el = document.querySelector('script[type="svelte/env"]');
if (el) {
return new Function('return ' + el?.textContent)();
}
}
return {};
}
19 changes: 19 additions & 0 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { s } from '../../../utils/misc.js';
import { create_prerendering_url_proxy } from './utils.js';
import { Csp, csp_ready } from './csp.js';

const envScript = renderEnvScript();

// TODO rename this function/module

const updated = {
Expand Down Expand Up @@ -137,6 +139,7 @@ export async function render_response({

try {
rendered = options.root.render(props);
rendered.head += envScript;
} finally {
unsubscribe();
}
Expand Down Expand Up @@ -355,3 +358,19 @@ function serialize_error(error) {
}
return serialized;
}

function renderEnvScript() {
/** @type {Record<string, string | undefined>} */
const env = {};
if (typeof process === 'object' && typeof process.env === 'object') {
Object.entries(process.env).forEach(([name, value]) => {
if (name.startsWith('SVELTE_PUBLIC_')) {
env[name] = value;
}
});
}
if (Object.keys(env).length === 0) {
return '';
}
return `\n <script type="svelte/env">${devalue(env)}</script>`;
}
5 changes: 5 additions & 0 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ declare module '$app/env' {
* By default, `svelte-kit dev` runs with `mode=development` and `svelte-kit build` runs with `mode=production`.
*/
export const mode: string;
/**
* Runtime environment variables.
* Only variables starting with "SVELTE_PUBLIC_" are available on the client, the server has access to all variables.
*/
export const env: Record<string, string | undefined>;
}

/**
Expand Down