Skip to content

"Cannot call replaceState(...) before router is initialized" in $effect #15927

Description

@al3xnag

Describe the bug

It's a bit counter-intuitive that router is still not initialized inside $effect, when the router actually has already resolved the route and rendered the page and components.

// src/routes/+page.svelte
<script>
  import { replaceState } from '$app/navigation';

  $effect(() => {
    replaceState('#asd', {});
  });
</script>
Uncaught (in promise) Error: Cannot call replaceState(...) before router is initialized
  in $effect
  in +page.svelte
  in +layout.svelte
  in root.svelte
  in undefined

Reproduction

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

Logs

System Info

System:
    OS: macOS 26.4.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 136.81 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0
    Yarn: 1.22.22
    npm: 10.9.2
  Browsers:
    Chrome: 148.0.7778.216
    Firefox: 151.0.2
    Safari: 26.4
  npmPackages:
    @sveltejs/adapter-auto: ^7.0.1 => 7.0.1
    @sveltejs/adapter-static: ^3.0.10 => 3.0.10
    @sveltejs/kit: ^2.57.0 => 2.61.1
    @sveltejs/vite-plugin-svelte: ^7.0.0 => 7.1.2
    svelte: ^5.55.2 => 5.55.9
    vite: ^8.0.7 => 8.0.14

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions