Describe the bug
When manually setting the query parameters in the URL using history, the $page store does not update correctly to reflect these changes.
In my SvelteKit application, I have a function that updates the query parameters in the URL using the history API. However, after updating the query parameters, the $page store does not reflect these changes. This behavior is inconsistent with the expected behavior, where the $page store should update to reflect the current state of the page, including the query parameters.
I also attempted to use goto to work around this, but the issue in my application is that the URL search params are being updated from a search input, and when you goto, the focus is lost on the input. Manually handling refocusing the input is possible, but quite the hassle.
My current workaround is to not rely on $page.url, and only use window.location.
Reproduction
Here is a repo you can clone so you can reproduce the issue: https://github.com/jhwheeler/sveltekit-search-params-issue
In this example, when you click the "Update Query Params" button, it updates the query parameters in the URL using the history API. However, the $page store does not update correctly to reflect these changes:
<script>
import { page } from '$app/stores'
import { browser } from '$app/environment'
let queryParams = ''
function updateQueryParams() {
queryParams = 'newParams'
const params = new URLSearchParams(window.location.search)
params.set('q', queryParams)
history.replaceState(
history.state,
'',
decodeURIComponent(`${window.location.pathname}?${params}`),
)
}
$: currentSearchParams = browser ? Object.fromEntries($page.url.searchParams) : null
$: console.log('currentSearchParams', currentSearchParams)
</script>
<button on:click={updateQueryParams}>Update Query Params</button>
<p>Query Params: {queryParams}</p>
{#if browser}
<p>currentSearchParams: {JSON.stringify(currentSearchParams)}</p>
{/if}
Here is a GIF of the output:

System Info
System:
OS: macOS 13.2.1
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 2.10 GB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.12.0 - ~/.nvm/versions/node/v18.12.0/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.0/bin/npm
Browsers:
Brave Browser: 116.1.57.57
Chrome: 116.0.5845.140
Edge: 113.0.1774.57
Safari: 16.3
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.1.0
@sveltejs/kit: ^1.20.4 => 1.24.0
svelte: ^4.0.5 => 4.2.0
vite: ^4.4.2 => 4.4.9
Severity
serious, but I can work around it
Describe the bug
When manually setting the query parameters in the URL using history, the
$pagestore does not update correctly to reflect these changes.In my SvelteKit application, I have a function that updates the query parameters in the URL using the
historyAPI. However, after updating the query parameters, the $page store does not reflect these changes. This behavior is inconsistent with the expected behavior, where the$pagestore should update to reflect the current state of the page, including the query parameters.I also attempted to use
gototo work around this, but the issue in my application is that the URL search params are being updated from a search input, and when yougoto, the focus is lost on the input. Manually handling refocusing the input is possible, but quite the hassle.My current workaround is to not rely on
$page.url, and only usewindow.location.Reproduction
Here is a repo you can clone so you can reproduce the issue: https://github.com/jhwheeler/sveltekit-search-params-issue
In this example, when you click the "Update Query Params" button, it updates the query parameters in the URL using the history API. However, the
$pagestore does not update correctly to reflect these changes:Here is a GIF of the output:
System Info
System: OS: macOS 13.2.1 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 2.10 GB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 18.12.0 - ~/.nvm/versions/node/v18.12.0/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 8.19.2 - ~/.nvm/versions/node/v18.12.0/bin/npm Browsers: Brave Browser: 116.1.57.57 Chrome: 116.0.5845.140 Edge: 113.0.1774.57 Safari: 16.3 npmPackages: @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 @sveltejs/kit: ^1.20.4 => 1.24.0 svelte: ^4.0.5 => 4.2.0 vite: ^4.4.2 => 4.4.9Severity
serious, but I can work around it