Skip to content

Commit 050447a

Browse files
elliott-with-the-longest-name-on-githubdummdidummRich-Harrisbenmccann
authored
feat: Allow bypassing invalidateAll in enhance (#9889)
* feat: Allow bypassing `invalidateAll` in `enhance` * changeset * typedoc * oops * cleanup * Update .changeset/shaggy-trainers-drum.md * technically fixed, though types will have to catch up * Revert "technically fixed, though types will have to catch up" oops, wrong branch This reverts commit a9ae11b. * Update .changeset/shaggy-trainers-drum.md Co-authored-by: Ben McCann <[email protected]> * Update packages/kit/test/apps/dev-only/package.json Co-authored-by: Ben McCann <[email protected]> * fix: Types --------- Co-authored-by: Simon H <[email protected]> Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Ben McCann <[email protected]>
1 parent 96993cf commit 050447a

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

.changeset/shaggy-trainers-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: add `invalidateAll` boolean option to `enhance` callback

packages/kit/src/exports/public.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ export type SubmitFunction<
12821282
/**
12831283
* Call this to get the default behavior of a form submission response.
12841284
* @param options Set `reset: false` if you don't want the `<form>` values to be reset after a successful submission.
1285+
* @param invalidateAll Set `invalidateAll: false` if you don't want the action to call `invalidateAll` after submission.
12851286
*/
1286-
update(options?: { reset: boolean }): Promise<void>;
1287+
update(options?: { reset?: boolean; invalidateAll?: boolean }): Promise<void>;
12871288
}) => void)
12881289
>;
12891290

packages/kit/src/runtime/app/forms.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,25 @@ export function enhance(form_element, submit = () => {}) {
104104
/**
105105
* @param {{
106106
* action: URL;
107+
* invalidateAll?: boolean;
107108
* result: import('@sveltejs/kit').ActionResult;
108109
* reset?: boolean
109110
* }} opts
110111
*/
111-
const fallback_callback = async ({ action, result, reset }) => {
112+
const fallback_callback = async ({
113+
action,
114+
result,
115+
reset = true,
116+
invalidateAll: shouldInvalidateAll = true
117+
}) => {
112118
if (result.type === 'success') {
113-
if (reset !== false) {
119+
if (reset) {
114120
// We call reset from the prototype to avoid DOM clobbering
115121
HTMLFormElement.prototype.reset.call(form_element);
116122
}
117-
await invalidateAll();
123+
if (shouldInvalidateAll) {
124+
await invalidateAll();
125+
}
118126
}
119127

120128
// For success/failure results, only apply action if it belongs to the
@@ -222,7 +230,13 @@ export function enhance(form_element, submit = () => {}) {
222230
return form_element;
223231
},
224232
formElement: form_element,
225-
update: (opts) => fallback_callback({ action, result, reset: opts?.reset }),
233+
update: (opts) =>
234+
fallback_callback({
235+
action,
236+
result,
237+
reset: opts?.reset,
238+
invalidateAll: opts?.invalidateAll
239+
}),
226240
// @ts-expect-error generic constraints stuff we don't care about
227241
result
228242
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load() {
2+
return {
3+
changes_every_load: new Date()
4+
};
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function load({ url }) {
2+
const invalidate_all = url.searchParams.get('invalidate_all') === 'true';
3+
return {
4+
invalidate_all
5+
};
6+
}
7+
8+
export const actions = {
9+
default: () => {}
10+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { enhance } from '$app/forms';
3+
4+
export let data;
5+
6+
/**
7+
* @param {HTMLFormElement} node
8+
*/
9+
function enhanceWrapper(node) {
10+
return enhance(
11+
node,
12+
() =>
13+
({ update }) =>
14+
update({ invalidateAll: data.invalidate_all })
15+
);
16+
}
17+
</script>
18+
19+
<form method="POST" use:enhanceWrapper>
20+
<pre>{data.changes_every_load.toISOString()}</pre>
21+
<button type="submit">invalidate</button>
22+
</form>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,32 @@ test.describe('Matchers', () => {
868868
});
869869

870870
test.describe('Actions', () => {
871+
test("invalidateAll = false doesn't invalidate all", async ({ page, javaScriptEnabled }) => {
872+
await page.goto('/actions/invalidate-all?invalidate_all=false');
873+
const preSubmitContent = await page.locator('pre').textContent();
874+
await page.click('button');
875+
// The value that should not change is time-based and might not have the granularity to change
876+
// if we don't give it time to
877+
await page.waitForTimeout(1000);
878+
const postSubmitContent = await page.locator('pre').textContent();
879+
if (!javaScriptEnabled) {
880+
expect(preSubmitContent).not.toBe(postSubmitContent);
881+
} else {
882+
expect(preSubmitContent).toBe(postSubmitContent);
883+
}
884+
});
885+
886+
test('invalidateAll = true does invalidate all', async ({ page }) => {
887+
await page.goto('/actions/invalidate-all?invalidate_all=true');
888+
const preSubmitContent = await page.locator('pre').textContent();
889+
await page.click('button');
890+
// The value that should not change is time-based and might not have the granularity to change
891+
// if we don't give it time to
892+
await page.waitForTimeout(1000);
893+
const postSubmitContent = await page.locator('pre').textContent();
894+
expect(preSubmitContent).not.toBe(postSubmitContent);
895+
});
896+
871897
test('Submitting a form with a file input but no enctype="multipart/form-data" logs a warning', async ({
872898
page,
873899
javaScriptEnabled

0 commit comments

Comments
 (0)