mirror of
https://github.com/leptos-rs/leptos
synced 2024-09-20 06:21:57 +00:00
First pass of method to generate routelist
This commit is contained in:
parent
aef589cd24
commit
63b1837315
6 changed files with 71 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
use leptos_router::generate_route_list;
|
||||||
mod todo;
|
mod todo;
|
||||||
|
|
||||||
// boilerplate to run in different modes
|
// boilerplate to run in different modes
|
||||||
|
@ -28,6 +29,10 @@ cfg_if! {
|
||||||
|
|
||||||
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
|
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
|
||||||
let addr = conf.leptos_options.site_address.clone();
|
let addr = conf.leptos_options.site_address.clone();
|
||||||
|
println!("BEFFOORE");
|
||||||
|
let routes = generate_route_list(|cx| view! { cx, <TodoApp/> });
|
||||||
|
println!("HIIIIIIIIIIII");
|
||||||
|
println!("Routes: {:#?}", routes);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let leptos_options = &conf.leptos_options;
|
let leptos_options = &conf.leptos_options;
|
||||||
|
|
|
@ -38,9 +38,11 @@ cfg_if! {
|
||||||
pub async fn get_todos(cx: Scope) -> Result<Vec<Todo>, ServerFnError> {
|
pub async fn get_todos(cx: Scope) -> Result<Vec<Todo>, ServerFnError> {
|
||||||
// this is just an example of how to access server context injected in the handlers
|
// this is just an example of how to access server context injected in the handlers
|
||||||
let req =
|
let req =
|
||||||
use_context::<actix_web::HttpRequest>(cx).expect("couldn't get HttpRequest from context");
|
use_context::<actix_web::HttpRequest>(cx);
|
||||||
println!("req.path = {:?}", req.path());
|
|
||||||
|
|
||||||
|
if let Some(req) = req{
|
||||||
|
println!("req.path = {:#?}", req.path());
|
||||||
|
}
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
|
|
||||||
let mut conn = db().await?;
|
let mut conn = db().await?;
|
||||||
|
@ -103,6 +105,10 @@ pub fn TodoApp(cx: Scope) -> impl IntoView {
|
||||||
cx,
|
cx,
|
||||||
<Todos/>
|
<Todos/>
|
||||||
}/>
|
}/>
|
||||||
|
<Route path="potato" view=|cx| view! {
|
||||||
|
cx,
|
||||||
|
<Todos/>
|
||||||
|
}/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</main>
|
</main>
|
||||||
</Router>
|
</Router>
|
||||||
|
|
|
@ -11,8 +11,8 @@ use wasm_bindgen::JsCast;
|
||||||
use leptos_reactive::use_transition;
|
use leptos_reactive::use_transition;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
create_location, matching::resolve_path, History, Location, LocationChange, RouteContext,
|
create_location, matching::resolve_path, Branch, History, Location, LocationChange,
|
||||||
RouterIntegrationContext, State,
|
RouteContext, RouterIntegrationContext, State,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "ssr"))]
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
@ -49,6 +49,7 @@ pub struct RouterContext {
|
||||||
pub(crate) struct RouterContextInner {
|
pub(crate) struct RouterContextInner {
|
||||||
pub location: Location,
|
pub location: Location,
|
||||||
pub base: RouteContext,
|
pub base: RouteContext,
|
||||||
|
pub possible_routes: RefCell<Option<Vec<Branch>>>,
|
||||||
base_path: String,
|
base_path: String,
|
||||||
history: Box<dyn History>,
|
history: Box<dyn History>,
|
||||||
cx: Scope,
|
cx: Scope,
|
||||||
|
@ -104,7 +105,7 @@ impl RouterContext {
|
||||||
value: base_path.to_string(),
|
value: base_path.to_string(),
|
||||||
replace: true,
|
replace: true,
|
||||||
scroll: false,
|
scroll: false,
|
||||||
state: State(None)
|
state: State(None),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,6 +154,7 @@ impl RouterContext {
|
||||||
referrers,
|
referrers,
|
||||||
state,
|
state,
|
||||||
set_state,
|
set_state,
|
||||||
|
possible_routes: Default::default(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle all click events on anchor tags
|
// handle all click events on anchor tags
|
||||||
|
@ -175,6 +177,15 @@ impl RouterContext {
|
||||||
pub fn base(&self) -> RouteContext {
|
pub fn base(&self) -> RouteContext {
|
||||||
self.inner.base.clone()
|
self.inner.base.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A list of all possible routes this router can match.
|
||||||
|
pub fn possible_branches(&self) -> Vec<Branch> {
|
||||||
|
self.inner
|
||||||
|
.possible_routes
|
||||||
|
.borrow()
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RouterContextInner {
|
impl RouterContextInner {
|
||||||
|
|
|
@ -12,7 +12,7 @@ use crate::{
|
||||||
expand_optionals, get_route_matches, join_paths, Branch, Matcher, RouteDefinition,
|
expand_optionals, get_route_matches, join_paths, Branch, Matcher, RouteDefinition,
|
||||||
RouteMatch,
|
RouteMatch,
|
||||||
},
|
},
|
||||||
RouteContext, RouterContext,
|
PossibleBranchContext, RouteContext, RouterContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Contains route definitions and manages the actual routing process.
|
/// Contains route definitions and manages the actual routing process.
|
||||||
|
@ -42,6 +42,7 @@ pub fn Routes(
|
||||||
})
|
})
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
create_branches(
|
create_branches(
|
||||||
&children,
|
&children,
|
||||||
&base.unwrap_or_default(),
|
&base.unwrap_or_default(),
|
||||||
|
@ -49,6 +50,10 @@ pub fn Routes(
|
||||||
&mut branches,
|
&mut branches,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(context) = use_context::<PossibleBranchContext>(cx) {
|
||||||
|
*context.0.borrow_mut() = branches.clone();
|
||||||
|
}
|
||||||
|
|
||||||
// whenever path changes, update matches
|
// whenever path changes, update matches
|
||||||
let matches = create_memo(cx, {
|
let matches = create_memo(cx, {
|
||||||
let router = router.clone();
|
let router = router.clone();
|
||||||
|
|
34
router/src/extract_routes.rs
Normal file
34
router/src/extract_routes.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
use leptos::*;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
use crate::{Branch, RouterIntegrationContext, ServerIntegration};
|
||||||
|
|
||||||
|
/// Context to contain all possible routes.
|
||||||
|
#[derive(Clone, Default, Debug)]
|
||||||
|
pub struct PossibleBranchContext(pub(crate) Rc<RefCell<Vec<Branch>>>);
|
||||||
|
|
||||||
|
/// Generates a list of all routes this application could possibly serve.
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
pub fn generate_route_list<IV>(app_fn: impl FnOnce(Scope) -> IV + 'static) -> Vec<String>
|
||||||
|
where
|
||||||
|
IV: IntoView + 'static,
|
||||||
|
{
|
||||||
|
let runtime = create_runtime();
|
||||||
|
run_scope(runtime, move |cx| {
|
||||||
|
let integration = ServerIntegration {
|
||||||
|
path: "http://leptos.rs/".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
provide_context(cx, RouterIntegrationContext::new(integration));
|
||||||
|
let branches = PossibleBranchContext::default();
|
||||||
|
provide_context(cx, branches.clone());
|
||||||
|
|
||||||
|
let _ = app_fn(cx).into_view(cx);
|
||||||
|
|
||||||
|
let branches = branches.0.borrow();
|
||||||
|
branches
|
||||||
|
.iter()
|
||||||
|
.flat_map(|branch| branch.routes.last().map(|route| route.pattern.clone()))
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
}
|
|
@ -184,12 +184,14 @@
|
||||||
#![cfg_attr(not(feature = "stable"), feature(type_name_of_val))]
|
#![cfg_attr(not(feature = "stable"), feature(type_name_of_val))]
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
|
mod extract_routes;
|
||||||
mod history;
|
mod history;
|
||||||
mod hooks;
|
mod hooks;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod matching;
|
pub mod matching;
|
||||||
|
|
||||||
pub use components::*;
|
pub use components::*;
|
||||||
|
pub use extract_routes::*;
|
||||||
pub use history::*;
|
pub use history::*;
|
||||||
pub use hooks::*;
|
pub use hooks::*;
|
||||||
pub use matching::*;
|
pub use matching::*;
|
||||||
|
|
Loading…
Reference in a new issue