Skip to content

Commit e5f8065

Browse files
[fix] Apply updated stuff (and other page properties) when get_navigation_result_from_branch called (#4392)
* add test * apply updated props.page when update / goto page * add changeset * fix lint * fix formatting * update page store more conservatively Co-authored-by: Rich Harris <[email protected]>
1 parent f445802 commit e5f8065

File tree

7 files changed

+86
-13
lines changed

7 files changed

+86
-13
lines changed
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+
apply updated `props.page` when update or goto page

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const routes = parse(components, dictionary, matchers);
2727
const default_layout = components[0]();
2828
const default_error = components[1]();
2929

30+
const root_stuff = {};
31+
3032
// We track the scroll position associated with each history entry in sessionStorage,
3133
// rather than on history.state itself, because when navigation is driven by
3234
// popstate it's too late to update the scroll position associated with the
@@ -86,10 +88,12 @@ export function create_client({ target, session, base, trailing_slash }) {
8688

8789
/** @type {import('./types').NavigationState} */
8890
let current = {
89-
// @ts-ignore - we need the initial value to be null
90-
url: null,
91+
branch: [],
92+
error: null,
9193
session_id: 0,
92-
branch: []
94+
stuff: root_stuff,
95+
// @ts-ignore - we need the initial value to be null
96+
url: null
9397
};
9498

9599
let started = false;
@@ -364,7 +368,7 @@ export function create_client({ target, session, base, trailing_slash }) {
364368
* stuff: Record<string, any>;
365369
* branch: Array<import('./types').BranchNode | undefined>;
366370
* status: number;
367-
* error?: Error;
371+
* error: Error | null;
368372
* routeId: string | null;
369373
* }} opts
370374
*/
@@ -387,6 +391,8 @@ export function create_client({ target, session, base, trailing_slash }) {
387391
url,
388392
params,
389393
branch,
394+
error,
395+
stuff,
390396
session_id
391397
},
392398
props: {
@@ -399,7 +405,13 @@ export function create_client({ target, session, base, trailing_slash }) {
399405
result.props[`props_${i}`] = loaded ? await loaded.props : null;
400406
}
401407

402-
if (!current.url || url.href !== current.url.href) {
408+
const page_changed =
409+
!current.url ||
410+
url.href !== current.url.href ||
411+
current.error !== error ||
412+
current.stuff !== stuff;
413+
414+
if (page_changed) {
403415
result.props.page = { error, params, routeId, status, stuff, url };
404416

405417
// TODO remove this for 1.0
@@ -583,14 +595,14 @@ export function create_client({ target, session, base, trailing_slash }) {
583595
let branch = [];
584596

585597
/** @type {Record<string, any>} */
586-
let stuff = {};
598+
let stuff = root_stuff;
587599
let stuff_changed = false;
588600

589601
/** @type {number | undefined} */
590602
let status = 200;
591603

592-
/** @type {Error | undefined} */
593-
let error;
604+
/** @type {Error | null} */
605+
let error = null;
594606

595607
// preload modules
596608
a.forEach((loader) => loader());

packages/kit/src/runtime/client/types.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ export type BranchNode = {
6868
};
6969

7070
export type NavigationState = {
71-
url: URL;
72-
params: Record<string, string>;
7371
branch: Array<BranchNode | undefined>;
72+
error: Error | null;
73+
params: Record<string, string>;
7474
session_id: number;
75+
stuff: Record<string, any>;
76+
url: URL;
7577
};

packages/kit/test/apps/basics/src/routes/store/stuff/__error.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@
99
};
1010
}
1111
</script>
12+
13+
<script>
14+
import { page } from '$app/stores';
15+
import { goto } from '$app/navigation';
16+
</script>
17+
18+
<button id="reload-button" on:click={() => goto($page.url.toString())}>Reload</button>

packages/kit/test/apps/basics/src/routes/store/stuff/__layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515

1616
<div id="store-stuff">{JSON.stringify($page.stuff)}</div>
1717

18-
<nav><a href="/store/stuff/xxx">xxx</a> <a href="/store/stuff/yyy">yyy</a> <a href="/store/stuff/zzz">zzz</a></nav>
18+
<nav><a href="/store/stuff/xxx">xxx</a> <a href="/store/stuff/yyy">yyy</a> <a href="/store/stuff/zzz">zzz</a> <a href="/store/stuff/foo">foo</a></nav>
1919

2020
<slot />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script context="module">
2+
let is_first = true;
3+
4+
export function load() {
5+
if (is_first) {
6+
is_first = false;
7+
throw new Error('uh oh');
8+
}
9+
10+
return {
11+
stuff: {
12+
foo: true
13+
},
14+
props: {
15+
number: 2
16+
}
17+
};
18+
}
19+
</script>
20+
21+
<script>
22+
export let number;
23+
</script>
24+
25+
<h1>stuff - foo</h1>
26+
<p>Number prop: {number}</p>
27+
<a href="/store">store</a>

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,11 +1590,11 @@ test.describe.parallel('$app/stores', () => {
15901590
await page.goto('/store');
15911591

15921592
expect(await page.textContent('h1')).toBe('Test');
1593-
expect(await page.textContent('h2')).toBe('Calls: 1');
1593+
expect(await page.textContent('h2')).toBe(javaScriptEnabled ? 'Calls: 2' : 'Calls: 1');
15941594

15951595
await clicknav('a[href="/store/result"]');
15961596
expect(await page.textContent('h1')).toBe('Result');
1597-
expect(await page.textContent('h2')).toBe(javaScriptEnabled ? 'Calls: 1' : 'Calls: 0');
1597+
expect(await page.textContent('h2')).toBe(javaScriptEnabled ? 'Calls: 2' : 'Calls: 0');
15981598

15991599
const oops = await page.evaluate(() => window.oops);
16001600
expect(oops).toBeUndefined();
@@ -1623,6 +1623,26 @@ test.describe.parallel('$app/stores', () => {
16231623
);
16241624
});
16251625

1626+
test('should load stuff after reloading by goto', async ({
1627+
page,
1628+
clicknav,
1629+
javaScriptEnabled
1630+
}) => {
1631+
const stuff1 = JSON.stringify({ name: 'SvelteKit', value: 789, error: 'uh oh' });
1632+
const stuff2 = JSON.stringify({ name: 'SvelteKit', value: 123, foo: true });
1633+
await page.goto('/store/stuff/www');
1634+
1635+
await clicknav('a[href="/store/stuff/foo"]');
1636+
expect(await page.textContent('#store-stuff')).toBe(stuff1);
1637+
1638+
await clicknav('#reload-button');
1639+
expect(await page.textContent('#store-stuff')).toBe(javaScriptEnabled ? stuff2 : stuff1);
1640+
1641+
await clicknav('a[href="/store/stuff/zzz"]');
1642+
await clicknav('a[href="/store/stuff/foo"]');
1643+
expect(await page.textContent('#store-stuff')).toBe(stuff2);
1644+
});
1645+
16261646
test('navigating store contains from and to', async ({ app, page, javaScriptEnabled }) => {
16271647
await page.goto('/store/navigating/a');
16281648

0 commit comments

Comments
 (0)