Skip to content

Commit 80279cc

Browse files
authored
svelte: Dynamically import $app/paths in init() hook (#12948)
1 parent ed2e864 commit 80279cc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

svelte/src/hooks.client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { asset } from '$app/paths';
2-
31
export async function init() {
42
if (import.meta.env.VITE_MSW_ENABLED) {
53
let { http, passthrough } = await import('msw');
@@ -12,6 +10,11 @@ export async function init() {
1210
http.get('https://:avatars.githubusercontent.com/u/:id', passthrough),
1311
http.get('https://code.cdn.mozilla.net/fonts/*', passthrough),
1412
);
13+
14+
// We need to dynamically import `$app/paths` here to avoid a race condition during app startup,
15+
// resulting in a "ReferenceError: __SVELTEKIT_PAYLOAD__ is not defined" error.
16+
let { asset } = await import('$app/paths');
17+
1518
await worker.start({
1619
serviceWorker: { url: asset('/mockServiceWorker.js') },
1720
onUnhandledRequest(request, print) {

0 commit comments

Comments
 (0)