Skip to content
Open
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/common-boats-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent reactivity loss during fork
20 changes: 20 additions & 0 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export class Batch {

is_fork = false;

/**
* Branches that had their CLEAN flag toggled during fork execution
* @type {Set<Effect> | null}
*/
toggled_branches = null;

is_deferred() {
return this.is_fork || this.#blocking_pending > 0;
}
Expand Down Expand Up @@ -857,6 +863,12 @@ export function schedule_effect(signal) {
if ((flags & (ROOT_EFFECT | BRANCH_EFFECT)) !== 0) {
if ((flags & CLEAN) === 0) return;
effect.f ^= CLEAN;

// Track branches toggled during fork execution so we can restore
// their CLEAN flag after flush
if (current_batch !== null && current_batch.is_fork) {
(current_batch.toggled_branches ??= new Set()).add(effect);
}
Comment on lines +867 to +871
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense...there's only one thing that it kinda irks me here: we are restoring ALL the branches to CLEAN but wouldn't this lead to over-running?

Copy link
Contributor Author

@hmnd hmnd Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't think that's a concern because we only do this for branches that had CLEAN before the fork. I'm pretty sure setting CLEAN doesn't necessarily mean branches will run either, it just allows them to be scheduled.

}
}

Expand Down Expand Up @@ -959,6 +971,14 @@ export function fork(fn) {

flushSync(fn);

// Restore CLEAN flags that were toggled during fork initialization.
if (batch.toggled_branches !== null) {
for (const effect of batch.toggled_branches) {
effect.f |= CLEAN;
}
batch.toggled_branches = null;
}

batch_values = null;

// revert state changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let count = $state(0);
</script>

<button onclick={() => count++}>{count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target }) {
const [forkBtn, counterBtn] = target.querySelectorAll('button');

flushSync(() => {
forkBtn.click();
});

assert.equal(counterBtn.textContent, '0');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '1');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import { fork } from 'svelte';
import Child from './Child.svelte';

let show = $state(false);
</script>

<button onclick={() => {
let f = fork(() => {
show = !show;
});
f.discard();
}}>fork</button>

{#if show}
hi
{:else}
{#if show || !show}
<Child />
{/if}
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let count = $state(0);
</script>

<button onclick={() => count++}>{count}</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target }) {
const [forkBtn, counterBtn] = target.querySelectorAll('button');

flushSync(() => {
forkBtn.click();
});

assert.equal(counterBtn.textContent, '0');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '1');

flushSync(() => {
counterBtn.click();
});

assert.equal(counterBtn.textContent, '2');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { fork } from 'svelte';
import Child from './Child.svelte';

let show = $state(false);
let pendingFork = $state(null);
</script>

<button onclick={() => {
// Create fork but don't discard to simulate SvelteKit preload
pendingFork = fork(() => {
show = !show;
});
}}>fork</button>

{#if show}
hi
{:else}
{#if show || !show}
<Child />
{/if}
{/if}
Loading