Skip to content

Commit 21b0173

Browse files
committed
Refactor view transition to use onNavigate
We can also preload data again since we don't have race conditions.
1 parent e2d9d3a commit 21b0173

File tree

2 files changed

+8
-41
lines changed

2 files changed

+8
-41
lines changed

Diff for: src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
%sveltekit.head%
88
</head>
9-
<body>
9+
<body data-sveltekit-preload-data="hover">
1010
<div>%sveltekit.body%</div>
1111
</body>
1212
</html>

Diff for: src/lib/page-transition.js

+7-40
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
import { beforeNavigate } from '$app/navigation';
2-
import { navigating } from '$app/stores';
3-
import { onDestroy } from 'svelte';
4-
5-
function getNavigationStore() {
6-
/** @type {((val?: any) => void)[]} */
7-
let callbacks = [];
8-
9-
const navigation = {
10-
...navigating,
11-
complete: async () => {
12-
await new Promise((res, _) => {
13-
callbacks.push(res);
14-
});
15-
}
16-
};
17-
18-
// This used to subscribe inside the callback, but that resolved the promise too early
19-
const unsub = navigating.subscribe((n) => {
20-
if (n === null) {
21-
while (callbacks.length > 0) {
22-
const res = callbacks.pop();
23-
res?.();
24-
}
25-
}
26-
});
27-
28-
onDestroy(() => {
29-
unsub();
30-
});
31-
32-
return navigation;
33-
}
1+
import { onNavigate } from '$app/navigation';
342

353
export const preparePageTransition = () => {
36-
const navigation = getNavigationStore();
37-
38-
// before navigating, start a new transition
39-
beforeNavigate(() => {
4+
onNavigate(async (navigation) => {
405
if (!document.startViewTransition) {
416
return;
427
}
43-
const navigationComplete = navigation.complete();
448

45-
document.startViewTransition(async () => {
46-
await navigationComplete;
9+
return new Promise((oldStateCaptureResolve) => {
10+
document.startViewTransition(async () => {
11+
oldStateCaptureResolve();
12+
await navigation.complete;
13+
});
4714
});
4815
});
4916
};

0 commit comments

Comments
 (0)