Skip to content

Commit d8c8bd7

Browse files
authored
fix: don't use fork of unrelated route (#14947)
We haven't been checking if the fork still matches the route we want to load at the end of the navigation. At that point it could've changed, or it could never have matched (as in the related issue). Fixes #14946
1 parent 94935df commit d8c8bd7

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

.changeset/dull-spies-hammer.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: don't use fork of unrelated route

packages/kit/src/runtime/client/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,9 @@ async function navigate({
16901690
}
16911691
}
16921692

1693+
// also compare ids to avoid using wrong fork (e.g. a new one could've been added while navigating)
1694+
const load_cache_fork = intent && load_cache?.id === intent.id ? load_cache.fork : null;
16931695
// reset preload synchronously after the history state has been set to avoid race conditions
1694-
const load_cache_fork = load_cache?.fork;
16951696
load_cache = null;
16961697

16971698
navigation_result.props.page.state = state;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>slow navigation</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>target</p>

packages/kit/test/apps/basics/test/client.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,21 @@ test.describe('data-sveltekit attributes', () => {
10471047
expect(page).toHaveURL('/data-sveltekit/preload-data/offline/slow-navigation');
10481048
});
10491049

1050+
test('data-sveltekit-preload does not abort ongoing navigation #2', async ({ page }) => {
1051+
await page.goto('/data-sveltekit/preload-data/offline');
1052+
1053+
await page.locator('#slow-navigation').dispatchEvent('click');
1054+
await page.waitForTimeout(100); // wait for navigation to start
1055+
await page.locator('#one').dispatchEvent('mousemove');
1056+
await Promise.all([
1057+
page.waitForTimeout(100), // wait for preloading to start
1058+
page.waitForLoadState('networkidle') // wait for preloading to finish
1059+
]);
1060+
1061+
expect(page).toHaveURL('/data-sveltekit/preload-data/offline/slow-navigation');
1062+
await expect(page.getByText('slow navigation', { exact: true })).toBeVisible();
1063+
});
1064+
10501065
test('data-sveltekit-preload-data tap works after data-sveltekit-preload-code hover', async ({
10511066
page
10521067
}) => {

0 commit comments

Comments
 (0)