Skip to content
Closed
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/rich-dryers-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: update `page` store when `pushState` or `replaceState` is called
12 changes: 8 additions & 4 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1876,10 +1876,12 @@ export function pushState(url, state) {
[STATES_KEY]: state
};

history.pushState(opts, '', resolve_url(url));
const url_object = resolve_url(url);

history.pushState(opts, '', url_object);
has_navigated = true;

page = { ...page, state };
page = { ...page, state, url: url_object, route: { id: url_object.pathname } };
root.$set({ page });

clear_onward_history(current_history_index, current_navigation_index);
Expand Down Expand Up @@ -1914,9 +1916,11 @@ export function replaceState(url, state) {
[STATES_KEY]: state
};

history.replaceState(opts, '', resolve_url(url));
const url_object = resolve_url(url);

history.replaceState(opts, '', url_object);

page = { ...page, state };
page = { ...page, state, url: url_object, route: { id: url_object.pathname } };
root.$set({ page });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<button data-id="invalidate" on:click={invalidateAll}>invalidate all</button>

<p>active: {$page.state.active ?? false}</p>
<div class="route_id">route.id: {$page.route.id}</div>
<span>{data.now}</span>
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
<button data-id="two" on:click={two}>replace state on child page</button>

<p>active: {$page.state.active ?? false}</p>
<div class="route_id">route.id: {$page.route.id}</div>
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 @@ -1045,11 +1045,15 @@ test.describe('Shallow routing', () => {
test('Pushes state to a new URL', async ({ baseURL, page }) => {
await page.goto('/shallow-routing/push-state');
await expect(page.locator('p')).toHaveText('active: false');
await expect(page.locator('div.route_id')).toHaveText('route.id: /shallow-routing/push-state');

await page.locator('[data-id="two"]').click();
expect(page.url()).toBe(`${baseURL}/shallow-routing/push-state/a`);
await expect(page.locator('h1')).toHaveText('parent');
await expect(page.locator('p')).toHaveText('active: true');
await expect(page.locator('div.route_id')).toHaveText(
'route.id: /shallow-routing/push-state/a'
);

await page.reload();
await expect(page.locator('h1')).toHaveText('a');
Expand Down Expand Up @@ -1115,8 +1119,14 @@ test.describe('Shallow routing', () => {
await page.goto('/shallow-routing/replace-state/b');
await clicknav('[href="/shallow-routing/replace-state"]');

await expect(page.locator('div.route_id')).toHaveText(
'route.id: /shallow-routing/replace-state'
);
await page.locator('[data-id="two"]').click();
await expect(page.locator('p')).toHaveText('active: true');
await expect(page.locator('div.route_id')).toHaveText(
'route.id: /shallow-routing/replace-state/a'
);

await page.goBack();
expect(page.url()).toBe(`${baseURL}/shallow-routing/replace-state/b`);
Expand Down