You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
If a server action performs a redirect and is called from an ActionForm submit, then the ActionForm will correctly redirect you to the destination page but it will also:
follow the redirect
make a new request to the destination
attempt to deserialize the result and raise an error
Leptos Dependencies
leptos = { version = "0.5.0-beta" }
leptos_meta = { version = "0.5.0-beta" }
leptos_actix = { version = "0.5.0-beta", optional = true }
leptos_router = { version = "0.5.0-beta" }
have also tried with the current version on main.
To Reproduce
Have 2 routes
On the first roue have an ActionForm with a single submit button and have a server function as it's action
Handle any errors from the ActionForm action and log them to the console
The server function has a redirect to the other route
Click the button within the ActionForm
Observe in the console an error logged similar to: Error: error deserializing server function results: expected value at line 1 column 1
Observe in the network tab the additional fetch to the destination page
Example code:
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
#[component]
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();
view! {
// injects a stylesheet into the document <head>
// id=leptos means cargo-leptos will hot-reload this stylesheet
<Stylesheet id="leptos" href="/pkg/leptos_start.css"/>
// sets the document title
<Title text="Welcome to Leptos"/>
// content for this welcome page
<Router>
<main>
<Routes>
<Route path="" view=HomePage/>
<Route path="/another-page" view=AnotherPage/>
</Routes>
</main>
</Router>
}
}
/// Renders the home page of your application.
#[component]
fn HomePage() -> impl IntoView {
let something = create_server_action::<SomeThing>();
create_effect(move |_| {
match something.value().get() {
Some(Ok(success)) => {
log::info!("Success: {}", success);
}
Some(Err(error)) => {
log::error!("Error: {}", error);
}
_ => {}
};
});
log::info!("Home Page");
view! {
<h1>"Welcome to Leptos!"</h1>
<ActionForm action=something>
<input type="hidden" name="should_work" value="true" />
<button type="submit">
"Redirect"
</button>
</ActionForm>
}
}
#[component]
fn AnotherPage() -> impl IntoView {
log::info!("Another Page");
view! {
<h1>"Another Page"</h1>
}
}
#[server(SomeThing, "/api")]
pub async fn something(should_work: bool) -> Result<bool, ServerFnError> {
leptos_actix::redirect("/another-page");
Ok(true)
}
Expected behavior
The ActionForm should not follow the redirect so it doesn't produce an error or additional request.
Screenshots
Console with error received.
Network tab details showing the request to the something server action and the extra request to another-page
Request chain for extra request to another-page showing it is initiated from the something request
See #1358 and #1513. I'm open to suggestions on how this could be handled better but following the redirect is the browser's behavior when fetching a page that redirects, and I and the others in those issues have not found a way to prevent that while still redirecting.
Thanks for this info. I have read through the linked issues and dug around and yeah it doesn't look like there is a way to prevent the redirect. The extra request isn't too bad, but the error it still an issue. I have created a #1604 PR which just adds a guard to prevent parsing the body in-case of a redirect.
Describe the bug
If a server action performs a redirect and is called from an ActionForm submit, then the ActionForm will correctly redirect you to the destination page but it will also:
Leptos Dependencies
have also tried with the current version on main.
To Reproduce
Error: error deserializing server function results: expected value at line 1 column 1
Example code:
Expected behavior
The ActionForm should not follow the redirect so it doesn't produce an error or additional request.
Screenshots
Console with error received.
Network tab details showing the request to the
something
server action and the extra request toanother-page
Request chain for extra request to
another-page
showing it is initiated from thesomething
requestAdditional context
A repo with the issue can be found here: https://github.com/JonRCahill/leptos-server-action-redirect-issue
The text was updated successfully, but these errors were encountered: