Fix hydration of link element (#4561)
* Fix hydration of link element * revert some logging changes * clean up fullstack history provider * fix router tests * fix document context type
This commit is contained in:
1 parent
6cd4453662
commit
021565c171
12 files changed
+141
-109
No files matched your search
@@ -1,3 +1,4 @@
|
||||
use dioxus_core::provide_context;
|
||||
use dioxus_core::queue_effect;
|
||||
use dioxus_core::ScopeId;
|
||||
use dioxus_document::{
|
||||
@@ -66,18 +67,51 @@ extern "C" {
|
||||
|
||||
}
|
||||
|
||||
fn init_document_with(document: impl FnOnce(), history: impl FnOnce()) {
|
||||
use dioxus_core::has_context;
|
||||
|
||||
ScopeId::ROOT.in_runtime(|| {
|
||||
if has_context::<Rc<dyn Document>>().is_none() {
|
||||
document();
|
||||
}
|
||||
if has_context::<Rc<dyn History>>().is_none() {
|
||||
history();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Provides the Document through [`ScopeId::provide_context`].
|
||||
pub fn init_document() {
|
||||
let provider: Rc<dyn Document> = Rc::new(WebDocument);
|
||||
if ScopeId::ROOT.has_context::<Rc<dyn Document>>().is_none() {
|
||||
ScopeId::ROOT.provide_context(provider);
|
||||
}
|
||||
let history_provider: Rc<dyn History> = Rc::new(WebHistory::default());
|
||||
if ScopeId::ROOT.has_context::<Rc<dyn History>>().is_none() {
|
||||
ScopeId::ROOT.provide_context(history_provider);
|
||||
// If hydrate is enabled, we add the FullstackWebDocument with the initial hydration data
|
||||
#[cfg(not(feature = "hydrate"))]
|
||||
{
|
||||
use dioxus_history::provide_history_context;
|
||||
|
||||
init_document_with(
|
||||
|| {
|
||||
provide_context(Rc::new(WebDocument) as Rc<dyn Document>);
|
||||
},
|
||||
|| {
|
||||
provide_history_context(Rc::new(WebHistory::default()));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "hydrate")]
|
||||
pub fn init_fullstack_document() {
|
||||
use dioxus_fullstack_hooks::{
|
||||
document::FullstackWebDocument, history::provide_fullstack_history_context,
|
||||
};
|
||||
|
||||
init_document_with(
|
||||
|| {
|
||||
provide_context(Rc::new(FullstackWebDocument::from(WebDocument)) as Rc<dyn Document>);
|
||||
},
|
||||
|| provide_fullstack_history_context(WebHistory::default()),
|
||||
);
|
||||
}
|
||||
|
||||
/// The web-target's document provider.
|
||||
#[derive(Clone)]
|
||||
pub struct WebDocument;
|
||||
|
||||
@@ -64,7 +64,8 @@ pub async fn run(mut virtual_dom: VirtualDom, web_config: Config) -> ! {
|
||||
#[cfg(all(feature = "devtools", debug_assertions))]
|
||||
let mut hotreload_rx = devtools::init();
|
||||
|
||||
let should_hydrate = web_config.hydrate;
|
||||
// If the hydrate feature is enabled, launch the client with hydration enabled
|
||||
let should_hydrate = web_config.hydrate || cfg!(feature = "hydrate");
|
||||
|
||||
let mut websys_dom = WebsysDom::new(web_config, runtime);
|
||||
|
||||
@@ -134,6 +135,10 @@ pub async fn run(mut virtual_dom: VirtualDom, web_config: Config) -> ! {
|
||||
virtual_dom.in_runtime(|| dioxus_core::ScopeId::APP.throw_error(error));
|
||||
}
|
||||
server_data.in_context(|| {
|
||||
#[cfg(feature = "document")]
|
||||
virtual_dom.in_runtime(|| {
|
||||
document::init_fullstack_document();
|
||||
});
|
||||
virtual_dom.rebuild(&mut websys_dom);
|
||||
});
|
||||
websys_dom.skip_mutations = false;
|
||||
|
||||
Reference in new issue
Block a user