Skip to content

Svelte 4/5: Children/slot rerenders DOM when moved #12895

Open
@Azarattum

Description

@Azarattum

Describe the bug

When doing a conditional wrapping like so:

{#if wrap}
  <div>
    {@render children()}
  </div>
{:else}
  {@render children()}
{/if}

When the wrap variable changes, all the children are rerendered. Meaning all the DOM nodes inside are recreated, even though they themselves have not changed. This could cause performance issues and inconsistency. For example, it triggers all the transitions again. It would be really nice if when children/slot moves in markup, Svelte would also move the corresponding DOM node instead of creating a new one (which is exactly the same).

I would like to know if there is currently any workaround to achieve the conditional wrapping without rerendering. It would be nice if there is any way to prevent transitions from triggering at least. This is what broke my app and made me discover this behavior.

#7528 related

Reproduction

Svelte 5 REPL

In the REPL it is clear that Math.random() reruns every time wrap is changed. The in:fly transition is triggered every time as well. Also you can check the console to verify that div with id node is different on every instantiation.

Logs

No response

System Info

System:
    OS: macOS 14.2.1
    CPU: (8) arm64 Apple M1
    Memory: 127.20 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.3.0 - /opt/homebrew/bin/node
    npm: 10.8.1 - /opt/homebrew/bin/npm
    pnpm: 9.4.0 - ~/Library/pnpm/pnpm
    bun: 1.1.21 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 127.1.68.137
    Safari: 17.2.1

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