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

Respect the form enctype in progressive form enhancement example #1936

Open
simonflk opened this issue Jul 17, 2021 · 0 comments · May be fixed by #12198
Open

Respect the form enctype in progressive form enhancement example #1936

simonflk opened this issue Jul 17, 2021 · 0 comments · May be fixed by #12198
Labels
examples p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Milestone

Comments

@simonflk
Copy link

simonflk commented Jul 17, 2021

Describe the problem

The form enhancement that ships in the demo ignores the form[enctype] and always posts the data as a multi-part form using FormData.

While the <form> elements in the todos component do not explicitly define the enctype, they inherit the default value of application/x-www-form-urlencoded.

In other words, the behaviour differs depending on the context:

Context enctype example
JS disabled application/x-www-form-urlencoded text=Buy%20some%20milk
JS enabled multipart/form-data ------WebKitFormBoundaryVXPDCeRyJsYB7s4v\nContent-Disposition: form-data; name="text"\n\nbuy some milk\n------WebKitFormBoundaryVXPDCeRyJsYB7s4v--

I think it would be preferable for the JS version to mirror the behaviour of native HTML form. Especially if this ends up becoming core functionality in sveltekit.

Describe the proposed solution

Modify the enhance action so that it mirrors browser behaviour.

According to MDN, enctype can be set on the form itself, but it can also be overridden by a formenctype on the button which triggered the submit. The submit button is accessible on the SubmitEvent

For example:

-        const body = new FormData(form);
+        const formData = new FormData(form);
+        const enctype = event.submitter.formEnctype || form.enctype;
+        const body = (enctype === 'multipart/form-data')
+            ? formData
+            : new URLSearchParams(formData);

Notes

Alternatives considered

Documentation/comments so that developers are not caught off-guard.

Importance

nice to have

Additional Information

No response

@benmccann benmccann added the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label Aug 4, 2021
@Rich-Harris Rich-Harris added this to the 1.0 milestone Apr 26, 2022
@Rich-Harris Rich-Harris modified the milestones: 1.0, whenever Aug 27, 2022
@allozaur allozaur linked a pull request May 8, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants