Skip to content
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

feat: Allow bypassing invalidateAll in enhance #9889

Merged
merged 14 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-trainers-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

[feat] Add `invalidateAll` boolean option to `enhance` callback
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 18 additions & 4 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ export function enhance(form, submit = () => {}) {
* @param {{
* action: URL;
* result: import('types').ActionResult;
* reset?: boolean
* reset?: boolean;
* invalidateAll?: boolean;
* }} opts
*/
const fallback_callback = async ({ action, result, reset }) => {
const fallback_callback = async ({
action,
result,
reset,
invalidateAll: shouldInvalidateAll
tcc-sejohnson marked this conversation as resolved.
Show resolved Hide resolved
}) => {
if (result.type === 'success') {
if (reset !== false) {
// We call reset from the prototype to avoid DOM clobbering
HTMLFormElement.prototype.reset.call(form);
}
await invalidateAll();
if (shouldInvalidateAll !== false) {
await invalidateAll();
}
}

// For success/failure results, only apply action if it belongs to the
Expand Down Expand Up @@ -112,7 +120,13 @@ export function enhance(form, submit = () => {}) {
action,
data,
form,
update: (opts) => fallback_callback({ action, result, reset: opts?.reset }),
update: (opts) =>
fallback_callback({
action,
result,
reset: opts?.reset,
invalidateAll: opts?.invalidateAll
}),
// @ts-expect-error generic constraints stuff we don't care about
result
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function load() {
return {
changes_every_load: new Date()
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function load({ url }) {
const invalidate_all = url.searchParams.get('invalidate_all') === 'true';
return {
invalidate_all
};
}

export const actions = {
default: () => {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { enhance } from '$app/forms';

export let data;

/**
* @param {HTMLFormElement} node
*/
function enhanceWrapper(node) {
return enhance(
node,
() =>
({ update }) =>
update({ invalidateAll: data.invalidate_all })
);
}
</script>

<form method="POST" use:enhanceWrapper>
<pre>{data.changes_every_load.toISOString()}</pre>
<button type="submit">invalidate</button>
</form>
26 changes: 26 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,32 @@ test.describe('Matchers', () => {
});

test.describe('Actions', () => {
test("invalidateAll = false doesn't invalidate all", async ({ page, javaScriptEnabled }) => {
await page.goto('/actions/invalidate-all?invalidate_all=false');
const preSubmitContent = await page.locator('pre').textContent();
await page.click('button');
// The value that should not change is time-based and might not have the granularity to change
// if we don't give it time to
await page.waitForTimeout(1000);
tcc-sejohnson marked this conversation as resolved.
Show resolved Hide resolved
const postSubmitContent = await page.locator('pre').textContent();
if (!javaScriptEnabled) {
expect(preSubmitContent).not.toBe(postSubmitContent);
} else {
expect(preSubmitContent).toBe(postSubmitContent);
}
});

test('invalidateAll = true does invalidate all', async ({ page }) => {
await page.goto('/actions/invalidate-all?invalidate_all=true');
const preSubmitContent = await page.locator('pre').textContent();
await page.click('button');
// The value that should not change is time-based and might not have the granularity to change
// if we don't give it time to
await page.waitForTimeout(1000);
const postSubmitContent = await page.locator('pre').textContent();
expect(preSubmitContent).not.toBe(postSubmitContent);
});

test('Error props are returned', async ({ page, javaScriptEnabled }) => {
await page.goto('/actions/form-errors');
await page.click('button');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/dev-only/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-basics",
"name": "test-no",
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
"private": true,
"version": "0.0.2-next.0",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ declare module '$app/forms' {
/**
* Call this to get the default behavior of a form submission response.
* @param options Set `reset: false` if you don't want the `<form>` values to be reset after a successful submission.
* @param invalidateAll Set `invalidateAll: false` if you don't want the action to call `invalidateAll` after submission.
*/
update(options?: { reset: boolean }): Promise<void>;
update(options?: { reset?: boolean; invalidateAll?: boolean }): Promise<void>;
}) => void)
>;

Expand Down