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

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

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

Loading…
Cancel
Save