diff --git a/router/src/components/route.rs b/router/src/components/route.rs index 2be29a8f61..7469460bd7 100644 --- a/router/src/components/route.rs +++ b/router/src/components/route.rs @@ -1,7 +1,6 @@ use crate::{ matching::{resolve_path, PathMatch, RouteDefinition, RouteMatch}, ParamsMap, RouterContext, SsrMode, StaticData, StaticMode, StaticParamsMap, - StaticRenderContext, }; use leptos::{leptos_dom::Transparent, *}; use std::{ @@ -180,10 +179,7 @@ where E: IntoView, F: Fn() -> E + 'static, P: std::fmt::Display, - S: Fn( - &StaticRenderContext, - ) -> Pin>> - + 'static, + S: Fn() -> Pin>> + 'static, { define_route( children, diff --git a/router/src/components/static_render.rs b/router/src/components/static_render.rs index cd9f972ae9..a3b54110a5 100644 --- a/router/src/components/static_render.rs +++ b/router/src/components/static_render.rs @@ -5,14 +5,14 @@ use leptos::{provide_context, IntoView, LeptosOptions}; #[cfg(feature = "ssr")] use leptos_meta::MetaContext; use linear_map::LinearMap; +use serde::{Deserialize, Serialize}; #[cfg(feature = "ssr")] use std::path::Path; use std::{ - any::{Any, TypeId}, collections::HashMap, fmt::Display, future::Future, - hash::{BuildHasherDefault, Hash, Hasher}, + hash::{Hash, Hasher}, path::PathBuf, pin::Pin, rc::Rc, @@ -42,55 +42,7 @@ impl Hasher for TypeIdHasher { } } -/// A context that can be used to store application data that should be available when resolving static routes. -/// This is useful for things like database connections to pull dynamic path parameters from. -/// -/// Note that this context will be reused for every route, so you should not store any -/// route-specific data in it, nor mutate any data in it. -#[derive(Debug, Default)] -pub struct StaticRenderContext( - HashMap, BuildHasherDefault>, -); - -impl StaticRenderContext { - #[doc(hidden)] - pub fn new() -> Self { - Self::default() - } - - /// Insert a value into the context. - /// - /// # Example - /// ```rust - /// use leptos_router::StaticRenderContext; - /// - /// let mut context = StaticRenderContext::new(); - /// context.insert(42); - /// ``` - #[inline] - pub fn insert(&mut self, value: T) { - self.0.insert(TypeId::of::(), Box::new(value)); - } - - /// Get a value from the context. - /// - /// # Example - /// ```rust - /// use leptos_router::StaticRenderContext; - /// - /// let mut context = StaticRenderContext::new(); - /// context.insert(42); - /// assert_eq!(context.get::(), Some(&42)); - /// ``` - #[inline] - pub fn get(&self) -> Option<&T> { - self.0 - .get(&TypeId::of::()) - .and_then(|v| v.downcast_ref()) - } -} - -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct StaticParamsMap(pub LinearMap>); impl StaticParamsMap { @@ -289,7 +241,6 @@ impl ResolvedStaticPath { pub async fn build_static_routes( options: &LeptosOptions, app_fn: impl Fn() -> IV + 'static + Clone, - static_context: StaticRenderContext, routes: &[RouteListing], static_data_map: &StaticDataMap, ) -> Result<(), std::io::Error> @@ -300,7 +251,6 @@ where options, app_fn, || {}, - static_context, routes, static_data_map, ) @@ -312,7 +262,6 @@ pub async fn build_static_routes_with_additional_context( options: &LeptosOptions, app_fn: impl Fn() -> IV + 'static + Clone, additional_context: impl Fn() + 'static + Clone, - static_context: StaticRenderContext, routes: &[RouteListing], static_data_map: &StaticDataMap, ) -> Result<(), std::io::Error> @@ -322,9 +271,7 @@ where let mut static_data: HashMap<&str, StaticParamsMap> = HashMap::new(); for (key, value) in static_data_map { match value { - Some(value) => { - static_data.insert(key, value.as_ref()(&static_context).await) - } + Some(value) => static_data.insert(key, value.as_ref()().await), None => static_data.insert(key, StaticParamsMap::default()), }; } @@ -334,20 +281,19 @@ where .collect::>(); // TODO: maybe make this concurrent in some capacity for route in static_routes { - if route.static_mode() == Some(StaticMode::Upfront) { - let mut path = StaticPath::new(route.leptos_path()); - for p in path.parents().into_iter().rev() { - if let Some(data) = static_data.get(p.path()) { - path.add_params(data); - } - } - if let Some(data) = static_data.get(path.path()) { + let mut path = StaticPath::new(route.leptos_path()); + for p in path.parents().into_iter().rev() { + if let Some(data) = static_data.get(p.path()) { path.add_params(data); } - for path in path.into_paths() { - path.write(options, app_fn.clone(), additional_context.clone()) - .await?; - } + } + if let Some(data) = static_data.get(path.path()) { + path.add_params(data); + } + for path in path.into_paths() { + println!("building static route: {}", path); + path.write(options, app_fn.clone(), additional_context.clone()) + .await?; } } Ok(()) @@ -382,8 +328,8 @@ pub fn purge_all_static_routes( pub type StaticData = Rc; -pub type StaticDataFn = dyn Fn(&StaticRenderContext) -> Pin>> - + 'static; +pub type StaticDataFn = + dyn Fn() -> Pin>> + 'static; pub type StaticDataMap = HashMap>;