From c61b435c0a6b7e9b4fccccf72700082b8f4b686e Mon Sep 17 00:00:00 2001 From: Niklas Eicker Date: Sat, 16 Dec 2023 21:55:10 +0100 Subject: [PATCH 1/3] Support additional context for route generation --- integrations/actix/src/lib.rs | 9 +++++---- integrations/axum/src/lib.rs | 9 +++++---- integrations/viz/src/lib.rs | 10 ++++++---- router/src/extract_routes.rs | 3 +++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index 982d41c7c7..de01e52ff7 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -886,7 +886,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -898,7 +898,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None) + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -912,7 +912,7 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -922,12 +922,13 @@ where pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, + additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (mut routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn); + leptos_router::generate_route_list_inner(app_fn, additional_context); // Actix's Router doesn't follow Leptos's // Match `*` or `*someword` to replace with replace it with "/{tail.*} diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index 2eb9a9509b..ac3a1cfc56 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -1301,7 +1301,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1314,7 +1314,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None) + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1329,7 +1329,7 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 } /// TODO docs @@ -1363,12 +1363,13 @@ pub async fn build_static_routes( pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, + additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn); + leptos_router::generate_route_list_inner(app_fn, additional_context); // Axum's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/integrations/viz/src/lib.rs b/integrations/viz/src/lib.rs index d1870b979a..39f7df70b4 100644 --- a/integrations/viz/src/lib.rs +++ b/integrations/viz/src/lib.rs @@ -1000,7 +1000,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1012,7 +1012,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None) + generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1025,20 +1025,22 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 } + /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically /// create routes in Viz's Router without having to use wildcard matching or fallbacks. Takes in your root app Element /// as an argument so it can walk you app tree. This version is tailored to generate Viz compatible paths. pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, + additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn); + leptos_router::generate_route_list_inner(app_fn, additional_context); // Viz's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/router/src/extract_routes.rs b/router/src/extract_routes.rs index b85aff1e1d..4c5ef0679c 100644 --- a/router/src/extract_routes.rs +++ b/router/src/extract_routes.rs @@ -108,6 +108,7 @@ impl RouteListing { /// [`viz`]: pub fn generate_route_list_inner( app_fn: impl Fn() -> IV + 'static + Clone, + additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, @@ -122,6 +123,8 @@ where let branches = PossibleBranchContext::default(); provide_context(branches.clone()); + additional_context(); + leptos::suppress_resource_load(true); _ = app_fn().into_view(); leptos::suppress_resource_load(false); From 18b9ac6134bc143893d02327e31f77712c388443 Mon Sep 17 00:00:00 2001 From: Niklas Eicker Date: Sat, 16 Dec 2023 22:15:03 +0100 Subject: [PATCH 2/3] Create extra methods for passing additional context --- integrations/actix/src/lib.rs | 23 +++++++++++++++++++---- integrations/axum/src/lib.rs | 24 ++++++++++++++++++++---- integrations/viz/src/lib.rs | 22 ++++++++++++++++++---- router/src/extract_routes.rs | 15 +++++++++++++++ 4 files changed, 72 insertions(+), 12 deletions(-) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index de01e52ff7..3e3a173349 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -886,7 +886,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -898,7 +898,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) + generate_route_list_with_exclusions_and_ssg(app_fn, None) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -912,7 +912,7 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -922,13 +922,28 @@ where pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, +) -> (Vec, StaticDataMap) + where + IV: IntoView + 'static, +{ + generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) +} + +/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically +/// create routes in Actix's App without having to use wildcard matching or fallbacks. Takes in your root app Element +/// as an argument so it can walk you app tree. This version is tailored to generated Actix compatible paths. Adding excluded_routes +/// to this function will stop `.leptos_routes()` from generating a route for it, allowing a custom handler. These need to be in Actix path format. +/// Additional context will be provided to the app Element. +pub fn generate_route_list_with_exclusions_and_ssg_and_context( + app_fn: impl Fn() -> IV + 'static + Clone, + excluded_routes: Option>, additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (mut routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); // Actix's Router doesn't follow Leptos's // Match `*` or `*someword` to replace with replace it with "/{tail.*} diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index ac3a1cfc56..e0fb2d81d2 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -1301,7 +1301,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1314,7 +1314,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) + generate_route_list_with_exclusions_and_ssg(app_fn, None) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1329,7 +1329,7 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 } /// TODO docs @@ -1363,13 +1363,29 @@ pub async fn build_static_routes( pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, +) -> (Vec, StaticDataMap) + where + IV: IntoView + 'static, +{ + generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) +} + +/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically +/// create routes in Axum's Router without having to use wildcard matching or fallbacks. Takes in your root app Element +/// as an argument so it can walk you app tree. This version is tailored to generate Axum compatible paths. Adding excluded_routes +/// to this function will stop `.leptos_routes()` from generating a route for it, allowing a custom handler. These need to be in Axum path format +/// Additional context will be provided to the app Element. +#[tracing::instrument(level = "trace", fields(error), skip_all)] +pub fn generate_route_list_with_exclusions_and_ssg_and_context( + app_fn: impl Fn() -> IV + 'static + Clone, + excluded_routes: Option>, additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); // Axum's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/integrations/viz/src/lib.rs b/integrations/viz/src/lib.rs index 39f7df70b4..8abbfbfcc5 100644 --- a/integrations/viz/src/lib.rs +++ b/integrations/viz/src/lib.rs @@ -1000,7 +1000,7 @@ pub fn generate_route_list( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, None).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1012,7 +1012,7 @@ pub fn generate_route_list_with_ssg( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, None, || {}) + generate_route_list_with_exclusions_and_ssg(app_fn, None) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1025,7 +1025,7 @@ pub fn generate_route_list_with_exclusions( where IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes, || {}).0 + generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0 } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1034,13 +1034,27 @@ where pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, +) -> (Vec, StaticDataMap) + where + IV: IntoView + 'static, +{ + generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) +} + +/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically +/// create routes in Viz's Router without having to use wildcard matching or fallbacks. Takes in your root app Element +/// as an argument so it can walk you app tree. This version is tailored to generate Viz compatible paths. +/// Additional context will be provided to the app Element. +pub fn generate_route_list_with_exclusions_and_ssg_and_context( + app_fn: impl Fn() -> IV + 'static + Clone, + excluded_routes: Option>, additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); // Viz's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/router/src/extract_routes.rs b/router/src/extract_routes.rs index 4c5ef0679c..50cfc2c9e8 100644 --- a/router/src/extract_routes.rs +++ b/router/src/extract_routes.rs @@ -108,6 +108,21 @@ impl RouteListing { /// [`viz`]: pub fn generate_route_list_inner( app_fn: impl Fn() -> IV + 'static + Clone, +) -> (Vec, StaticDataMap) + where + IV: IntoView + 'static, +{ + generate_route_list_inner_with_context(app_fn, ||{}) +} +/// Generates a list of all routes this application could possibly serve. This returns the raw routes in the leptos_router +/// format. Odds are you want `generate_route_list()` from either the [`actix`], [`axum`], or [`viz`] integrations if you want +/// to work with their router. +/// +/// [`actix`]: +/// [`axum`]: +/// [`viz`]: +pub fn generate_route_list_inner_with_context( + app_fn: impl Fn() -> IV + 'static + Clone, additional_context: impl Fn() + 'static + Clone, ) -> (Vec, StaticDataMap) where From 79514de053dd7c8d0a2dbca65c1d5ce8605400b3 Mon Sep 17 00:00:00 2001 From: Niklas Eicker Date: Sat, 16 Dec 2023 22:15:46 +0100 Subject: [PATCH 3/3] Format --- integrations/actix/src/lib.rs | 15 +++++++++++---- integrations/axum/src/lib.rs | 15 +++++++++++---- integrations/viz/src/lib.rs | 15 +++++++++++---- router/src/extract_routes.rs | 6 +++--- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index 3e3a173349..7b41d156c6 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -923,10 +923,14 @@ pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, ) -> (Vec, StaticDataMap) - where - IV: IntoView + 'static, +where + IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) + generate_route_list_with_exclusions_and_ssg_and_context( + app_fn, + excluded_routes, + || {}, + ) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -943,7 +947,10 @@ where IV: IntoView + 'static, { let (mut routes, static_data_map) = - leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context( + app_fn, + additional_context, + ); // Actix's Router doesn't follow Leptos's // Match `*` or `*someword` to replace with replace it with "/{tail.*} diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index e0fb2d81d2..24eede3ad3 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -1364,10 +1364,14 @@ pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, ) -> (Vec, StaticDataMap) - where - IV: IntoView + 'static, +where + IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) + generate_route_list_with_exclusions_and_ssg_and_context( + app_fn, + excluded_routes, + || {}, + ) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1385,7 +1389,10 @@ where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context( + app_fn, + additional_context, + ); // Axum's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/integrations/viz/src/lib.rs b/integrations/viz/src/lib.rs index 8abbfbfcc5..f5648217cd 100644 --- a/integrations/viz/src/lib.rs +++ b/integrations/viz/src/lib.rs @@ -1035,10 +1035,14 @@ pub fn generate_route_list_with_exclusions_and_ssg( app_fn: impl Fn() -> IV + 'static + Clone, excluded_routes: Option>, ) -> (Vec, StaticDataMap) - where - IV: IntoView + 'static, +where + IV: IntoView + 'static, { - generate_route_list_with_exclusions_and_ssg_and_context(app_fn, excluded_routes, || {}) + generate_route_list_with_exclusions_and_ssg_and_context( + app_fn, + excluded_routes, + || {}, + ) } /// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically @@ -1054,7 +1058,10 @@ where IV: IntoView + 'static, { let (routes, static_data_map) = - leptos_router::generate_route_list_inner_with_context(app_fn, additional_context); + leptos_router::generate_route_list_inner_with_context( + app_fn, + additional_context, + ); // Viz's Router defines Root routes as "/" not "" let mut routes = routes .into_iter() diff --git a/router/src/extract_routes.rs b/router/src/extract_routes.rs index 50cfc2c9e8..031632f13d 100644 --- a/router/src/extract_routes.rs +++ b/router/src/extract_routes.rs @@ -109,10 +109,10 @@ impl RouteListing { pub fn generate_route_list_inner( app_fn: impl Fn() -> IV + 'static + Clone, ) -> (Vec, StaticDataMap) - where - IV: IntoView + 'static, +where + IV: IntoView + 'static, { - generate_route_list_inner_with_context(app_fn, ||{}) + generate_route_list_inner_with_context(app_fn, || {}) } /// Generates a list of all routes this application could possibly serve. This returns the raw routes in the leptos_router /// format. Odds are you want `generate_route_list()` from either the [`actix`], [`axum`], or [`viz`] integrations if you want