-
-
Notifications
You must be signed in to change notification settings - Fork 700
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
Proposal: Better Server function redirect handling #2069
Comments
There is an easy way to get the actual response body in the browser, and that is to include the Accept header with the encoding you want ( The reason it works the way it does currently is that this is the only way form submission works without JS and Webassembly enabled, to provide graceful degradation. We're working on a server fn rewrite for 0.6 that will change some of how this would be done, but probably not this behavior. Open to ideas that will preserve graceful degradation though |
The issue isn't about getting the response body, its about handling the redirect. Right now the only place that its handled is in the Form component in leptos_router. Where the action is proxied and if a 302 comes in the header of the response it will navigate on the client. This is great, but lets say for instance that you have an action behind a guard where failure to authenticate redirects you to /signin. This action wouldn't be called from a form because its returning a list of items to display to the user. If I call leptos_axum::redirect("/signin") after failure to authenticate it will redirect during SSR, but it won't redirect during client side rendering. This is a shortcoming of putting 302 header handling inside of a component where the whole action handling is re-implemented for a specific use case. |
There are two separate issues here I think:
|
I'm always hesitant to do something working around what the browser/html does, but I also am not seeing a great solution for that |
Added in #2158. |
There are several issues where this has been brought up over the last few months, see
#1600 (comment)
#1358
#1513
The brief overview, for those not familiar is that the current implementation of server actions will attempt to decode the response body of any request that is not 400-599 status codes. This causes problems as the response bodies of 302 (and any other redirect) is the body of the redirected page (which is frequently HTML).
There is currently no way to get at the actual request body in major browsers so that alternative is to change the call signatures of the server function API.
I have several proposals that have varying levels of breaking change.
Change the signature of call_server_fn to be
Result<MaybeRedirect<T>, ServerFnError>
whereMaybeRedirect<T>
requires the caller to check if the response body is a redirect. This is probably unreasonable, but it is a solution and at worst would introduce an unwrap() to get at the value of T.Localize all of the client side networking code (Action Forms, Forms, Server Functions) into a single place with a create_request() API that gives the user the option to configure automatic client side navigation on 302. Something like
ServerFnError::Redirect(RedirectResponse)
variant and introduce a navigate() helper onRedirectResponse
that makes it possible to hand this in userland.I'd like to hear feedback on some of these proposals, I personally think 2 with 3 are the most idiomatic way of handling this.
The text was updated successfully, but these errors were encountered: