fix(server): don't redirect error responses from #[get] server functions (#5429)

The post-handler redirect logic was unconditionally converting responses
to 302 Found when the request had Accept: text/html and a Referer header.
This overwrote error status codes (4xx, 5xx), making it impossible to
return errors from #[get] endpoints accessed via browser navigation.

Add a response.status().is_success() guard so only successful responses
get the Post/Redirect/Get treatment.

Fixes #5428
This commit is contained in:
John Costa authored and GitHub committed 2026-03-30 18:46:48 -07:00
1 parent e5b9c39f4e
commit 45442af41d
1 file changed
+5 -3
+5 -3
View File
@@ -113,9 +113,11 @@ impl ServerFunction {
response.headers_mut().extend(headers); response.headers_mut().extend(headers);
} }
// if it accepts text/html (i.e., is a plain form post) and doesn't already have a // If the response is successful and accepts text/html (i.e., is a
// Location set, then redirect to Referer // plain form post) and doesn't already have a Location set, then
if accepts_html { // redirect to Referer. Only redirect on success so that error
// responses (4xx, 5xx) propagate correctly to the client.
if accepts_html && response.status().is_success() {
if let Some(referrer) = referrer { if let Some(referrer) = referrer {
let has_location = response.headers().get(LOCATION).is_some(); let has_location = response.headers().get(LOCATION).is_some();
if !has_location { if !has_location {