Replies: 2 comments
-
I do like your Idea as i experimented a lot with similar patterns. The main issue i have with this is that you need to have the form inside the specific +page.svelte in this scenario. In the end i pretty much always ended up with something similar to trpc. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. Yeah, that limits it quite a bit. Importing these action functions from other In my own code, I have created a |
Beta Was this translation helpful? Give feedback.
-
Consider this:
+page.server.js
+page.svelte
Actions defined in
+page.server.ts
will be available on the actions object, which will be passed as a prop to the component.Each action is a function, which when invoked, returns an object with props that can be spread onto a form element. The interface for such a return value could look something like this:
The function could also accept an optional options argument, including options as whether the submission should cause a reload and maybe callbacks such as
onsuccess
andonerror
etc.Spreading the props onto a form element avoids the need to modify the native
<form>
element'saction
attribute (as React does) or require a custom<Form>
component (as Qwik does). Instead, we can embrace the form tag as it is, which also aligns nicely with the way routing with plain<a>
tags already works in SvelteKit.And, by using spread props instead of a Svelte
use:
action, we avoid the need to manually add theaction
andmethod
attributes to the form to make them available during SSR and enable form submission without JavaScript.Additionally, the action can be manually invoked by using the
perform()
method on it. The perform method takes one argument, which could be a plain object or an instance ofFormData
. It returns a promise that resolves when the action succeeds or throws if the action encounters an error in+page.server.ts
.Furthermore, the action could expose an
isPending
property, which becomestrue
when the action is waiting for the response, and adata
property, which is initiallynull
or holds the result of the last invocation of the action. Thedata
property would resemble that of the currentform
(ActionData
) prop, but since it's tailored to the action, it offers the additional advantage of potentially inheriting the types.So all in all, the interface for this feature might look something like this:
This is an idea inspired by server actions in React and route actions in Qwik, which many find very convenient and easy to reason about. However, I don't have enough knowledge to know if it's possible to implement something like this, or if there are other considerations that I haven't thought of. Nonetheless, such a change would require Svelte 5 (due to the ability to send events as props), but I believe it should be possible to implement this without causing any breaking changes. And while this feature isn't essential to get the job done (Svelte and SvelteKit are already awesome as they are!), I think it would simplify and reduce boilerplate code when working with forms and submissions in SvelteKit.
Beta Was this translation helpful? Give feedback.
All reactions