Skip to content

Commit ac9b7de

Browse files
authored
fix: use strict equality for key block comparisons in runes mode (#14285)
fixes #14283
1 parent 25d9aa1 commit ac9b7de

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

.changeset/hot-frogs-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: use strict equality for key block comparisons in runes mode

packages/svelte/src/internal/client/dom/blocks/key.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/** @import { Effect, TemplateNode } from '#client' */
22
import { UNINITIALIZED } from '../../../../constants.js';
33
import { block, branch, pause_effect } from '../../reactivity/effects.js';
4-
import { safe_not_equal } from '../../reactivity/equality.js';
4+
import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
5+
import { is_runes } from '../../runtime.js';
56
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
67

78
/**
@@ -24,8 +25,10 @@ export function key_block(node, get_key, render_fn) {
2425
/** @type {Effect} */
2526
var effect;
2627

28+
var changed = is_runes() ? not_equal : safe_not_equal;
29+
2730
block(() => {
28-
if (safe_not_equal(key, (key = get_key()))) {
31+
if (changed(key, (key = get_key()))) {
2932
if (effect) {
3033
pause_effect(effect);
3134
}

packages/svelte/src/internal/client/reactivity/equality.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export function safe_not_equal(a, b) {
1515
: a !== b || (a !== null && typeof a === 'object') || typeof a === 'function';
1616
}
1717

18+
/**
19+
* @param {unknown} a
20+
* @param {unknown} b
21+
* @returns {boolean}
22+
*/
23+
export function not_equal(a, b) {
24+
return a !== b;
25+
}
26+
1827
/** @type {Equals} */
1928
export function safe_equals(value) {
2029
return !safe_not_equal(value, this.v);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
assert.deepEqual(logs, ['rendering']);
7+
8+
const btn = target.querySelector('button');
9+
flushSync(() => btn?.click());
10+
11+
assert.deepEqual(logs, ['rendering']);
12+
}
13+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
let inner = Symbol();
3+
let outer = $state({ inner });
4+
</script>
5+
6+
<button onclick={() => outer = { inner }}>update</button>
7+
8+
{#key outer.inner}
9+
{console.log('rendering')}
10+
{/key}

0 commit comments

Comments
 (0)