Skip to content

State issues with preloading and async svelte #14902

@nebwards

Description

@nebwards

Describe the bug

I understand there are some issues with recent svelte versions. I have a simple example for active links which highlights a bug with (what I assume to be) async svelte and/or preloading data.

Consider this +layout.svelte

<script lang="ts">
    import { page } from '$app/state';

    let { children } = $props();

    const links = ['/', '/page-1', '/page-2', '/page-3'];

    function isActivePath(path: string) {
        return page.url.pathname === path;
    }
</script>

<div>
    svelte page.url.pathname = {page.url.pathname}
</div>

<div>
    <nav>
        {#each links as link}
            <a href={link} data-active={isActivePath(link)}>{link}</a>
        {/each}
    </nav>
    {@render children()}
</div>

I would expect that when navigating to each of these pages, data-active becomes true on the respective anchor tag. However, instead I am seeing some odd behaviour:

  • If hovering on a link, then clicking it, without hovering over other links first, this all works as expected.
  • If hovering on a few different links, then clicking any one of them, data-active is no longer updated. Now navigating between the pages does not call isActivePath.

If async svelte is enabled, then removing data-sveltekit-preload-data="hover" 'fixes' these issues.
If aysnc svelte is not enabled, these issues do not appear.

All pages in this example simply contain an h1 tag - no async loading or remote functions.

In a more complex application, the actual page.url.pathname seems to be getting updated on hover preload aswell, and wrong pages are being loaded on clicking the links. I was not able to reproduce that in a simple example.

svelte_nav_bug.mp4

Reproduction

https://github.com/nebwards/svelte_nav_bug

Logs

System Info

System:
  OS: Windows 11 10.0.26100
  CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
  Memory: 5.73 GB / 15.93 GB
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  pnpm: 10.18.2
  bun: 1.3.0
Browsers:
  Edge: Chromium (140.0.3485.54)
  Internet Explorer: 11.0.26100.1882
npmPackages:
  @sveltejs/adapter-auto: ^7.0.0 => 7.0.0
  @sveltejs/kit: ^2.47.1 => 2.48.4
  @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1
  svelte: ^5.41.0 => 5.43.4
  vite: ^7.1.10 => 7.2.2

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions