Skip to content
Open
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/wide-otters-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: ensure `version` is defined when importing from `$app/env` with explicit environment variables
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/app/env/index.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions packages/kit/test/apps/options-2/src/hooks.client.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
});
Loading