Skip to content

Different behavior HMR .svelte and .svelte.js #14398

@dm-de

Description

@dm-de

Describe the bug

There is a difference in the behavior of Hot Module Reloading in Vite.

I'm not sure: is this a bug or not?

For example, if you edit below Component.svelte, it is also possible that App.svelte is reloaded and then you will lose the global state.

Note: examples will not show this behavior in Playground, because it is fully reloaded.
You need to setup simple vite installation with:
npm create vite@latest

Reproduction

EXAMPLE 1:

After editing Component.svelte:
App.svelte is reloaded and state is lost.
User-ID is 0 in Component

console:

GET http://localhost:5173/src/Component.svelte?t=1732219190118
GET http://localhost:5173/src/App.svelte?t=1732219190118

<!-- App.svelte -->
<script module>
  export let global = $state({
    user: {id:0},
  })
</script>

<script>
  import Component from './Component.svelte'
  import {onMount} from 'svelte'

  onMount(() => {
    global.user.id = 1
  })
</script>

<Component />





<!-- Component.svelte -->
<script>
  import {global} from './App.svelte'
  $inspect(global)
</script>

User ID: {global.user.id}

.
.
.

EXAMPLE 2:

After editing Component.svelte:
Only Component.svelte is reloaded and state is not lost.
User-ID is 1 in Component

console:

GET http://localhost:5173/src/Component.svelte?t=1732219722537

//global.svelte.js
export let global = $state({
    user: {id:0},
})





<!-- App.svelte -->
<script>
  import Component from './Component.svelte'
  import {onMount} from 'svelte'
  import {global} from './global.svelte.js'

  onMount(() => {
    global.user.id = 1
  })
</script>

<Component />





<!-- Component.svelte -->
<script>
  import {global} from './global.svelte.js'
  $inspect(global)
</script>

User ID: {global.user.id}

Logs

No response

System Info

Svelte 5.2.7

Severity

annoyance

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