diff --git a/.changeset/happy-eggs-rest.md b/.changeset/happy-eggs-rest.md new file mode 100644 index 0000000000..d3c39085f6 --- /dev/null +++ b/.changeset/happy-eggs-rest.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure each block consistency to internal mutations to the collection diff --git a/packages/svelte/src/internal/client/dom/blocks/each.js b/packages/svelte/src/internal/client/dom/blocks/each.js index 55bb474db4..67cb8df34e 100644 --- a/packages/svelte/src/internal/client/dom/blocks/each.js +++ b/packages/svelte/src/internal/client/dom/blocks/each.js @@ -225,6 +225,14 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f // continue in hydration mode set_hydrating(true); } + + // When we mount the each block for the first time, the collection won't be + // connected to this effect as the effect hasn't finished running yet and its deps + // won't be assigned. However, it's possible that when reconciling the each block + // that a mutation occurred and it's made the collection MAYBE_DIRTY, so reading the + // collection again can provide consistency to the reactive graph again as the deriveds + // will now be `CLEAN`. + get_collection(); }); if (hydrating) { diff --git a/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/_config.js b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/_config.js new file mode 100644 index 0000000000..c4325dcac2 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/_config.js @@ -0,0 +1,16 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, target }) { + const button = target.querySelector('button'); + + button?.click(); + flushSync(); + + assert.htmlEqual( + target.innerHTML, + `` + ); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/checkbox.svelte b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/checkbox.svelte new file mode 100644 index 0000000000..dff7d20e66 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/checkbox.svelte @@ -0,0 +1,7 @@ + + + diff --git a/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/main.svelte b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/main.svelte new file mode 100644 index 0000000000..08fcc01fb1 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/checkbox-binding-derived/main.svelte @@ -0,0 +1,28 @@ + + + + +{#each keys as key (key)} + +{/each}