Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/empty-animals-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: wait an extra microtask in dev before calling `$$_init_$$`
8 changes: 8 additions & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,21 @@ async function kit({ svelte_config }) {
remotes.push(remote);

if (opts?.ssr) {
// we need to add an `await Promise.resolve()` because if the user imports this function
// on the client AND in a load function when loading the client module we will trigger
// an ssrLoadModule during dev. During a link preload the the module can be mistakenly
// loaded and transformed twice and the first time all its exports would be undefined
// triggering a dev server error. By adding a microtask we ensure that the module is fully loaded

// Extra newlines to prevent syntax errors around missing semicolons or comments
code +=
'\n\n' +
dedent`
import * as $$_self_$$ from './${path.basename(id)}';
import { init_remote_functions as $$_init_$$ } from '@sveltejs/kit/internal';

${dev_server ? 'await Promise.resolve()' : ''}

$$_init_$$($$_self_$$, ${s(file)}, ${s(remote.hash)});

for (const [name, fn] of Object.entries($$_self_$$)) {
Expand Down