fix can go back hydration (#4209)

This commit is contained in:
Evan Almloff authored and GitHub committed 2025-06-18 16:48:59 -07:00
1 parent a0ab159c01
commit 315854b449
10 files changed
+190 -8

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
//! A history provider for fullstack apps that is compatible with hydration.
use std::cell::OnceCell;
use dioxus_core::{
prelude::{generation, queue_effect},
schedule_update,
};
use dioxus_history::History;
// If we are currently in a scope and this is the first run then queue a rerender
// for after hydration
fn match_hydration<O>(
during_hydration: impl FnOnce() -> O,
after_hydration: impl FnOnce() -> O,
) -> O {
if generation() == 0 {
let update = schedule_update();
queue_effect(move || update());
during_hydration()
} else {
after_hydration()
}
}
/// A history provider for fullstack apps that is compatible with hydration.
#[derive(Clone)]
pub struct FullstackHistory<H> {
initial_route: OnceCell<String>,
#[cfg(feature = "server")]
in_hydration_context: std::cell::Cell<bool>,
history: H,
}
impl<H> FullstackHistory<H> {
/// Create a new `FullstackHistory` with the given history.
pub fn new(history: H) -> Self {
Self {
initial_route: OnceCell::new(),
#[cfg(feature = "server")]
in_hydration_context: std::cell::Cell::new(false),
history,
}
}
/// Create a new `FullstackHistory` with the given history and initial route.
pub fn new_server(history: H) -> Self
where
H: History,
{
let initial_route = history.current_route();
let history = Self::new(history);
history.initial_route.set(initial_route).unwrap();
history
}
/// Get the initial route of the history.
fn initial_route(&self) -> String {
let entry = dioxus_fullstack_protocol::serialize_context().create_entry();
let route = self.initial_route.get_or_init(|| {
entry
.get()
.expect("Failed to get initial route from hydration context")
});
#[cfg(feature = "server")]
if !self.in_hydration_context.get() {
entry.insert(route, std::panic::Location::caller());
self.in_hydration_context.set(true);
}
route.clone()
}
}
impl<H: History> History for FullstackHistory<H> {
fn current_prefix(&self) -> Option<String> {
self.history.current_prefix()
}
fn can_go_back(&self) -> bool {
match_hydration(|| false, || self.history.can_go_back())
}
fn can_go_forward(&self) -> bool {
match_hydration(|| false, || self.history.can_go_forward())
}
fn external(&self, url: String) -> bool {
self.history.external(url)
}
fn updater(&self, callback: std::sync::Arc<dyn Fn() + Send + Sync>) {
self.history.updater(callback)
}
fn include_prevent_default(&self) -> bool {
self.history.include_prevent_default()
}
fn current_route(&self) -> String {
match_hydration(|| self.initial_route(), || self.history.current_route())
}
fn go_back(&self) {
self.history.go_back();
}
fn go_forward(&self) {
self.history.go_forward();
}
fn push(&self, route: String) {
self.history.push(route);
}
fn replace(&self, path: String) {
self.history.replace(path);
}
}
+8 -2
View File
@@ -1,7 +1,13 @@
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
pub mod history;
mod hooks;
pub use hooks::*;
mod streaming;
pub use streaming::*;
pub mod prelude {
//! A prelude of commonly used items in dioxus-fullstack-hooks.
pub use crate::hooks::*;
pub use crate::streaming::*;
}