kit: `$page.state` is lost after page refresh
Describe the bug
When refreshing a page (or submitting a form) with a PageState, the PageState is lost. However, when checking the history.state['sveltekit:states'], we see that the expected state is still attached to the current history entry.
Reproduction
The following +page.svelte demonstrates the issue:
<script lang="ts">
import { browser } from '$app/environment'
import { replaceState } from '$app/navigation'
import { page } from '$app/stores'
let historyState = browser && history.state['sveltekit:states']
</script>
<div>App PageState: {$page.state.filter}</div>
<div>History state: {JSON.stringify(historyState)}</div>
<hr />
<button on:click={() => replaceState('', { filter: 'banana' })}>Replace state with banana</button>
<button on:click={() => (historyState = history.state['sveltekit:states'])}>
Refresh from state.history
</button>
After initial loading the $page.state.filter is undefined
- Click on "Replace state with banana
=> PageState shows
{"filter":"banana"} - Click on “Refresh from state.history”
=> PageState and history state show
{"filter":"banana"} - Refresh the browser page
=> PageState shows
undefined - Click on “Refresh from state.history”
=> history state shows
{"filter":"banana"}but page state remains undefined
Logs
No response
System Info
System:
OS: Windows 11 10.0.26063
CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
Memory: 13.65 GB / 31.71 GB
Binaries:
Node: 21.1.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.6.7 - ~\AppData\Local\pnpm\pnpm.CMD
Browsers:
Chrome: 122.0.6261.112
Edge: Chromium (123.0.2420.10)
Internet Explorer: 11.0.26063.1
npmPackages:
@sveltejs/adapter-node: ^4.0.1 => 4.0.1
@sveltejs/kit: ^2.5.2 => 2.5.2
@sveltejs/vite-plugin-svelte: ^3.0.2 => 3.0.2
svelte: ^4.2.10 => 4.2.10
vite: ^5.1.2 => 5.1.2
Severity
serious, but I can work around it
Additional Information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 18
- Comments: 15 (9 by maintainers)
Consider one of the primary use cases that shallow routing was added to solve: Instagram-style navigation, where clicking on a photo on
/[user]opens a preview page but also updates the URL.If you reload the page, you’ll get a server rendered page for a different route —
/[user]/[photo]. The previous$page.statejust doesn’t apply here, and it could even be detrimental (depending on what you do with it). You certainly don’t want to navigate from/[user]/[photo]back to/[user](where the$page.statedoes apply) upon hydration, because that would cause flashing.And in general that applies to any use of
$page.state— because it’s not available during SSR, it would at minimum cause some flashing and weirdness if it was applied post-hydration.It’s definitely possible that there are some cases where you do want to apply state on hydration, flickering be damned. Perhaps an option would make sense for that:
I don’t think it should be the default though.
Just encountered this myself, took a while to debug. I just switched all of my modals to use shallow routing, as suggested by the docs. But because of this issue it seems like I’ll have to abandon this work and keep using stores instead, since some of my modals contain forms, which close the dialog when submitted. Additionally, hitting the back button after this happens does nothing, because it pops the state from the history, but since
$page.statewas already undefined, nothing happens.Additionally, I’ve encountered a bug where closing the model sometimes causes the whole page to go back. Which means that, sometimes,
$page.stateis set even when it shouldn’t be anymore (back navigation already happened). I’m not sure how to reproduce this but it seems to happen quite often.All in all, it seems that
$page.stateis generally out of sync withhistory.state.I believe an issue I have with this system is that the state isn’t completely reset on re-load, but I’m not sure if this is fixable:
This results in a state where the back button does nothing (it pops
history.state, but this doesn’t affect$page.stateas it was already). I don’t remember but I think then hitting forward does apply the state which wasn’t applied initially.This issue is critical for those who want to display the popup again upon refreshing, as it disregards the default state behavior of the browser. Is there any solution for this?
Going to keep this open while I don’t know the reason for
$page.statebeing reset on browser refresh https://github.com/sveltejs/kit/issues/11956#issuecomment-2004266264 However, for issues related to$page.statebeing lost after an enhanced form submission (which invokesinvalidateAll) see https://github.com/sveltejs/kit/issues/11783Workaround:
Use this instead of
$page.state. This correctly loads the state after a page reload and does not remove it on form submission. Additionally, it doesn’t update the store when the state doesn’t change but$pagewould update without the state changing.Warning: SvelteKit version
2.12.0broke this workaround, but this is fixed again in2.15.2. Generally using the$pagestore is broken in these versions. Make sure to upgrade to2.15.2+if this isn’t working for you, this took me a while to debug. Interestingly the first 1-2 navigations will work, only then it will break.The behavior of SSR hydrating should be a concern for developers implementing SSR, not an issue to be solved by blocking actual browser functionality. Isn’t it natural for SSR operations to ignore history.state by default? Why should the existence of SSR prevent the use of history.state in the browser entirely?
Do other frameworks have implementations that completely block basic browser functionality? I think it’s a serious problem for a framework to prevent the use of the browser’s basic functionality.
Chrome just added support for the new NavigationActivation api. This sounds like a accurate callback based api to get insights into the current
PageState. Alas, it is chrome only atm.Seems like this is noted as a caveat in the docs. I can’t explain why though. cc: @Rich-Harris