From 14b4af4bbc1576b8e185521f54912bcdd634b699 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Fri, 16 Feb 2024 16:22:13 -0600 Subject: [PATCH] fix cargo check all features except the router --- .../examples/axum-desktop/Cargo.toml | 2 +- .../examples/axum-desktop/src/server.rs | 2 +- .../examples/static-hydrated/src/main.rs | 29 ++-- packages/fullstack/src/axum_adapter.rs | 5 +- packages/fullstack/src/server_context.rs | 28 ++-- packages/router/benches/incremental.rs | 131 +++++++++--------- packages/router/src/router_cfg.rs | 1 - 7 files changed, 105 insertions(+), 93 deletions(-) diff --git a/packages/fullstack/examples/axum-desktop/Cargo.toml b/packages/fullstack/examples/axum-desktop/Cargo.toml index d789b48e5..bae2a9241 100644 --- a/packages/fullstack/examples/axum-desktop/Cargo.toml +++ b/packages/fullstack/examples/axum-desktop/Cargo.toml @@ -7,7 +7,7 @@ publish = false [lib] [dependencies] -dioxus = { workspace = true, features = ["launch"] } +dioxus = { workspace = true, features = ["launch", "fullstack"] } axum = { workspace = true, optional = true } tokio = { workspace = true, features = ["full"], optional = true } serde = "1.0.159" diff --git a/packages/fullstack/examples/axum-desktop/src/server.rs b/packages/fullstack/examples/axum-desktop/src/server.rs index f14f58abe..68912f452 100644 --- a/packages/fullstack/examples/axum-desktop/src/server.rs +++ b/packages/fullstack/examples/axum-desktop/src/server.rs @@ -5,7 +5,7 @@ use axum_desktop::*; use dioxus::prelude::*; -use dioxus_fullstack::server_fn::axum::register_explicit; +use server_fn::axum::register_explicit; #[tokio::main] async fn main() { diff --git a/packages/fullstack/examples/static-hydrated/src/main.rs b/packages/fullstack/examples/static-hydrated/src/main.rs index c3c988e49..3fb981cee 100644 --- a/packages/fullstack/examples/static-hydrated/src/main.rs +++ b/packages/fullstack/examples/static-hydrated/src/main.rs @@ -14,16 +14,25 @@ use serde::{Deserialize, Serialize}; #[cfg(feature = "server")] #[tokio::main] async fn main() { - pre_cache_static_routes( - &ServeConfig::new_with_router( - dioxus_fullstack::router::FullstackRouterConfig::::default(), - ) - .assets_path("docs") - .incremental(IncrementalRendererConfig::default().static_dir("docs")) - .build(), - ) - .await - .unwrap(); + let wrapper = DefaultRenderer { + before_body: r#" + + + + + Dioxus Application + + "# + .to_string(), + after_body: r#" + "# + .to_string(), + }; + let mut renderer = IncrementalRenderer::builder().build(); + pre_cache_static_routes::(&mut renderer, &wrapper) + .await + .unwrap(); } // Hydrate the page diff --git a/packages/fullstack/src/axum_adapter.rs b/packages/fullstack/src/axum_adapter.rs index 3ccef1403..07bd1ff2e 100644 --- a/packages/fullstack/src/axum_adapter.rs +++ b/packages/fullstack/src/axum_adapter.rs @@ -361,7 +361,8 @@ pub async fn render_handler_with_context( ) -> impl IntoResponse { let (parts, _) = request.into_parts(); let url = parts.uri.path_and_query().unwrap().to_string(); - let parts: Arc> = Arc::new(RwLock::new(parts.into())); + let parts: Arc> = + Arc::new(tokio::sync::RwLock::new(parts.into())); let mut server_context = DioxusServerContext::new(parts.clone()); inject_context(&mut server_context); @@ -483,7 +484,7 @@ async fn handle_server_fns_inner( server_fn::axum::get_server_fn_service(&path_string) { - let server_context = DioxusServerContext::new(Arc::new(RwLock::new(parts))); + let server_context = DioxusServerContext::new(Arc::new(tokio::sync::RwLock::new(parts))); additional_context(); // store Accepts and Referrer in case we need them for redirect (below) diff --git a/packages/fullstack/src/server_context.rs b/packages/fullstack/src/server_context.rs index 50e657f40..f70d8548e 100644 --- a/packages/fullstack/src/server_context.rs +++ b/packages/fullstack/src/server_context.rs @@ -12,7 +12,7 @@ pub struct DioxusServerContext { std::sync::RwLock>, >, response_parts: std::sync::Arc>, - pub(crate) parts: Arc>, + pub(crate) parts: Arc>, html_data: Arc>, } @@ -24,7 +24,9 @@ impl Default for DioxusServerContext { response_parts: std::sync::Arc::new(RwLock::new( http::response::Response::new(()).into_parts().0, )), - parts: std::sync::Arc::new(RwLock::new(http::request::Request::new(()).into_parts().0)), + parts: std::sync::Arc::new(tokio::sync::RwLock::new( + http::request::Request::new(()).into_parts().0, + )), html_data: Arc::new(RwLock::new(HTMLData::default())), } } @@ -40,7 +42,7 @@ mod server_fn_impl { impl DioxusServerContext { /// Create a new server context from a request - pub fn new(parts: impl Into>>) -> Self { + pub fn new(parts: impl Into>>) -> Self { Self { parts: parts.into(), shared_context: Arc::new(RwLock::new(SendSyncAnyMap::new())), @@ -82,19 +84,15 @@ mod server_fn_impl { /// Get the request that triggered: /// - The initial SSR render if called from a ScopeState or ServerFn /// - The server function to be called if called from a server function after the initial render - pub fn request_parts( - &self, - ) -> std::sync::LockResult> { - self.parts.read() + pub fn request_parts(&self) -> tokio::sync::RwLockReadGuard<'_, http::request::Parts> { + self.parts.blocking_read() } /// Get the request that triggered: /// - The initial SSR render if called from a ScopeState or ServerFn /// - The server function to be called if called from a server function after the initial render - pub fn request_parts_mut( - &self, - ) -> std::sync::LockResult> { - self.parts.write() + pub fn request_parts_mut(&self) -> tokio::sync::RwLockWriteGuard<'_, http::request::Parts> { + self.parts.blocking_write() } /// Extract some part from the request @@ -185,7 +183,7 @@ impl std::future::Future for ProvideServerContext { } /// A trait for extracting types from the server context -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] pub trait FromServerContext: Sized { /// The error type returned when extraction fails. This type must implement `std::error::Error`. type Rejection: std::error::Error; @@ -215,7 +213,7 @@ impl std::error::Error for NotFoundInServerContext {} pub struct FromContext(pub(crate) T); -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl FromServerContext for FromContext { type Rejection = NotFoundInServerContext; @@ -232,7 +230,7 @@ impl FromServerContext for FromContext { pub struct Axum; #[cfg(feature = "axum")] -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl< I: axum::extract::FromRequestParts<(), Rejection = R>, R: axum::response::IntoResponse + std::error::Error, @@ -241,6 +239,6 @@ impl< type Rejection = R; async fn from_request(req: &DioxusServerContext) -> Result { - Ok(I::from_request_parts(&mut *req.request_parts_mut().unwrap(), &()).await?) + Ok(I::from_request_parts(&mut *req.request_parts_mut(), &()).await?) } } diff --git a/packages/router/benches/incremental.rs b/packages/router/benches/incremental.rs index 14a7eadfa..b851ee79d 100644 --- a/packages/router/benches/incremental.rs +++ b/packages/router/benches/incremental.rs @@ -11,7 +11,45 @@ use dioxus_ssr::Renderer; pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("build 1000 routes", |b| { - let mut renderer = IncrementalRenderer::builder(DefaultRenderer { + let mut renderer = IncrementalRenderer::builder() + .static_dir("./static") + .invalidate_after(Duration::from_secs(10)) + .build(); + let wrapper = DefaultRenderer { + before_body: r#" + + + + + Dioxus Application + + "# + .to_string(), + after_body: r#" + "# + .to_string(), + }; + + b.iter(|| { + tokio::runtime::Runtime::new().unwrap().block_on(async { + for id in 0..1000 { + render_route( + &mut renderer, + Route::Post { id }, + &mut tokio::io::sink(), + |_| Box::pin(async move {}), + &wrapper, + ) + .await + .unwrap(); + } + }) + }) + }); + + c.bench_function("build 1000 routes no memory cache", |b| { + let wrapper = DefaultRenderer { before_body: r#" @@ -25,56 +63,22 @@ pub fn criterion_benchmark(c: &mut Criterion) { after_body: r#" "# .to_string(), - }) - .static_dir("./static") - .invalidate_after(Duration::from_secs(10)) - .build(); - - b.iter(|| { - tokio::runtime::Runtime::new().unwrap().block_on(async { - for id in 0..1000 { - render_route( - &mut renderer, - Route::Post { id }, - &mut tokio::io::sink(), - |_| {}, - ) - .await - .unwrap(); - } - }) - }) - }); - - c.bench_function("build 1000 routes no memory cache", |b| { + }; b.to_async(tokio::runtime::Runtime::new().unwrap()) .iter(|| async { - let mut renderer = IncrementalRenderer::builder(DefaultRenderer { - before_body: r#" - - - - - Dioxus Application - - "# - .to_string(), - after_body: r#" - "# - .to_string(), - }) - .static_dir("./static") - .memory_cache_limit(0) - .invalidate_after(Duration::from_secs(10)) - .build(); + let mut renderer = IncrementalRenderer::builder() + .static_dir("./static") + .memory_cache_limit(0) + .invalidate_after(Duration::from_secs(10)) + .build(); for id in 0..1000 { render_route( &mut renderer, Route::Post { id }, &mut tokio::io::sink(), - |_| {}, + |_| Box::pin(async move {}), + &wrapper, ) .await .unwrap(); @@ -91,7 +95,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { RenderPathProps::builder().path(Route::Post { id }).build(), ); - vdom.rebuild(); + vdom.rebuild_in_place(); struct Ignore; @@ -106,27 +110,28 @@ pub fn criterion_benchmark(c: &mut Criterion) { }) }); c.bench_function("cache static", |b| { + let wrapper = DefaultRenderer { + before_body: r#" + + + + + Dioxus Application + + "# + .to_string(), + after_body: r#" + "# + .to_string(), + }; b.to_async(tokio::runtime::Runtime::new().unwrap()) .iter(|| async { - let mut renderer = IncrementalRenderer::builder(DefaultRenderer { - before_body: r#" - - - - - Dioxus Application - - "# - .to_string(), - after_body: r#" - "# - .to_string(), - }) - .static_dir("./static") - .build(); + let mut renderer = IncrementalRenderer::builder() + .static_dir("./static") + .build(); - pre_cache_static_routes::(&mut renderer) + pre_cache_static_routes::(&mut renderer, &wrapper) .await .unwrap(); }) @@ -148,7 +153,7 @@ fn Blog() -> Element { #[component] fn Post(id: usize) -> Element { rsx! { - for _ in 0..*id { + for _ in 0..id { div { "PostId: {id}" } diff --git a/packages/router/src/router_cfg.rs b/packages/router/src/router_cfg.rs index 559c72393..8ec762b14 100644 --- a/packages/router/src/router_cfg.rs +++ b/packages/router/src/router_cfg.rs @@ -135,7 +135,6 @@ where MemoryHistory::::with_initial_path( dioxus_fullstack::prelude::server_context() .request_parts() - .unwrap() .uri .to_string() .parse()