From 997b99081bfb9172c3ef098303b907ae52e4f5fe Mon Sep 17 00:00:00 2001 From: Niklas Eicker Date: Sun, 21 Jan 2024 19:33:05 +0100 Subject: [PATCH] change: for static routes, remove `.static` and provide additional context for static_params closures (#2207) --- router/src/components/static_render.rs | 53 ++++++++------------------ 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/router/src/components/static_render.rs b/router/src/components/static_render.rs index 81712d2523..58245a79ff 100644 --- a/router/src/components/static_render.rs +++ b/router/src/components/static_render.rs @@ -1,7 +1,7 @@ #[cfg(feature = "ssr")] use crate::{RouteListing, RouterIntegrationContext, ServerIntegration}; #[cfg(feature = "ssr")] -use leptos::{provide_context, IntoView, LeptosOptions}; +use leptos::{create_runtime, provide_context, IntoView, LeptosOptions}; #[cfg(feature = "ssr")] use leptos_meta::MetaContext; use linear_map::LinearMap; @@ -204,9 +204,8 @@ impl ResolvedStaticPath { IV: IntoView + 'static, { let html = self.build(options, app_fn, additional_context).await; - let path = Path::new(&options.site_root) - .join(format!("{}.static.html", self.0.trim_start_matches('/'))); - + let file_path = static_file_path(options, &self.0); + let path = Path::new(&file_path); if let Some(path) = path.parent() { std::fs::create_dir_all(path)? } @@ -247,12 +246,15 @@ where IV: IntoView + 'static, { let mut static_data: HashMap<&str, StaticParamsMap> = HashMap::new(); + let runtime = create_runtime(); + additional_context(); for (key, value) in static_data_map { match value { Some(value) => static_data.insert(key, value.as_ref()().await), None => static_data.insert(key, StaticParamsMap::default()), }; } + runtime.dispose(); let static_routes = routes .iter() .filter(|route| route.static_mode().is_some()) @@ -277,33 +279,6 @@ where Ok(()) } -#[doc(hidden)] -#[cfg(feature = "ssr")] -pub fn purge_dir_of_static_files(path: PathBuf) -> Result<(), std::io::Error> { - for entry in path.read_dir()? { - let entry = entry?; - let path = entry.path(); - if path.is_dir() { - purge_dir_of_static_files(path)?; - } else if path.is_file() { - if let Some(name) = path.file_name().and_then(|i| i.to_str()) { - if name.ends_with(".static.html") { - std::fs::remove_file(path)?; - } - } - } - } - Ok(()) -} - -/// Purge all statically generated route files -#[cfg(feature = "ssr")] -pub fn purge_all_static_routes( - options: &LeptosOptions, -) -> Result<(), std::io::Error> { - purge_dir_of_static_files(Path::new(&options.site_root).to_path_buf()) -} - pub type StaticData = Arc; pub type StaticDataFn = dyn Fn() -> Pin + Send + Sync>> @@ -350,17 +325,20 @@ pub enum StaticResponse { #[inline(always)] #[cfg(feature = "ssr")] pub fn static_file_path(options: &LeptosOptions, path: &str) -> String { - format!("{}{}.static.html", options.site_root, path) + let trimmed_path = path.trim_start_matches('/'); + let path = if trimmed_path.is_empty() { + "index" + } else { + trimmed_path + }; + format!("{}/{}.html", options.site_root, path) } #[doc(hidden)] #[inline(always)] #[cfg(feature = "ssr")] pub fn not_found_path(options: &LeptosOptions) -> String { - format!( - "{}{}.static.html", - options.site_root, options.not_found_path - ) + format!("{}{}.html", options.site_root, options.not_found_path) } #[doc(hidden)] @@ -443,7 +421,6 @@ where let body = ResolvedStaticPath(path.into()) .build(options, app_fn, additional_context) .await; - let path = Path::new(&options.site_root) - .join(format!("{}.static.html", path.trim_start_matches('/'))); + let path = Path::new(&static_file_path(options, path)).into(); StaticResponse::WriteFile { body, path } }