Skip to content

Commit fb711d7

Browse files
authored
fix: ensure environment setup is in its own chunk (#14441)
* fix: ensure environment setup is in its own chunk This way you cannot run into initialization order issues where e.g. some remote function uses environment variables before they're set Fixes #14439 * fix test
1 parent 3de8e38 commit fb711d7

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

.changeset/busy-pumas-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: ensure environment setup is in its own chunk

packages/kit/src/exports/vite/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,13 @@ async function kit({ svelte_config }) {
682682
config.build.rollupOptions.output = {
683683
...config.build.rollupOptions.output,
684684
manualChunks(id, meta) {
685+
// Prevent core runtime and env from ending up in a remote chunk, which could break because of initialization order
685686
if (id === `${runtime_directory}/app/server/index.js`) {
686687
return 'app-server';
687688
}
689+
if (id === `${runtime_directory}/shared-server.js`) {
690+
return 'app-shared-server';
691+
}
688692

689693
// Check if this is a *.remote.ts file
690694
if (svelte_config.kit.moduleExtensions.some((ext) => id.endsWith(`.remote${ext}`))) {

packages/kit/test/apps/basics/src/routes/remote/+page.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
set_count_server_set,
1010
resolve_deferreds
1111
} from './query-command.remote.js';
12+
import { q } from './accessing-env.remote';
1213
1314
let { data } = $props();
1415
1516
let command_result = $state(null);
16-
let release;
17+
18+
// we just want it not to be treeshaken away
19+
q;
1720
1821
const count = browser ? get_count() : null; // so that we get a remote request in the browser
1922
</script>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import { query } from '$app/server';
12
import { env } from '$env/dynamic/private';
23
import { env as public_env } from '$env/dynamic/public';
34

45
if (!env.PRIVATE_DYNAMIC || !public_env.PUBLIC_DYNAMIC) {
56
// This checks that dynamic env vars are available when prerendering remote functions
67
// https://github.com/sveltejs/kit/pull/14219
8+
// and are not in the same chunk as this one
9+
// https://github.com/sveltejs/kit/issues/14439
710
throw new Error('Dynamic environment variables are not set up correctly');
811
}
12+
13+
// placeholder query that needs to be imported/used elsewhere so that bundling/chunking would include env setup if not setup correctly
14+
export const q = query(() => {});

0 commit comments

Comments
 (0)