Repository: sveltejs/kit (js)

Issue:
$page store not correctly updating
### Describe the bug

When watching for url changes the store seems to stop being reactive after a short while.

For example in my repro, i have a link that appends `#modal` to the url, i then have a modal component using the page store to watch this change and show some html, initially it works as expected but after opening then closing then opening then closing again that last close stops working.

There are no errors

### Reproduction

https://github.com/mattpilott/sveltekit-page-store-issue

### Logs

_No response_

### System Info

```shell
System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 4.57 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.6.0 - /opt/homebrew/bin/node
    npm: 8.15.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0.1
    Safari: 16.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.63 
    @sveltejs/kit: next => 1.0.0-next.392 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.0 => 3.0.2
```


### Severity

serious, but I can work around it

### Additional Information

# How to repro
1. Clone repo `git clone`
2. Install deps `npm i`
3. Run dev server `npm run dev`
4. Open browser `http://localhost:5173`
5. Click link `Open Modal`
6. Click link `Close me`
7. Click link `Open Modal` again
8. Click link `Close me` again
9. Issue will happen `modal wont close even though url is updated`


Pull request:
[fix] update current.url on hashchange
Fixes #5693

### Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check`

### Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. All changesets should be `patch` until SvelteKit 1.0


Patch:
diff --git a/.changeset/funny-cycles-travel.md b/.changeset/funny-cycles-travel.md
new file mode 100644
index 000000000..80f8a991c
--- /dev/null
+++ b/.changeset/funny-cycles-travel.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+[fix] update current.url on hashchange
diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js
index 09be6d707..9cfcf0b3e 100644
--- a/packages/kit/src/runtime/client/client.js
+++ b/packages/kit/src/runtime/client/client.js
@@ -1321,10 +1321,12 @@ export function create_client({ target, base, trailing_slash }) {
 				if (hash !== undefined && base === location.href.split('#')[0]) {
 					// set this flag to distinguish between navigations triggered by
 					// clicking a hash link and those triggered by popstate
+					// TODO why not update history here directly?
 					hash_navigating = true;
 
 					update_scroll_positions(current_history_index);
 
+					current.url = url;
 					stores.page.set({ ...page, url });
 					stores.page.notify();
 


Test patch:
diff --git a/packages/kit/test/apps/basics/src/routes/routing/hashes/pagestore/+page.svelte b/packages/kit/test/apps/basics/src/routes/routing/hashes/pagestore/+page.svelte
index 4a20ec56a..630589a9d 100644
--- a/packages/kit/test/apps/basics/src/routes/routing/hashes/pagestore/+page.svelte
+++ b/packages/kit/test/apps/basics/src/routes/routing/hashes/pagestore/+page.svelte
@@ -17,5 +17,6 @@
 <h1 id="window-hash">{hash}</h1>
 <h1 id="page-url-hash">{$page.url.hash}</h1>
 
-<a href="#target">Nav to #ing with anchor tag</a>
+<a href="#target">Nav to hash</a>
+<a href="/routing/hashes/pagestore">Nav to page</a>
 <div id="target">Target</div>
diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js
index 1c1237820..d581e34ed 100644
--- a/packages/kit/test/apps/basics/test/client.test.js
+++ b/packages/kit/test/apps/basics/test/client.test.js
@@ -551,6 +551,9 @@ test.describe('Routing', () => {
 		await page.click('[href="#target"]');
 		expect(await page.textContent('#window-hash')).toBe('#target');
 		expect(await page.textContent('#page-url-hash')).toBe('#target');
+		await page.click('[href="/routing/hashes/pagestore"]');
+		await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
+		await expect(page.locator('#page-url-hash')).toHaveText('');
 	});
 
 	test('does not normalize external path', async ({ page }) => {