sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.39k stars 1.88k forks source link

Supporting Form Actions on regular +page.js/ts #11843

Open katriellucas opened 6 months ago

katriellucas commented 6 months ago

Describe the problem

In accord with the docs:

A +page.server.js file can export actions, which allow you to POST data to the server using the <form> element.

Why not allow Sveltekit form actions on regular +page.js? Having access to this feature would be very beneficial for SPAs that make use of external APIs to do a myriad of functions such as getting authentication tokens, search queries, fetching and sending data thru external APIs.

Describe the proposed solution

Allow the usage of Form actions on client-side, the only constraints I see are that CSR has to be enabled and the user needs JavaScript. Which in this case, JS is likely a requirement as we woudn't be able to use +server.js files anyway.

Alternatives considered

Importance

would make my life easier

Additional Information

On a side note, I think this would be even more useful with #11828, as it's pretty common to set cookies on form actions.

Conduitry commented 6 months ago

Is the suggestion here that the +page.js function would get a synthetic FormData object and would then be responsible for making whatever underlying request is appropriate? I'm not sure how it would work otherwise, as there is no sufficiently unopinionated way to turn a <form> into an AJAX call. That was one of the reasons for implementing form actions the way they are - they fall back to regular old-fashioned form submissions, which work in a very standard way.

katriellucas commented 6 months ago

Is the suggestion here that the +page.js function would get a synthetic FormData object and would then be responsible for making whatever underlying request is appropriate? I'm not sure how it would work otherwise, as there is no sufficiently unopinionated way to turn a <form> into an AJAX call. That was one of the reasons for implementing form actions the way they are - they fall back to regular old-fashioned form submissions, which work in a very standard way.

If I'm understanding right, yes, the request is basicaly what Superforms does, whoever, I think Sveltekit does it better syntactically. And if I'm not wrong, this would allows us to use use:enhance without server files.