Thanks to visit codestin.com
Credit goes to github.com

Skip to content

$page.state is lost after page refresh #11956

Open
@mpost

Description

@mpost

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

  1. Click on "Replace state with banana
    => PageState shows {"filter":"banana"}
  2. Click on "Refresh from state.history"
    => PageState and history state show {"filter":"banana"}
  3. Refresh the browser page
    => PageState shows undefined
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions