Skip to content

fix: fire afterUpdate even if dependencies are not used in template #9442

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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/dirty-dancers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: fire `afterUpdate` even if dependencies are not used in template
2 changes: 1 addition & 1 deletion packages/svelte/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export default defineConfig({
format: 'umd',
name: 'svelte'
},
plugins: [resolve(), commonjs(), terser()]
plugins: [resolve(), commonjs()]
});
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ComponentContext = {
s: MaybeSignal<Record<string, unknown>>;
/** accessors */
a: Record<string, any> | null;
/** effectgs */
/** effects */
e: null | Array<EffectSignal>;
/** mounted */
m: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/main/main-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ function init_update_callbacks() {
// TODO somehow beforeUpdate ran twice on mount in Svelte 4 if it causes a render
// possibly strategy to get this back if needed: analyse beforeUpdate function for assignements to state,
// if yes, add a call to the component to force-run beforeUpdate once.
untrack(() => update_callbacks.b.forEach(/** @param {any} c */ (c) => c()));
update_callbacks.b.forEach((c) => c());
flush_local_render_effects();
// beforeUpdate can run again once if afterUpdate causes another update,
// but afterUpdate shouldn't be called again in that case to prevent infinite loops
if (!called_after) {
user_effect(() => {
called_before = false;
called_after = true;
untrack(() => update_callbacks.a.forEach(/** @param {any} c */ (c) => c()));
update_callbacks.a.forEach((c) => c());
// managed_effect so that it's not cleaned up when the parent effect is cleaned up
const managed = managed_effect(() => {
destroy_signal(managed);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test, ok } from '../../test';
import { flushSync } from 'svelte';

const log = console.log;
/** @type {string[]} */
let messages = [];

export default test({
before_test: () => {
console.log = (msg) => messages.push(msg);
messages = [];
},
after_test: () => {
console.log = log;
},
test({ assert, target, window }) {
const [button1, button2] = target.querySelectorAll('button');
ok(button1);
ok(button2);

button1.click();
flushSync();
button2.click();
flushSync();

assert.deepEqual(messages, ['after update 0, 0', 'after update 1, 0', 'after update 1, 1']);
assert.htmlEqual(
target.innerHTML,
`
<button>count:
1</button><button>count
2:
1</button><hr>
1
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import AfterUpdate from "./sub.svelte";
let count = 0;
let count2 = 0;

function increment() {
count += 1;
}
function increment2() {
count2 += 1;
}
</script>

<button on:click={increment}>
count: {count}
</button>
<button on:click={increment2}>
count 2: {count2}
</button>
<hr />
<AfterUpdate {count} {count2}/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { afterUpdate } from "svelte";

export let count;
export let count2;

afterUpdate(()=>{
console.log(`after update ${count}, ${count2}`);
});
</script>

{count}