Skip to content

Commit c0b582e

Browse files
authored
fix: batch synchronous invalidate invocations (#10145)
fixes #10142
1 parent 61091fa commit c0b582e

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

.changeset/khaki-doors-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: batch synchronous invalidate invocations

packages/kit/src/runtime/client/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function create_client(app, target) {
153153
// but batch multiple synchronous invalidations.
154154
pending_invalidate = pending_invalidate || Promise.resolve();
155155
await pending_invalidate;
156+
if (!pending_invalidate) return;
156157
pending_invalidate = null;
157158

158159
const url = new URL(location.href);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const ssr = false;
2+
3+
let count = 0;
4+
5+
export function load({ depends }) {
6+
depends('multiple:invalidations-go-brr');
7+
return { count: count++ };
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { invalidate } from '$app/navigation';
3+
4+
export let data;
5+
</script>
6+
7+
<div>
8+
<button
9+
id="multiple-batched"
10+
on:click={(event) => {
11+
const btn = event.currentTarget;
12+
invalidate('multiple:invalidations-go-brr');
13+
invalidate('multiple:invalidations-go-brr');
14+
15+
Promise.resolve()
16+
.then(() => invalidate('multiple:invalidations-go-brr'))
17+
.then(() => {
18+
btn.dataset.done = 'true';
19+
});
20+
}}>{data.count}</button
21+
>
22+
</div>

packages/kit/test/apps/basics/test/client.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ test.describe('Invalidation', () => {
436436
await expect(page.getByText('layout: 4, page: 4')).toBeVisible();
437437
});
438438

439+
test('multiple synchronous invalidations are batched', async ({ page }) => {
440+
await page.goto('/load/invalidation/multiple-batched');
441+
const btn = page.locator('#multiple-batched');
442+
await expect(btn).toHaveText('0');
443+
444+
await btn.click();
445+
await expect(btn).toHaveAttribute('data-done', 'true');
446+
await expect(btn).toHaveText('2');
447+
});
448+
439449
test('invalidateAll persists through redirects', async ({ page }) => {
440450
await page.goto('/load/invalidation/multiple/redirect');
441451
await page.locator('button.redirect').click();

0 commit comments

Comments
 (0)