Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-emus-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure `$effect.root` is ignored on the server
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/tests/runtime-legacy/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -285,6 +285,7 @@ async function run_test_variant(

if (config.test_ssr) {
await config.test_ssr({
logs,
// @ts-expect-error
assert: {
...assert,
Expand Down
Original file line number Diff line number Diff line change
@@ -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, []);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
$effect.root(() => {
console.log('effect1');
});
const cleanup = $effect.root(() => {
console.log('effect2');
});
</script>

<button onclick={cleanup}>cleanup</button>
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ export default test({
});

assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
},
test_ssr({ assert, logs }) {
assert.deepEqual(logs, []);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

function setStore() {
store = writable(0, () => {
console.log('start');
return () => console.log('stop');
return () => {};
});
}
</script>
Expand Down
Loading