Make clippy happy, pass tests
This commit is contained in:
8 files changed
+57
-70
No files matched your search
@@ -302,6 +302,13 @@ fn apply_request_parts_to_response<B>(
|
||||
}
|
||||
}
|
||||
|
||||
type AxumHandler<F> = (
|
||||
F,
|
||||
ServeConfig,
|
||||
SSRState,
|
||||
Arc<dyn Fn() -> VirtualDom + Send + Sync>,
|
||||
);
|
||||
|
||||
/// SSR renderer handler for Axum with added context injection.
|
||||
///
|
||||
/// # Example
|
||||
@@ -350,18 +357,13 @@ fn apply_request_parts_to_response<B>(
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn render_handler_with_context<F: FnMut(&mut DioxusServerContext)>(
|
||||
State((mut inject_context, cfg, ssr_state, virtual_dom_factory)): State<(
|
||||
F,
|
||||
ServeConfig,
|
||||
SSRState,
|
||||
Arc<dyn Fn() -> VirtualDom + Send + Sync>,
|
||||
)>,
|
||||
State((mut inject_context, cfg, ssr_state, virtual_dom_factory)): State<AxumHandler<F>>,
|
||||
request: Request<Body>,
|
||||
) -> impl IntoResponse {
|
||||
let (parts, _) = request.into_parts();
|
||||
let url = parts.uri.path_and_query().unwrap().to_string();
|
||||
let parts: Arc<tokio::sync::RwLock<http::request::Parts>> =
|
||||
Arc::new(tokio::sync::RwLock::new(parts.into()));
|
||||
Arc::new(tokio::sync::RwLock::new(parts));
|
||||
let mut server_context = DioxusServerContext::new(parts.clone());
|
||||
inject_context(&mut server_context);
|
||||
|
||||
@@ -384,13 +386,15 @@ pub async fn render_handler_with_context<F: FnMut(&mut DioxusServerContext)>(
|
||||
}
|
||||
}
|
||||
|
||||
type RenderHandlerExtractor = (
|
||||
ServeConfig,
|
||||
Arc<dyn Fn() -> VirtualDom + Send + Sync>,
|
||||
SSRState,
|
||||
);
|
||||
|
||||
/// SSR renderer handler for Axum
|
||||
pub async fn render_handler(
|
||||
State((cfg, virtual_dom_factory, ssr_state)): State<(
|
||||
ServeConfig,
|
||||
Arc<dyn Fn() -> VirtualDom + Send + Sync>,
|
||||
SSRState,
|
||||
)>,
|
||||
State((cfg, virtual_dom_factory, ssr_state)): State<RenderHandlerExtractor>,
|
||||
request: Request<Body>,
|
||||
) -> impl IntoResponse {
|
||||
render_handler_with_context(
|
||||
|
||||
@@ -48,7 +48,7 @@ impl SsrRendererPool {
|
||||
SERVER_CONTEXT.with(|ctx| ctx.replace(server_context));
|
||||
// poll the future, which may call server_context()
|
||||
tracing::info!("Rebuilding vdom");
|
||||
let _ = vdom.rebuild(&mut NoOpMutations);
|
||||
vdom.rebuild(&mut NoOpMutations);
|
||||
vdom.wait_for_suspense().await;
|
||||
tracing::info!("Suspense resolved");
|
||||
// after polling the future, we need to restore the context
|
||||
@@ -76,9 +76,11 @@ impl SsrRendererPool {
|
||||
tx.send(Ok((renderer, RenderFreshness::now(None), html)));
|
||||
}
|
||||
Err(err) => {
|
||||
dioxus_ssr::incremental::IncrementalRendererError::Other(
|
||||
Box::new(err),
|
||||
);
|
||||
_ = tx.send(Err(
|
||||
dioxus_ssr::incremental::IncrementalRendererError::Other(
|
||||
Box::new(err),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -113,7 +115,7 @@ impl SsrRendererPool {
|
||||
.with(|ctx| ctx.replace(Box::new(server_context)));
|
||||
// poll the future, which may call server_context()
|
||||
tracing::info!("Rebuilding vdom");
|
||||
let _ = vdom.rebuild(&mut NoOpMutations);
|
||||
vdom.rebuild(&mut NoOpMutations);
|
||||
vdom.wait_for_suspense().await;
|
||||
tracing::info!("Suspense resolved");
|
||||
// after polling the future, we need to restore the context
|
||||
@@ -184,26 +186,21 @@ impl SSRState {
|
||||
}
|
||||
|
||||
/// Render the application to HTML.
|
||||
pub fn render<'a>(
|
||||
pub async fn render<'a>(
|
||||
&'a self,
|
||||
route: String,
|
||||
cfg: &'a ServeConfig,
|
||||
virtual_dom_factory: impl FnOnce() -> VirtualDom + Send + Sync + 'static,
|
||||
server_context: &'a DioxusServerContext,
|
||||
) -> impl std::future::Future<
|
||||
Output = Result<RenderResponse, dioxus_ssr::incremental::IncrementalRendererError>,
|
||||
> + Send
|
||||
+ 'a {
|
||||
async move {
|
||||
let ServeConfig { .. } = cfg;
|
||||
) -> Result<RenderResponse, dioxus_ssr::incremental::IncrementalRendererError> {
|
||||
let ServeConfig { .. } = cfg;
|
||||
|
||||
let (freshness, html) = self
|
||||
.renderers
|
||||
.render_to(cfg, route, virtual_dom_factory, server_context)
|
||||
.await?;
|
||||
let (freshness, html) = self
|
||||
.renderers
|
||||
.render_to(cfg, route, virtual_dom_factory, server_context)
|
||||
.await?;
|
||||
|
||||
Ok(RenderResponse { html, freshness })
|
||||
}
|
||||
Ok(RenderResponse { html, freshness })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,7 +318,7 @@ impl RenderResponse {
|
||||
fn pre_renderer() -> Renderer {
|
||||
let mut renderer = Renderer::default();
|
||||
renderer.pre_render = true;
|
||||
renderer.into()
|
||||
renderer
|
||||
}
|
||||
|
||||
fn incremental_pre_renderer(
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// A ServeConfig is used to configure how to serve a Dioxus application. It contains information about how to serve static assets, and what content to render with [`dioxus-ssr`].
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct ServeConfigBuilder {
|
||||
pub(crate) root_id: Option<&'static str>,
|
||||
pub(crate) index_path: Option<PathBuf>,
|
||||
|
||||
@@ -120,7 +120,7 @@ mod server_fn_impl {
|
||||
}
|
||||
|
||||
std::thread_local! {
|
||||
pub(crate) static SERVER_CONTEXT: std::cell::RefCell<Box<DioxusServerContext>> = std::cell::RefCell::new(Box::new(DioxusServerContext::default() ));
|
||||
pub(crate) static SERVER_CONTEXT: std::cell::RefCell<Box<DioxusServerContext>> = Default::default();
|
||||
}
|
||||
|
||||
/// Get information about the current server request.
|
||||
@@ -218,7 +218,7 @@ impl<T: Send + Sync + Clone + 'static> FromServerContext for FromContext<T> {
|
||||
type Rejection = NotFoundInServerContext<T>;
|
||||
|
||||
async fn from_request(req: &DioxusServerContext) -> Result<Self, Self::Rejection> {
|
||||
Ok(Self(req.clone().get::<T>().ok_or_else(|| {
|
||||
Ok(Self(req.clone().get::<T>().ok_or({
|
||||
NotFoundInServerContext::<T>(std::marker::PhantomData::<T>)
|
||||
})?))
|
||||
}
|
||||
@@ -239,6 +239,6 @@ impl<
|
||||
type Rejection = R;
|
||||
|
||||
async fn from_request(req: &DioxusServerContext) -> Result<Self, Self::Rejection> {
|
||||
Ok(I::from_request_parts(&mut *req.request_parts_mut(), &()).await?)
|
||||
Ok(I::from_request_parts(&mut req.request_parts_mut(), &()).await?)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user