fix: ensure `$effect.root` is ignored on the server (#12332)

Ignore the contents of the effect root, just return a noop where necessary

fixes #12322
pull/12340/head
Simon H 6 months ago committed by GitHub
parent cf16acda32
commit 243c4b78b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure `$effect.root` is ignored on the server

@ -412,11 +412,8 @@ const global_visitors = {
}
if (rune === '$effect.root') {
const args = /** @type {import('estree').Expression[]} */ (
node.arguments.map((arg) => context.visit(arg))
);
// Just call the function directly
return b.call(args[0]);
// ignore $effect.root() calls, just return a noop which mimics the cleanup function
return b.arrow([], b.block([]));
}
if (rune === '$state.snapshot') {

@ -61,7 +61,7 @@ export interface RuntimeTest<Props extends Record<string, any> = Record<string,
warnings: any[];
hydrate: Function;
}) => void | Promise<void>;
test_ssr?: (args: { assert: Assert }) => void | Promise<void>;
test_ssr?: (args: { logs: any[]; assert: Assert }) => void | Promise<void>;
accessors?: boolean;
immutable?: boolean;
intro?: boolean;
@ -285,6 +285,7 @@ async function run_test_variant(
if (config.test_ssr) {
await config.test_ssr({
logs,
// @ts-expect-error
assert: {
...assert,

@ -0,0 +1,18 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
html: '<button>cleanup</button>',
async test({ assert, target, logs }) {
const btn = target.querySelector('button');
btn?.click();
flushSync();
assert.deepEqual(logs, ['effect1', 'effect2']);
},
test_ssr({ assert, logs }) {
assert.deepEqual(logs, []);
}
});

@ -0,0 +1,10 @@
<script>
$effect.root(() => {
console.log('effect1');
});
const cleanup = $effect.root(() => {
console.log('effect2');
});
</script>
<button onclick={cleanup}>cleanup</button>

@ -24,5 +24,8 @@ export default test({
});
assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
},
test_ssr({ assert, logs }) {
assert.deepEqual(logs, []);
}
});

@ -5,8 +5,7 @@
function setStore() {
store = writable(0, () => {
console.log('start');
return () => console.log('stop');
return () => {};
});
}
</script>

Loading…
Cancel
Save