Add the ability to change the request within clientAction #8779
Replies: 6 comments 6 replies
-
Stumbled across another use case for this- compressing files client side before sending them to the server. Server's have to have some max request size, but if we can intercept the request to the server and downsample large image files, we can save users a server error. |
Beta Was this translation helpful? Give feedback.
-
I don't know that I agree - this feels sort of normal to me? You're telling the browser "hey don't do the default thing I want to add some stuff to the payload"? This is how you'd do it without Remix as well: // With Remix
<Form onSubmit={(e) => {
e.preventDefault();
let formData = new FormData(e.target);
formData.set(key, value);
submit(formData);
}}>
// Without Remix
<form onSubmit={(e) => {
e.preventDefault();
let formData = new FormData(e.target);
formData.set(key, value);
fetch(url, { method: 'post', body: formData });
}}> It feels somewhat less ergonomic to do this through a Remix abstraction IMO. When given a choice, I would prefer leaning on the platform APIs. There's a related proposal for adding custom headers to the behind-the-scenes |
Beta Was this translation helpful? Give feedback.
-
Using event.preventDefault is shame because you can no longer use When I do this work inside of onSubmit with event.preventDefault, remix has no idea the navigation state should really be "submitting". Moving this task to the clientAction seems like it would somewhat elegantly let remix handle this "pre-server-submission" kind of task. |
Beta Was this translation helpful? Give feedback.
-
Just thought of another use case for a capability like this. A clientLoader should be able to signal the server loader not a fetch a certain piece of data (presumably because we have it in client side cache). One natural way to do this would be for the client loader to modify the request sent to the server loader (add some query param or header or some such) which tells the server loader to skip some piece of work. The client loader would then stitch the server response together with what it has in the client side cache. |
Beta Was this translation helpful? Give feedback.
-
Some scenario where you may want to add more data to the serverAction, if you're using And while you could use useLocation().state and render a hidden input with that data it could be useful to add more things directly in the clientAction too. |
Beta Was this translation helpful? Give feedback.
-
Though not a critical, but looks like logical improvement to me for existing stuff. From my DX for me just easier to do all the work in one place (client action), rather splitting it between clientAction and component code. My use case:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Admittedly this has the chance to be abused but there are a few scenarios where modifying the request sent to the server might be handy.
For me, I'm trying to save card details on form submit (using stripe) and then send the card-id in the formData to the serverAction.
Right now, this kind of "right before you submit" processing has to be handled with an event.preventDefault() on form submit, which is a shame. Client action provides the perfect place to perform this kind of processing and then hand the request off to the server.
I propose add request as an optional parameter to
serverAction
. If no request is passed, the server action is called with the same request as client action. If a request is passed, that request is sent to the server instead.The usage would look something like
Beta Was this translation helpful? Give feedback.
All reactions