Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/lazy_routes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]
axum = { version = "0.8.1", optional = true }
console_error_panic_hook = "0.1.7"
console_log = "1.0"
leptos = { path = "../../leptos", features = ["tracing"] }
leptos = { path = "../../leptos", features = ["tracing", "lazy"] }
leptos_meta = { path = "../../meta" }
leptos_axum = { path = "../../integrations/axum", optional = true }
leptos_router = { path = "../../router" }
Expand Down
1 change: 1 addition & 0 deletions leptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ subsecond = [
"web-sys/WebSocket",
"web-sys/Window",
]
lazy = ["tachys/lazy"]

[dev-dependencies]
tokio = { features = [
Expand Down
1 change: 1 addition & 0 deletions tachys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ default = ["testing"]
delegation = [] # enables event delegation
error-hook = []
hydrate = []
lazy = []
islands = ["dep:serde", "dep:serde_json"]
ssr = []
oco = ["dep:oco_ref"]
Expand Down
36 changes: 27 additions & 9 deletions tachys/src/view/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
};
use futures::future::{join, join_all};
use std::{any::TypeId, fmt::Debug};
#[cfg(any(feature = "ssr", feature = "hydrate"))]
#[cfg(any(feature = "ssr", all(feature = "hydrate", feature = "lazy")))]
use std::{future::Future, pin::Pin};

/// A type-erased view. This can be used if control flow requires that multiple different types of
Expand Down Expand Up @@ -67,10 +67,10 @@ pub struct AnyView {
resolve: fn(Erased) -> Pin<Box<dyn Future<Output = AnyView> + Send>>,
#[cfg(feature = "ssr")]
dry_resolve: fn(&mut Erased),
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", not(feature = "lazy")))]
#[allow(clippy::type_complexity)]
hydrate_from_server: fn(Erased, &Cursor, &PositionState) -> AnyViewState,
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", feature = "lazy"))]
#[allow(clippy::type_complexity)]
hydrate_async: fn(
Erased,
Expand Down Expand Up @@ -291,7 +291,7 @@ where
}
}

#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", not(feature = "lazy")))]
fn hydrate_from_server<T: RenderHtml + 'static>(
value: Erased,
cursor: &Cursor,
Expand All @@ -313,7 +313,7 @@ where
}
}

#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", feature = "lazy"))]
fn hydrate_async<T: RenderHtml + 'static>(
value: Erased,
cursor: &Cursor,
Expand Down Expand Up @@ -367,9 +367,9 @@ where
to_html_async: to_html_async::<T::Owned>,
#[cfg(feature = "ssr")]
to_html_async_ooo: to_html_async_ooo::<T::Owned>,
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", not(feature = "lazy")))]
hydrate_from_server: hydrate_from_server::<T::Owned>,
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", feature = "lazy"))]
hydrate_async: hydrate_async::<T::Owned>,
value: Erased::new(value),
}
Expand Down Expand Up @@ -572,7 +572,7 @@ impl RenderHtml for AnyView {
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", not(feature = "lazy")))]
{
if FROM_SERVER {
if cfg!(feature = "mark_branches") {
Expand All @@ -591,6 +591,14 @@ impl RenderHtml for AnyView {
);
}
}
#[cfg(all(feature = "hydrate", feature = "lazy"))]
{
use futures::FutureExt;

(self.hydrate_async)(self.value, cursor, position)
.now_or_never()
.unwrap()
}
#[cfg(not(feature = "hydrate"))]
{
_ = cursor;
Expand All @@ -607,18 +615,28 @@ impl RenderHtml for AnyView {
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
#[cfg(feature = "hydrate")]
#[cfg(all(feature = "hydrate", feature = "lazy"))]
{
if cfg!(feature = "mark_branches") {
cursor.advance_to_placeholder(position);
}
#[cfg(all(feature = "hydrate", feature = "lazy"))]
let state =
(self.hydrate_async)(self.value, cursor, position).await;
if cfg!(feature = "mark_branches") {
cursor.advance_to_placeholder(position);
}
state
}
#[cfg(all(feature = "hydrate", not(feature = "lazy")))]
{
_ = cursor;
_ = position;
panic!(
"the `lazy` feature on `tachys` must be activated to use lazy \
hydration"
);
}
#[cfg(not(feature = "hydrate"))]
{
_ = cursor;
Expand Down
Loading