fix: add leptos_axum::build_static_routes (closes #1843) (#1855)

This commit is contained in:
Greg Johnston 2023-10-27 17:09:52 -04:00 committed by GitHub
parent a11c6303e2
commit 777095670e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 38 deletions

View file

@ -1099,38 +1099,41 @@ where
let full_path = format!("http://leptos.dev{path}");
let (tx, rx) = futures::channel::oneshot::channel();
let local_pool = get_leptos_pool();
local_pool.spawn_pinned(move || {
async move {
let app = {
let full_path = full_path.clone();
let (req, req_parts) = generate_request_and_parts(req).await;
move || {
provide_contexts(full_path, req_parts, req.into(), default_res_options);
app_fn().into_view()
}
};
let (stream, runtime) =
render_to_stream_in_order_with_prefix_undisposed_with_context(
app,
|| "".into(),
add_context,
spawn_task!(async move {
let app = {
let full_path = full_path.clone();
let (req, req_parts) =
generate_request_and_parts(req).await;
move || {
provide_contexts(
full_path,
req_parts,
req.into(),
default_res_options,
);
app_fn().into_view()
}
};
// Extract the value of ResponseOptions from here
let res_options =
use_context::<ResponseOptions>().unwrap();
let (stream, runtime) =
render_to_stream_in_order_with_prefix_undisposed_with_context(
app,
|| "".into(),
add_context,
);
let html = build_async_response(stream, &options, runtime).await;
// Extract the value of ResponseOptions from here
let res_options = use_context::<ResponseOptions>().unwrap();
let new_res_parts = res_options.0.read().clone();
let html =
build_async_response(stream, &options, runtime).await;
let mut writable = res_options2.0.write();
*writable = new_res_parts;
let new_res_parts = res_options.0.read().clone();
_ = tx.send(html);
}
let mut writable = res_options2.0.write();
*writable = new_res_parts;
_ = tx.send(html);
});
let html = rx.await.expect("to complete HTML rendering");
@ -1323,6 +1326,29 @@ where
generate_route_list_with_exclusions_and_ssg(app_fn, excluded_routes).0
}
/// TODO docs
pub async fn build_static_routes<IV>(
options: &LeptosOptions,
app_fn: impl Fn() -> IV + 'static + Send + Clone,
routes: &[RouteListing],
static_data_map: StaticDataMap,
) where
IV: IntoView + 'static,
{
let options = options.clone();
let routes = routes.to_owned();
spawn_task!(async move {
leptos_router::build_static_routes(
&options,
app_fn,
&routes,
&static_data_map,
)
.await
.expect("could not build static 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
@ -1535,8 +1561,7 @@ where
async move {
let (tx, rx) = futures::channel::oneshot::channel();
let local_pool = get_leptos_pool();
local_pool.spawn_pinned(move || async move {
spawn_task!(async move {
let res = incremental_static_route(
tokio::fs::read_to_string(static_file_path(
&options, &path,
@ -1579,8 +1604,7 @@ where
async move {
let (tx, rx) = futures::channel::oneshot::channel();
let local_pool = get_leptos_pool();
local_pool.spawn_pinned(move || async move {
spawn_task!(async move {
let res = upfront_static_route(
tokio::fs::read_to_string(static_file_path(
&options, &path,

View file

@ -10,6 +10,7 @@ use std::{
future::Future,
pin::Pin,
rc::Rc,
sync::Arc,
};
thread_local! {
@ -162,8 +163,7 @@ pub fn StaticRoute<E, F, P, S>(
/// or `|| view! { <MyComponent/>` } or even, for a component with no props, `MyComponent`).
view: F,
/// Creates a map of the params that should be built for a particular route.
#[prop(optional)]
static_params: Option<S>,
static_params: S,
/// The static route mode
#[prop(optional)]
mode: StaticMode,
@ -179,7 +179,10 @@ where
E: IntoView,
F: Fn() -> E + 'static,
P: std::fmt::Display,
S: Fn() -> Pin<Box<dyn Future<Output = StaticParamsMap>>> + 'static,
S: Fn() -> Pin<Box<dyn Future<Output = StaticParamsMap> + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
define_route(
children,
@ -189,7 +192,7 @@ where
&[Method::Get],
data,
Some(mode),
static_params.map(|s| Rc::new(s) as _),
Some(Arc::new(static_params)),
)
}

View file

@ -15,7 +15,7 @@ use std::{
hash::{Hash, Hasher},
path::PathBuf,
pin::Pin,
rc::Rc,
sync::Arc,
};
#[derive(Debug, Default, Serialize, Deserialize)]
@ -304,10 +304,12 @@ pub fn purge_all_static_routes<IV>(
purge_dir_of_static_files(Path::new(&options.site_root).to_path_buf())
}
pub type StaticData = Rc<StaticDataFn>;
pub type StaticData = Arc<StaticDataFn>;
pub type StaticDataFn =
dyn Fn() -> Pin<Box<dyn Future<Output = StaticParamsMap>>> + 'static;
pub type StaticDataFn = dyn Fn() -> Pin<Box<dyn Future<Output = StaticParamsMap> + Send + Sync>>
+ Send
+ Sync
+ 'static;
pub type StaticDataMap = HashMap<String, Option<StaticData>>;