Skip to content

fix: ensure component root effect updates occur first #15439

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

Closed
wants to merge 2 commits into from
Closed
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/kind-schools-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure component root effect updates occur first
5 changes: 3 additions & 2 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
HEAD_EFFECT,
MAYBE_DIRTY,
EFFECT_HAS_DERIVED,
BOUNDARY_EFFECT
BOUNDARY_EFFECT,
DISCONNECTED
} from '../constants.js';
import { set } from './sources.js';
import * as e from '../errors.js';
Expand Down Expand Up @@ -229,7 +230,7 @@ export function inspect_effect(fn) {
* @returns {() => void}
*/
export function effect_root(fn) {
const effect = create_effect(ROOT_EFFECT, fn, true);
const effect = create_effect(ROOT_EFFECT | DISCONNECTED, fn, true);

return () => {
destroy_effect(effect);
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,14 @@ export function schedule_effect(signal) {
}
}

queued_root_effects.push(effect);
// Schedule the root effect for component trees first so any updates
// that affect the component tree occur first. Root effects that are
// not for component trees (i.e. $effect.root) will be marked as disconnected
if ((effect.f & DISCONNECTED) === 0) {
queued_root_effects.unshift(effect);
} else {
queued_root_effects.push(effect);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
let [, btn2] = target.querySelectorAll('button');

btn2.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<button>Set data</button><button>Clear data</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { toStore } from 'svelte/store'

let { data } = $props()
const currentValue = toStore(() => data.value)
</script>

<p>
Current value:
<span>{$currentValue}</span>
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import Child from './child.svelte'

let data = $state({ value: 'hello' });

const setData = () => (data = { value: 'hello' })
const clearData = () => (data = undefined)
</script>

<button onclick={setData}>Set data</button>
<button onclick={clearData}>Clear data</button>

{#if data}
<Child {data} />
{/if}