mirror of https://github.com/sveltejs/svelte
fix: distinct memoizer on style/class directives (#18466)
Fix #18465 The style and classe directives are generated using an unique memoizer for all the class/style directive, which means if you have multiple of the same time on one element you have overly broad invalidation/reruns. This fixes it by having one memoizer call per directive instead of collecting them. --------- Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com> Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>pull/18685/head
parent
a1ec9c8eb7
commit
3405b5e735
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: distinct memoizer on style/class directives
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
// This test counts mutations on hydration
|
||||||
|
// set_style() should not mutate style on hydration, except if mismatch
|
||||||
|
export default test({
|
||||||
|
mode: ['server', 'hydrate', 'client'],
|
||||||
|
|
||||||
|
ssrHtml: `
|
||||||
|
<button>click</button>
|
||||||
|
<div class="red"></div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
html: `
|
||||||
|
<button>click</button>
|
||||||
|
<div class="red"></div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
test({ target, assert, logs }) {
|
||||||
|
flushSync();
|
||||||
|
|
||||||
|
assert.deepEqual(logs, ['is_red()']);
|
||||||
|
|
||||||
|
const btn = target.querySelector('button');
|
||||||
|
const div = target.querySelector('div');
|
||||||
|
|
||||||
|
assert.equal(div?.className, 'red');
|
||||||
|
|
||||||
|
btn?.click();
|
||||||
|
|
||||||
|
flushSync();
|
||||||
|
|
||||||
|
assert.equal(div?.className, 'red active');
|
||||||
|
// only one call here
|
||||||
|
assert.deepEqual(logs, ['is_red()']);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<script>
|
||||||
|
function is_red() {
|
||||||
|
console.log("is_red()");
|
||||||
|
return "red";
|
||||||
|
}
|
||||||
|
|
||||||
|
let active = $state(false);
|
||||||
|
</script>
|
||||||
|
<button onclick={() => active = true}>click</button>
|
||||||
|
<div class:red={is_red()} class:active></div>
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
// This test counts mutations on hydration
|
||||||
|
// set_style() should not mutate style on hydration, except if mismatch
|
||||||
|
export default test({
|
||||||
|
mode: ['server', 'hydrate', 'client'],
|
||||||
|
|
||||||
|
ssrHtml: `
|
||||||
|
<button>click</button>
|
||||||
|
<div style="background-color: red; font-size: 1em;"></div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
html: `
|
||||||
|
<button>click</button>
|
||||||
|
<div style="background-color: red; font-size: 1em;"></div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
test({ target, assert, logs }) {
|
||||||
|
flushSync();
|
||||||
|
|
||||||
|
assert.deepEqual(logs, ['makeColor()']);
|
||||||
|
|
||||||
|
const btn = target.querySelector('button');
|
||||||
|
const div = target.querySelector('div');
|
||||||
|
|
||||||
|
// Note : we cannot compare HTML because set_style() use dom.style.cssText
|
||||||
|
// which can alter the format of the attribute...
|
||||||
|
|
||||||
|
assert.equal(div?.style.backgroundColor, 'red');
|
||||||
|
assert.equal(div?.style.fontSize, '1em');
|
||||||
|
|
||||||
|
btn?.click();
|
||||||
|
|
||||||
|
flushSync();
|
||||||
|
|
||||||
|
assert.equal(div?.style.backgroundColor, 'red');
|
||||||
|
assert.equal(div?.style.fontSize, '2em');
|
||||||
|
// only one call here
|
||||||
|
assert.deepEqual(logs, ['makeColor()']);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<script>
|
||||||
|
function makeColor() {
|
||||||
|
console.log("makeColor()");
|
||||||
|
return "red";
|
||||||
|
}
|
||||||
|
|
||||||
|
let size = $state('1em');
|
||||||
|
</script>
|
||||||
|
<button onclick={() => size = '2em'}>click</button>
|
||||||
|
<div style:background-color={makeColor()} style:font-size={size}></div>
|
||||||
Loading…
Reference in new issue