Make router work on web

This commit is contained in:
Jonathan Kelley committed 2024-01-19 16:36:40 -08:00
1 parent b0038bb24b
commit 85c7c22619
51 files changed
+110 -98

No files matched your search

+16 -16
View File
@@ -11,11 +11,11 @@ pub fn server_only(input: TokenStream) -> TokenStream {
if cfg!(any(feature = "ssr", feature = "liveview")) {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -26,11 +26,11 @@ pub fn client(input: TokenStream) -> TokenStream {
if cfg!(any(feature = "desktop", feature = "web", feature = "tui")) {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -41,11 +41,11 @@ pub fn web(input: TokenStream) -> TokenStream {
if cfg!(feature = "web") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -56,11 +56,11 @@ pub fn desktop(input: TokenStream) -> TokenStream {
if cfg!(feature = "desktop") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -71,11 +71,11 @@ pub fn fullstack(input: TokenStream) -> TokenStream {
if cfg!(feature = "web") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -86,11 +86,11 @@ pub fn ssr(input: TokenStream) -> TokenStream {
if cfg!(feature = "ssr") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -101,11 +101,11 @@ pub fn liveview(input: TokenStream) -> TokenStream {
if cfg!(feature = "liveview") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
@@ -116,11 +116,11 @@ pub fn tui(input: TokenStream) -> TokenStream {
if cfg!(feature = "tui") {
let input = TokenStream2::from(input);
quote! {
Some(#input)
#input
}
} else {
quote! {
None
()
}
}
.into()
+1 -1
View File
@@ -24,5 +24,5 @@ fn app() -> Element {
}
fn main() {
dioxus_desktop::launch::launch(app, Default::default(), Default::default());
dioxus::desktop::launch::launch(app, Default::default(), Default::default());
}
+11 -6
View File
@@ -8,10 +8,11 @@ use dioxus_core::AnyProps;
use dioxus_core::VProps;
/// A builder for a fullstack app.
pub struct LaunchBuilder<Cfg = (), ContextFn: ?Sized = ValidContext> {
pub struct LaunchBuilder<Cfg: 'static = (), ContextFn: ?Sized = ValidContext> {
launch_fn: LaunchFn<Cfg, ContextFn>,
contexts: Vec<Box<ContextFn>>,
platform_config: Option<Cfg>,
platform_config: Option<Box<dyn Any>>,
}
pub type LaunchFn<Cfg, Context> = fn(fn() -> Element, Vec<Box<Context>>, Cfg);
@@ -122,18 +123,22 @@ impl<Cfg> LaunchBuilder<Cfg, SendContext> {
}
}
impl<Cfg: Default, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
impl<Cfg: Default + 'static, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
/// Provide a platform-specific config to the builder.
pub fn with_cfg(mut self, config: impl Into<Option<Cfg>>) -> Self {
pub fn with_cfg<CG: 'static>(mut self, config: impl Into<Option<CG>>) -> Self {
if let Some(config) = config.into() {
self.platform_config = Some(config);
self.platform_config = Some(Box::new(config));
}
self
}
/// Launch your application.
pub fn launch(self, app: fn() -> Element) {
(self.launch_fn)(app, self.contexts, self.platform_config.unwrap_or_default())
let cfg: Box<Cfg> = self
.platform_config
.and_then(|c| c.downcast().ok())
.unwrap_or_default();
(self.launch_fn)(app, self.contexts, *cfg)
}
}
+3
View File
@@ -60,6 +60,9 @@ pub mod prelude {
#[cfg(feature = "fullstack")]
pub use dioxus_fullstack::prelude::*;
#[cfg(feature = "router")]
pub use dioxus_router;
}
#[cfg(feature = "web")]
+1 -1
View File
@@ -73,6 +73,6 @@ desktop = ["dioxus-desktop"]
warp = ["dep:warp", "ssr"]
axum = ["dep:axum", "tower-http", "ssr"]
salvo = ["dep:salvo", "ssr", "http-body-util"]
ssr = ["server_fn/ssr", "dioxus_server_macro/ssr", "tokio", "tokio-util", "tokio-stream", "dioxus-ssr", "tower", "hyper", "http", "tower-layer", "anymap", "tracing-futures", "pin-project", "thiserror"]
ssr = ["server_fn/ssr", "dioxus_server_macro/ssr", "tokio", "tokio-util", "tokio-stream", "dioxus-ssr", "dioxus-ssr/incremental", "tower", "hyper", "http", "tower-layer", "anymap", "tracing-futures", "pin-project", "thiserror"]
default-tls = ["server_fn/default-tls"]
rustls = ["server_fn/rustls"]
@@ -9,5 +9,5 @@ use dioxus_fullstack::prelude::server_fn::set_server_url;
fn main() {
// Set the url of the server where server functions are hosted.
set_server_url("http://127.0.0.1:8080");
dioxus_desktop::launch(app)
dioxus::desktop::launch(app)
}
@@ -5,8 +5,8 @@
//! ```
use dioxus::prelude::*;
use dioxus::router::prelude::*;
use dioxus_fullstack::prelude::*;
use dioxus_router::prelude::*;
fn main() {
let config = LaunchBuilder::fullstack();
@@ -7,8 +7,8 @@
#![allow(unused)]
use dioxus::prelude::*;
use dioxus::router::prelude::*;
use dioxus_fullstack::{launch, prelude::*};
use dioxus_router::prelude::*;
use serde::{Deserialize, Serialize};
// Generate all routes and output them to the docs path
+4 -4
View File
@@ -587,8 +587,8 @@ impl RouteEnum {
}
quote! {
impl ::dioxus_router::routable::Routable for #name where Self: Clone {
const SITE_MAP: &'static [::dioxus_router::routable::SiteMapSegment] = &[
impl dioxus_router::routable::Routable for #name where Self: Clone {
const SITE_MAP: &'static [dioxus_router::routable::SiteMapSegment] = &[
#(#site_map,)*
];
@@ -605,14 +605,14 @@ impl RouteEnum {
fn component_impl(&self) -> TokenStream2 {
let name = &self.name;
let props = quote! { ::std::rc::Rc<::std::cell::Cell<::dioxus_router::prelude::RouterConfig<#name>>> };
let props = quote! { ::std::rc::Rc<::std::cell::Cell<dioxus_router::prelude::RouterConfig<#name>>> };
quote! {
impl dioxus_core::ComponentFunction<#props> for #name {
fn rebuild(&self, props: #props) -> dioxus_core::Element {
let initial_route = self.clone();
rsx! {
::dioxus_router::prelude::Router::<#name> {
dioxus_router::prelude::Router::<#name> {
config: move || props.take().initial_route(initial_route)
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
use dioxus::prelude::*;
use dioxus_router::prelude::*;
use dioxus::router::prelude::*;
use std::str::FromStr;
#[cfg(feature = "liveview")]
@@ -1,7 +1,7 @@
use std::time::Duration;
use dioxus::prelude::*;
use dioxus_router::prelude::*;
use dioxus::router::prelude::*;
use dioxus_ssr::incremental::{DefaultRenderer, IncrementalRendererConfig};
@@ -29,7 +29,7 @@ async fn main() {
);
// This function is available if you enable the ssr feature
// on the dioxus_router crate.
// on the dioxus::router crate.
pre_cache_static_routes::<Route, _>(
&mut renderer,
&DefaultRenderer {
+1 -1
View File
@@ -51,7 +51,7 @@ macro_rules! default_history {
{
// If we are on wasm32 and the web feature is enabled, use the web history.
#[cfg(all(target_arch = "wasm32", feature = "web"))]
return Box::<AnyHistoryProviderImplWrapper::<WebHistory::<R>>>::default();
return Box::new(AnyHistoryProviderImplWrapper::new(WebHistory::<R>::default()));
// If we are using dioxus fullstack and the ssr feature is enabled, use the memory history with the initial path set to the current path in fullstack
#[cfg(all(feature = "fullstack", feature = "ssr"))]
return dioxus_router::prelude::MemoryHistory::with_initial_path(
+1 -1
View File
@@ -4,7 +4,7 @@ use dioxus::prelude::*;
use dioxus_signals::Signal;
fn main() {
// dioxus_desktop::launch(app);
// dioxus::desktop::launch(app);
}
// Because signal is never read in this component, this component will not rerun when the signal changes
+1 -1
View File
@@ -1,7 +1,7 @@
use dioxus::prelude::*;
fn main() {
// dioxus_desktop::launch(app);
// dioxus::desktop::launch(app);
}
fn app() -> Element {
+1 -1
View File
@@ -1,7 +1,7 @@
use dioxus::prelude::*;
fn main() {
// dioxus_desktop::launch(app);
// dioxus::desktop::launch(app);
}
#[derive(Clone, Copy)]
+1 -1
View File
@@ -2,7 +2,7 @@ use dioxus::prelude::*;
use dioxus_signals::*;
fn main() {
// dioxus_desktop::launch(App);
// dioxus::desktop::launch(App);
}
#[component]
+1 -1
View File
@@ -2,7 +2,7 @@ use dioxus::prelude::*;
use dioxus_signals::*;
fn main() {
// dioxus_desktop::launch(App);
// dioxus::desktop::launch(App);
}
#[component]
@@ -4,7 +4,7 @@ use dioxus::prelude::*;
use dioxus_signals::Signal;
fn main() {
// dioxus_desktop::launch(app);
// dioxus::desktop::launch(app);
}
#[derive(Clone, Copy, Default)]
+1 -1
View File
@@ -33,5 +33,5 @@ fs_extra = "1.2.0"
dioxus-signals = { workspace = true }
[features]
default = ["incremental"]
default = []
incremental = ["dep:tokio"]