mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
Stream resources using bincode => base64 instead of serde_json for bundle size and overall speed
This commit is contained in:
parent
b4cef51f08
commit
6365ac2c8c
4 changed files with 48 additions and 14 deletions
|
@ -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"]
|
||||
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"]
|
|
@ -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 = []
|
|
@ -17,6 +17,7 @@ use crate::{
|
|||
WriteSignal,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
pub fn create_resource<S, T, Fu>(
|
||||
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<S, T, Fu>(
|
||||
cx: Scope,
|
||||
source: impl Fn() -> S + 'static,
|
||||
fetcher: impl Fn(S) -> Fu + 'static,
|
||||
) -> Resource<S, T>
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + Serialize + DeserializeOwned + 'static,
|
||||
Fu: Future<Output = T> + '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<S, T, Fu>(
|
||||
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<S, T>(cx: Scope, _id: ResourceId, r: Rc<ResourceState<S, T>>)
|
||||
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()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"]
|
Loading…
Reference in a new issue