Skip to content

Commit d3b5257

Browse files
authored
fix: skip refreshing queries on full-page reload form submissions (#15803)
If a non-enhanced remote form is submitted, it results in a full-page reload. By the nature of a full-page reload, it's impossible for the form to return refreshed queries to the client. Therefore, because queries should be idempotent, we can simply turn `query().refresh()` into a no-op if it's a full-page reload.
1 parent 785cdd3 commit d3b5257

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

.changeset/shaky-ravens-search.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: skip refreshing queries on full-page reload form submissions

packages/kit/src/runtime/app/server/remote/query.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ function create_query_resource(__, payload, state, fn) {
434434
return false;
435435
},
436436
refresh() {
437+
const { event } = get_request_store();
438+
if (!event.isRemoteRequest) {
439+
// If the form submission is not a remote request, refreshing the data is
440+
// useless, because it can't be returned to the client.
441+
return Promise.resolve();
442+
}
437443
const refresh_context = get_refresh_context(__, 'refresh', payload);
438444
const is_immediate_refresh = !refresh_context.cache[refresh_context.payload];
439445
const value = is_immediate_refresh ? get_promise() : fn();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { get_count, set } from './form.remote';
3+
4+
const { params } = $props();
5+
</script>
6+
7+
<div id="count">Count: {await get_count(params.key)}</div>
8+
9+
<form {...set}>
10+
<input {...set.fields.key.as('hidden', params.key)} />
11+
<button type="submit">Submit</button>
12+
</form>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { form, query } from '$app/server';
2+
import * as v from 'valibot';
3+
4+
let counts = new Map();
5+
6+
export const get_count = query(v.string(), (key) => {
7+
return counts.get(key) ?? 0;
8+
});
9+
export const increment_count = query(v.string(), (key) => {
10+
counts.set(key, (counts.get(key) ?? 0) + 1);
11+
return counts.get(key);
12+
});
13+
14+
export const set = form(v.object({ key: v.string() }), async ({ key }) => {
15+
await increment_count(key).refresh();
16+
await get_count(key).refresh();
17+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,17 @@ test.describe('remote function mutations', () => {
796796
// the value display should also show the updated value
797797
await expect(page.locator('#set-value-display')).toHaveText('Set via method');
798798
});
799+
test('form does refresh queries when a remote request', async ({ page }) => {
800+
await page.goto(`/remote/form/noop-refresh-non-enhanced/${Date.now()}${Math.random()}`);
801+
802+
const count = page.locator('#count');
803+
await expect(count).toHaveText('Count: 0');
804+
805+
await page.click('button');
806+
807+
// Should have refreshed
808+
await expect(count).toHaveText('Count: 1');
809+
});
799810
});
800811

801812
test.describe('client error boundaries', () => {

packages/kit/test/apps/async/test/server.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ test.describe('remote functions', () => {
2222
);
2323
expect(code.includes('const with_read = prerender(')).toBe(false);
2424
});
25+
test("form doesn't refresh queries when not a remote request", async ({ page }) => {
26+
await page.goto(`/remote/form/noop-refresh-non-enhanced/${Date.now()}${Math.random()}`);
27+
28+
const count = page.locator('#count');
29+
await expect(count).toHaveText('Count: 0');
30+
31+
await page.click('button');
32+
33+
// Should not have refreshed
34+
await expect(count).toHaveText('Count: 0');
35+
});
2536
});

0 commit comments

Comments
 (0)