Skip to content

Commit

Permalink
[fix] route invalidation comparison based on url and route, not on id (
Browse files Browse the repository at this point in the history
  • Loading branch information
Algoinde committed Jan 9, 2023
1 parent ac082cf commit aa7f700
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-hairs-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix route id comparison for load change detection
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ export function create_client({ target, base }) {
let server_data = null;

const url_changed = current.url ? id !== current.url.pathname + current.url.search : false;
const route_changed = current.route ? id !== current.route.id : false;
const route_changed = current.route ? route.id !== current.route.id : false;

const invalid_server_nodes = loaders.reduce((acc, loader, i) => {
const previous = current.branch[i];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('./$types').LayoutLoad} */
export function load({ route }) {
return { route };
return { route, random: Math.random() };
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
</script>

<h1>route.id: {data.route.id}</h1>
<h2>random id: {data.random}</h2>
<slot />

<a href="/load/invalidation/route/shared/a">/load/invalidation/route/shared/a</a>
<a href="/load/invalidation/route/shared/b">/load/invalidation/route/shared/b</a>

<a href="/load/invalidation/route/shared/unchanged-x">/load/invalidation/route/shared/unchanged-x</a>
<a href="/load/invalidation/route/shared/unchanged-y">/load/invalidation/route/shared/unchanged-y</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>[x]</p>
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,16 @@ test.describe('Invalidation', () => {
expect(await page.textContent('h1')).toBe('route.id: /load/invalidation/route/shared/b');
});

test('route.id does not rerun layout if unchanged', async ({ page, clicknav }) => {
await page.goto('/load/invalidation/route/shared/unchanged-x');
expect(await page.textContent('h1')).toBe('route.id: /load/invalidation/route/shared/[x]');
const id = await page.textContent('h2');

await clicknav('[href="/load/invalidation/route/shared/unchanged-y"]');
expect(await page.textContent('h1')).toBe('route.id: /load/invalidation/route/shared/[x]');
expect(await page.textContent('h2')).toBe(id);
});

test('$page.url can safely be mutated', async ({ page }) => {
await page.goto('/load/mutated-url?q=initial');
await expect(page.getByText('initial')).toBeVisible();
Expand Down

0 comments on commit aa7f700

Please sign in to comment.