sveltejs / sveltejs/kit

pushState then data-sveltekit-replacestate breaks the back button

Open
#14,112 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

router
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

I have a modal which has links towards other pages.


// first-page/+page.svelte

<script>
	import { pushState } from "$app/navigation";
	import { page } from "$app/state";
	function showPopup() {
		pushState("", { showPopup: true });
	}
</script>

<button onclick={showPopup}>show popup</button>

{#if page.state.showPopup}
	<a href="/second-page">go to second page</a>
{/if}

This works fine, I correctly land in /second-page. When I go back with my browser's back button, I'm correctly redirected to /first-page, but the popup is still open while I need it to be closed.


I thought data-sveltekit-replacestate would solve my issue:

<a data-sveltekit-replacestate href="/second-page">go to second page</a>

But once on /second-page, after going back, the URL change to /first-page but the page doesn't load: the content of /second-page is still shown.


data-sveltekit-reload works as expected but the UX isn't great.


Is this behaviour expected? If so, is this shallow navigation actually doable without a full page reload?

Reproduction

See https://stackblitz.com/edit/sveltejs-kit-template-default-6v3iubbw?file=src%2Froutes%2F%2Bpage.svelte

Logs

System Info
System:
    OS: macOS 15.5
    CPU: (10) arm64 Apple M4
    Memory: 89.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.4.0 - /opt/homebrew/bin/node
    pnpm: 10.13.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 138.0.7204.184
  npmPackages:
    @sveltejs/adapter-auto: ^6.0.0 => 6.0.1 
    @sveltejs/adapter-static: ^3.0.8 => 3.0.8 
    @sveltejs/kit: ^2.16.0 => 2.26.1 
    @sveltejs/vite-plugin-svelte: ^6.0.0 => 6.1.0 
    svelte: ^5.25.0 => 5.37.0 
    vite: ^7.0.0 => 7.0.6
Severity

annoyance

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and reproduce the sequence using pushState, the second-page link, data-sveltekit-replacestate, and the browser back button. Trace the SvelteKit navigation and history behavior involved; done means back navigation restores /first-page with the popup closed and renders the correct page without requiring a full reload.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.