Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the id, which serves as an unique identifier for caching from what I see, will always be an URL-type thing?

Copy link
Member

@dummdidumm dummdidumm Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the id in that instance (as its comment on hover suggests) is url.pathname + url.search - you can backtrack that variable to get_navigation_intent where it's computed as such

Copy link
Contributor Author

@Algoinde Algoinde Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know it is currently pathname + search - I'm just considering:

  • id name is closer to "route id" than to "URL", which it currently represents in essence - I think that's what caused the initial oversight
  • we are not sure if down the line that id will change to something else more specific, breaking this comparison once again.

So I opted to shed that id dependency - especially because url is still there being returned for its intended purpose, while id's purpose is something else entirely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that id is probably a misleading name in this instance because of route.id, but that id cannot change during the cause of a route change (only if another route change is triggered as a result of it, at which point a new id will be computed). I think we can keep it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I meant "change" as in "someone changes what is computed inside get_navigation_intent at some point due to a caching requirement change and overlooks this comparison due to destructuring, commits and it's borken again".

Thanks for merging!

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