kit icon indicating copy to clipboard operation
kit copied to clipboard

Supporting Form Actions on regular +page.js/ts

Open katriellucas opened this issue 1 year ago • 2 comments

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

  • Do nothing, it's not like it's impossible the implement the same functionality on user-land.
  • Use Superforms a thirdparty lib to handle the forms on client-side.

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.

katriellucas avatar Feb 14 '24 02:02 katriellucas

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.

Conduitry avatar Feb 14 '24 02:02 Conduitry

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.

katriellucas avatar Feb 14 '24 04:02 katriellucas