Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enabling $page.url store changes when URL is changed directly in address bar #9548

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions .changeset/great-kangaroos-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: Enables $page.url changes when URL is directly altered in address bar
GabrielBarbosaGV marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,16 @@ export function create_client(app, target) {
'',
location.href
);
} else {
// In the occasion of the hash being updated directly through
// the browser's address bar, the page store needs to be
// updated
GabrielBarbosaGV marked this conversation as resolved.
Show resolved Hide resolved
const url = new URL(location.href, document.baseURI);

current.url = url;

stores.page.set({ ...page, url });
stores.page.notify();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<div id="store-data">{JSON.stringify($page.data)}</div>
<div id="store-error">{$page.error?.message}</div>
<div id="page-url">{$page.url}</div>

<nav>
<a href="/store/data/xxx">xxx</a> <a href="/store/data/yyy">yyy</a>
Expand Down
20 changes: 20 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,26 @@ test.describe('$app/stores', () => {
expect(await page.textContent('#nav-status')).toBe('not currently navigating');
}
});

test("url's hash is updated when it is modified in the address bar", async ({
page,
javaScriptEnabled
}) => {
if (javaScriptEnabled) {
const baseUrl = '/store/data/www';
// Go to page where store data, as well as URL, are displayed
await page.goto(baseUrl);

// Change hash to #2 in address bar
await page.evaluate(() => {
history.pushState({}, '', '#2');
});

expect(await page.locator('#page-url').textContent({ timeout: 5000 })).toMatch(/#2$/, {
timeout: 5000
});
}
});
});

test.describe('searchParams', () => {
Expand Down