Skip to content
Merged
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: 0 additions & 3 deletions packages/kit/src/runtime/app/environment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ if (__SVELTEKIT_EXPERIMENTAL_EXPLICIT_ENVIRONMENT_VARIABLES__) {
'Cannot import `$app/environment` when `experimental.explicitEnvironmentVariables` is enabled. Use `$app/env` instead.'
);
}

// force the Vite client to load, so that defines are definitely defined
import.meta.hot;
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,11 @@ export async function render_response({
}`);
}

// we need to eagerly import the Vite client module in development to ensure
// that Vite global constant replacements are initialised before our code runs
const init_app = `
{
${DEV ? `import('${paths.base}/@vite/client')` : ''}
${blocks.join('\n\n\t\t\t\t\t')}
}
`;
Expand Down
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;
16 changes: 16 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,19 @@ 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,
javaScriptEnabled
}) => {
test.skip(!javaScriptEnabled || !process.env.DEV);

await page.goto('/basepath', { wait_for_started: false });
await expect(page.locator('body.started')).toBeVisible();
expect(await page.pageErrors()).toHaveLength(0);
});
});
Loading