From 1ed409de84a94205620f5c9ae2afa81e4a69e713 Mon Sep 17 00:00:00 2001 From: Nic Polumeyv <162764842+Nic-Polumeyv@users.noreply.github.com> Date: Sat, 6 Jun 2026 23:31:21 -0400 Subject: [PATCH] fix: ensure version is defined when using $app/env with explicit env vars --- .changeset/wide-otters-shout.md | 5 +++++ packages/kit/src/runtime/app/env/index.js | 3 +++ .../kit/test/apps/options-2/src/hooks.client.js | 6 ++++++ packages/kit/test/apps/options-2/test/test.js | 14 ++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 .changeset/wide-otters-shout.md create mode 100644 packages/kit/test/apps/options-2/src/hooks.client.js diff --git a/.changeset/wide-otters-shout.md b/.changeset/wide-otters-shout.md new file mode 100644 index 000000000000..87948d6d46a8 --- /dev/null +++ b/.changeset/wide-otters-shout.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: ensure `version` is defined when importing from `$app/env` with explicit environment variables diff --git a/packages/kit/src/runtime/app/env/index.js b/packages/kit/src/runtime/app/env/index.js index 87a7b3521a10..e88b4a01f4cb 100644 --- a/packages/kit/src/runtime/app/env/index.js +++ b/packages/kit/src/runtime/app/env/index.js @@ -1,2 +1,5 @@ export { BROWSER as browser, DEV as dev } from 'esm-env'; export { building, version } from './internal.js'; + +// force the Vite client to load, so that defines are definitely defined +import.meta.hot; diff --git a/packages/kit/test/apps/options-2/src/hooks.client.js b/packages/kit/test/apps/options-2/src/hooks.client.js new file mode 100644 index 000000000000..191772d8dd05 --- /dev/null +++ b/packages/kit/test/apps/options-2/src/hooks.client.js @@ -0,0 +1,6 @@ +import { version } from '$app/env'; + +// Regression guard for #15971: this import runs before the router is initialized. +// Without the fix it throws `__SVELTEKIT_APP_VERSION__ is not defined` when +// `experimental.explicitEnvironmentVariables` is enabled. `void` keeps the import. +void version; diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 802d5faf331f..a206f6483ff9 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -184,3 +184,17 @@ test.describe("bundleStrategy: 'single'", () => { await expect(page.locator('h1', { hasText: 'It works!' })).toBeVisible(); }); }); + +test.describe('$app/env', () => { + // regression test for https://github.com/sveltejs/kit/issues/15971: + // importing `$app/env` before the router is initialized (e.g. in + // hooks.client.js) must not throw `__SVELTEKIT_APP_VERSION__ is not defined` + test('version is defined when imported during client init', async ({ page }) => { + /** @type {string[]} */ + const errors = []; + page.on('pageerror', (error) => errors.push(error.message)); + await page.goto('/basepath'); + await page.waitForTimeout(500); + expect(errors.filter((message) => message.includes('__SVELTEKIT_APP_VERSION__'))).toEqual([]); + }); +});