Skip to content

Svelte 5: Dynamic Components do not update correctly after SSR #13517

Open
@frschi

Description

@frschi

Describe the bug

Dynamic components in Svelte 5 + Sveltekit do not update correctly after SSR. The state updates, but the rendered component doesn't reflect this change:

store.ts:

import { browser } from "$app/environment";

const someStore = () => {
  let someCondition = $state(false)

  if(browser){
    someCondition = true
  }

  return {
    get someCondition () {
      return someCondition
    }
  }
}

export const store = someStore()

+page.svelte:

<script lang="ts">
    import A from './A.svelte'
    import B from './B.svelte'
    import {store} from './store'

    $inspect(store.someCondition)

    const SelectedComponent = $derived(store.someCondition ? A : B)
</script>

<!-- should display A, but displays B -->
<SelectedComponent />

{store.someCondition}

$inspect() and {store.someCondition} both show true. SelectedComponent should render A, however it renders B.

its kinda like #12333 just for components.

with this workaround in the store.ts it correctly updates to A:

	if (browser) {
		setTimeout(() => {
			someCondition = true;
		}, 0);
	}

Reproduction

in the repl its working correctly. however locally in a fresh sveltekit + svelte 5 project its not.

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA4WSwW6DMBBEf2VrRQIkFO4IUkE-IcfSA8LryhWsLdskrRD_XhkISZo0PSEz4-edsQcmZIuWpW8Do7pDlrJCaxYz9639wh6xdchiZlVvGv8ns42R2u0qAgCQnVbGQQHCqA6CbVJs5y3BjV6uevlQH6xTBsfVNS0X5_bTBhXN9o0kq7Fx4WJQHe4VcemkouhsahRZBwdssXHI96rTipAc5LDhaOQR-aPt8AoFpFBGFWXJmrGi7B6UTMLwADJWxGLWKS6FRM5SZ3oc47Xa61iXjj_tdb8VzfN77sH7IYcwgnwHg4_X4ixd5s5hY13tMBR1a3FpQYrQ05WAkySuTvCS5xD0xFFIQh5Ew9zVb5Qf2CvjjDHoekOwmD_uzg6js7Z6bwyzNi7ECYpf040vIZeAa9gwet5g8fxp6pp2RZZM32eY8n9M-SfmffwB1kZLRjYDAAA=

Logs

No response

System Info

System:
    OS: Linux 6.5 Ubuntu 23.10 23.10 (Mantic Minotaur)
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 21.84 GB / 31.26 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.11.1 - /usr/local/lib/nodejs/node-v20.11.1-linux-x64/bin/node
    Yarn: 1.22.22 - /usr/local/lib/nodejs/node-v20.11.1-linux-x64/bin/yarn
    npm: 10.2.4 - /usr/local/lib/nodejs/node-v20.11.1-linux-x64/bin/npm
    bun: 1.1.30 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 129.1.70.123
    Chrome: 129.0.6668.89
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.0.0-next.262

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions