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

include submitter value in enhanced GET forms #8273

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/tricky-foxes-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix enhanced GET form omitting submit value
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 12 additions & 5 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1461,21 +1461,28 @@ export function create_client({ target, base }) {
if (method !== 'get') return;

const url = new URL(
(event.submitter?.hasAttribute('formaction') && submitter?.formAction) || form.action
(submitter?.hasAttribute('formaction') && submitter?.formAction) || form.action
);

if (is_external_url(url, base)) return;

const { noscroll, reload } = get_router_options(
/** @type {HTMLFormElement} */ (event.target)
);
const event_form = /** @type {HTMLFormElement} */ (event.target);

const { noscroll, reload } = get_router_options(event_form);
if (reload) return;

event.preventDefault();
event.stopPropagation();

const data = new FormData(event_form);

const submitter_name = submitter?.getAttribute('name');
if (submitter_name) {
data.append(submitter_name, submitter?.getAttribute('value') ?? '');
}

// @ts-expect-error `URLSearchParams(fd)` is kosher, but typescript doesn't know that
url.search = new URLSearchParams(new FormData(event.target)).toString();
url.search = new URLSearchParams(data).toString();

navigate({
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

<h1>{$page.url.searchParams.get('q') ?? '...'}</h1>
<h2>{type}</h2>
<h3>{$page.url.searchParams.get('foo') ?? '...'}</h3>

<form>
<input name="q" />
<button>submit</button>
<button type="submit" name="foo" value="bar">Submit</button>
</form>
2 changes: 2 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ test.describe('Routing', () => {
await page.goto('/routing/form-get');
expect(await page.textContent('h1')).toBe('...');
expect(await page.textContent('h2')).toBe('enter');
expect(await page.textContent('h3')).toBe('...');

const requests = [];
page.on('request', (request) => requests.push(request.url()));
Expand All @@ -854,6 +855,7 @@ test.describe('Routing', () => {
expect(requests).toEqual([]);
expect(await page.textContent('h1')).toBe('updated');
expect(await page.textContent('h2')).toBe('form');
expect(await page.textContent('h3')).toBe('bar');
});
});

Expand Down