diff --git a/leptos/Cargo.toml b/leptos/Cargo.toml index 104206362..a074ca101 100644 --- a/leptos/Cargo.toml +++ b/leptos/Cargo.toml @@ -11,6 +11,6 @@ leptos_reactive = { path = "../leptos_reactive" } leptos_router = { path = "../router" } [features] -csr = ["leptos_core/csr", "leptos_router/csr", "leptos_macro/csr", "leptos_reactive/csr", "leptos_router/csr"] -hydrate = ["leptos_core/hydrate", "leptos_router/hydrate", "leptos_macro/hydrate", "leptos_reactive/hydrate", "leptos_router/hydrate"] -ssr = ["leptos_core/ssr", "leptos_router/ssr", "leptos_macro/ssr", "leptos_reactive/ssr", "leptos_router/ssr"] \ No newline at end of file +csr = ["leptos_core/csr", "leptos_router/csr", "leptos_macro/csr", "leptos_reactive/csr"] +hydrate = ["leptos_core/hydrate", "leptos_router/hydrate", "leptos_macro/hydrate", "leptos_reactive/hydrate"] +ssr = ["leptos_core/ssr", "leptos_router/ssr", "leptos_macro/ssr", "leptos_reactive/ssr"] \ No newline at end of file diff --git a/leptos_reactive/Cargo.toml b/leptos_reactive/Cargo.toml index b88eae45d..4f13e9d2d 100644 --- a/leptos_reactive/Cargo.toml +++ b/leptos_reactive/Cargo.toml @@ -12,6 +12,8 @@ futures = { version = "0.3", optional = true } js-sys = { version = "0.3", optional = true } serde-wasm-bindgen = { version = "0.4", optional = true } serde_json = { version = "1", optional = true } +base64 = { version = "0.13", optional = true } +bincode = "1" thiserror = "1" tokio = { version = "1", features = ["rt"], optional = true } wasm-bindgen = { version = "0.2", optional = true } @@ -23,6 +25,6 @@ sycamore = "0.8.0-beta.7" [features] csr = ["dep:wasm-bindgen", "dep:wasm-bindgen-futures", "dep:web-sys"] -hydrate = ["dep:js-sys", "dep:serde-wasm-bindgen", "dep:serde_json", "dep:wasm-bindgen", "dep:wasm-bindgen-futures", "dep:web-sys"] -ssr = ["dep:futures", "dep:serde_json", "dep:tokio"] +hydrate = ["dep:base64", "dep:js-sys", "dep:serde-wasm-bindgen", "dep:serde_json", "dep:wasm-bindgen", "dep:wasm-bindgen-futures", "dep:web-sys"] +ssr = ["dep:base64", "dep:futures", "dep:serde_json", "dep:tokio"] transition = [] \ No newline at end of file diff --git a/leptos_reactive/src/resource.rs b/leptos_reactive/src/resource.rs index 7ed40ecbc..5fec3320d 100644 --- a/leptos_reactive/src/resource.rs +++ b/leptos_reactive/src/resource.rs @@ -17,6 +17,7 @@ use crate::{ WriteSignal, }; +#[cfg(not(feature = "ssr"))] pub fn create_resource( cx: Scope, source: impl Fn() -> S + 'static, @@ -30,6 +31,24 @@ where create_resource_with_initial_value(cx, source, fetcher, None) } +#[cfg(feature = "ssr")] +pub fn create_resource( + cx: Scope, + source: impl Fn() -> S + 'static, + fetcher: impl Fn(S) -> Fu + 'static, +) -> Resource +where + S: PartialEq + Debug + Clone + 'static, + T: Debug + Clone + Serialize + DeserializeOwned + 'static, + Fu: Future + 'static, +{ + use futures::FutureExt; + + let initial_fut = fetcher(source()); + let initial_value = initial_fut.now_or_never(); + create_resource_with_initial_value(cx, source, fetcher, initial_value) +} + pub fn create_resource_with_initial_value( cx: Scope, source: impl Fn() -> S + 'static, @@ -84,7 +103,7 @@ where } } -#[cfg(any(feature = "csr", feature = "ssr"))] +#[cfg(not(feature = "hydrate"))] fn load_resource(cx: Scope, _id: ResourceId, r: Rc>) where S: PartialEq + Debug + Clone + 'static, @@ -108,10 +127,12 @@ where context.resolved_resources ); - if let Some(json) = context.resolved_resources.remove(&resource_id) { + if let Some(data) = context.resolved_resources.remove(&resource_id) { log::debug!("(create_resource) resource already resolved from server"); r.resolved.set(true); - let res = serde_json::from_str(&json).unwrap_throw(); + let decoded = base64::decode(&data).unwrap_throw(); + let res = bincode::deserialize(&decoded).unwrap_throw(); + log::debug!("deserialized = {res:?}"); r.set_value.update(|n| *n = Some(res)); r.set_loading.update(|n| *n = false); } else if context.pending_resources.remove(&resource_id) { @@ -124,7 +145,8 @@ where let set_value = r.set_value; let set_loading = r.set_loading; move |res: String| { - let res = serde_json::from_str(&res).ok(); + let decoded = base64::decode(&res).unwrap_throw(); + let res = bincode::deserialize(&decoded).unwrap_throw(); resolved.set(true); set_value.update(|n| *n = res); set_loading.update(|n| *n = false); @@ -376,7 +398,8 @@ where let fut = (self.fetcher)(self.source.get()); Box::pin(async move { let res = fut.await; - (id, serde_json::to_string(&res).unwrap()) + //(id, serde_json::to_string(&res).unwrap()) + (id, base64::encode(&bincode::serialize(&res).unwrap())) }) } } diff --git a/router/Cargo.toml b/router/Cargo.toml index 75f0077e4..226f5b2c8 100644 --- a/router/Cargo.toml +++ b/router/Cargo.toml @@ -15,12 +15,14 @@ linear-map = "1" log = "0.4" regex = { version = "1", optional = true } serde = { version = "1", features = ["derive"] } +bincode = "1" url = { version = "2", optional = true } urlencoding = "2" thiserror = "1" typed-builder = "0.10" js-sys = { version = "0.3", optional = true } -wasm-bindgen = "0.2" +wasm-bindgen = { version = "0.2", optional = true } +wasm-bindgen-futures = { version = "0.4", optional = true } [dependencies.web-sys] version = "0.3" @@ -37,11 +39,18 @@ features = [ "HtmlInputElement", "SubmitEvent", "Url", - "UrlSearchParams" + "UrlSearchParams", + # Fetching in Hydrate Mode + "Headers", + "Request", + "RequestInit", + "RequestMode", + "Response", + "Window", ] [features] -csr = ["leptos_core/csr", "leptos_dom/csr", "leptos_macro/csr", "leptos_reactive/csr", "dep:js-sys"] -hydrate = ["leptos_core/hydrate", "leptos_dom/hydrate", "leptos_macro/hydrate", "leptos_reactive/hydrate", "dep:js-sys"] +csr = ["leptos_core/csr", "leptos_dom/csr", "leptos_macro/csr", "leptos_reactive/csr", "dep:js-sys", "dep:wasm-bindgen"] +hydrate = ["leptos_core/hydrate", "leptos_dom/hydrate", "leptos_macro/hydrate", "leptos_reactive/hydrate", "dep:js-sys", "dep:wasm-bindgen", "dep:wasm-bindgen-futures"] ssr = ["leptos_core/ssr", "leptos_dom/ssr", "leptos_macro/ssr", "leptos_reactive/ssr", "dep:url", "dep:regex"] transition = ["leptos_core/transition", "leptos_reactive/transition"] \ No newline at end of file