feat: allow setting http response header in fullstack

This commit is contained in:
Jonathan Kelley committed 2025-10-15 01:20:16 -07:00
1 parent 0aa7351473
commit 20fa29ac6e
3 files changed
+38 -5

No files matched your search

+9 -1
View File
@@ -304,15 +304,23 @@ impl RenderHandleState {
.await;
match response {
Ok((status, freshness, rx)) => {
Ok((status, headers, freshness, rx)) => {
let mut response = Response::builder()
.status(status.status)
.header(CONTENT_TYPE, "text/html; charset=utf-8")
.body(Body::from_stream(rx))
.unwrap();
// Write our freshness header
freshness.write(response.headers_mut());
// write the other headers set by the user
for (key, value) in headers.into_iter() {
if let Some(key) = key {
response.headers_mut().insert(key, value);
}
}
response
}
+9 -3
View File
@@ -17,7 +17,7 @@ use dioxus_router::ParseRouteError;
use dioxus_ssr::Renderer;
use futures_channel::mpsc::Sender;
use futures_util::{Stream, StreamExt};
use http::{request::Parts, StatusCode};
use http::{request::Parts, HeaderMap, StatusCode};
use std::{
collections::HashMap,
fmt::Write,
@@ -103,6 +103,7 @@ impl SsrRendererPool {
) -> Result<
(
HttpError,
HeaderMap,
RenderFreshness,
impl Stream<Item = Result<String, IncrementalRendererError>>,
),
@@ -154,6 +155,7 @@ impl SsrRendererPool {
status: StatusCode::OK,
message: None,
},
HeaderMap::new(),
freshness,
ReceiverWithDrop {
receiver: rx,
@@ -292,9 +294,12 @@ impl SsrRendererPool {
// Check the FullstackContext in case the user set the statuscode manually or via a layout.
let http_status = streaming_context.current_http_status();
let headers = streaming_context
.take_response_headers()
.unwrap_or_default();
// Now that we handled any errors from rendering, we can send the initial ok result
_ = initial_result_tx.send(Ok(http_status));
_ = initial_result_tx.send(Ok((http_status, headers)));
// Wait long enough to assemble the `<head>` of the document before starting to stream
let mut pre_body = String::new();
@@ -418,12 +423,13 @@ impl SsrRendererPool {
let join_handle = Self::spawn_platform(create_render_future);
// Wait for the initial result which determines the status code
let status = initial_result_rx
let (status, headers) = initial_result_rx
.await
.map_err(|err| SSRError::Incremental(IncrementalRendererError::Other(err.into())))??;
Ok((
status,
headers,
RenderFreshness::now(None),
ReceiverWithDrop {
receiver: rx,