| {
let mut service = crate::server_fn_service(Default::default(), func);
async move {
diff --git a/packages/interpreter/src/sledgehammer_bindings.rs b/packages/interpreter/src/sledgehammer_bindings.rs
index fcd72937c..8b44f4486 100644
--- a/packages/interpreter/src/sledgehammer_bindings.rs
+++ b/packages/interpreter/src/sledgehammer_bindings.rs
@@ -123,13 +123,14 @@ mod js {
export function save_template(nodes, tmpl_id) {
templates[tmpl_id] = nodes;
}
- export function hydrate() {
+ export function hydrate(ids) {
+ console.log("hydrating", ids);
const hydrateNodes = document.querySelectorAll('[data-node-hydration]');
for (let i = 0; i < hydrateNodes.length; i++) {
const hydrateNode = hydrateNodes[i];
const hydration = hydrateNode.getAttribute('data-node-hydration');
const split = hydration.split(',');
- const id = parseInt(split[0]);
+ const id = ids[parseInt(split[0])];
nodes[id] = hydrateNode;
console.log("hydrating node", hydrateNode, id);
if (split.length > 1) {
@@ -155,7 +156,7 @@ mod js {
const split = id.split('node-id');
if (split.length > 1) {
console.log("hydrating text", currentNode.nextSibling, id);
- nodes[parseInt(split[1])] = currentNode.nextSibling;
+ nodes[ids[parseInt(split[1])]] = currentNode.nextSibling;
}
currentNode = treeWalker.nextNode();
}
@@ -215,7 +216,7 @@ mod js {
pub fn save_template(nodes: Vec, tmpl_id: u32);
#[wasm_bindgen]
- pub fn hydrate();
+ pub fn hydrate(ids: Vec);
#[wasm_bindgen]
pub fn get_node(id: u32) -> Node;
diff --git a/packages/ssr/src/cache.rs b/packages/ssr/src/cache.rs
index 9adcf395b..11f90058a 100644
--- a/packages/ssr/src/cache.rs
+++ b/packages/ssr/src/cache.rs
@@ -28,9 +28,9 @@ pub enum Segment {
/// A marker for where to insert a dynamic inner html
InnerHtmlMarker,
/// A marker for where to insert a node id for an attribute
- AttributeNodeMarker(usize),
+ AttributeNodeMarker,
/// A marker for where to insert a node id for a root node
- RootNodeMarker(usize),
+ RootNodeMarker,
}
impl std::fmt::Write for StringChain {
@@ -82,7 +82,7 @@ impl StringCache {
// we need to collect the inner html and write it at the end
let mut inner_html = None;
// we need to keep track of if we have dynamic attrs to know if we need to insert a style and inner_html marker
- let mut last_dyn_attr_id = None;
+ let mut has_dyn_attrs = false;
for attr in *attrs {
match attr {
TemplateAttribute::Static {
@@ -105,7 +105,7 @@ impl StringCache {
TemplateAttribute::Dynamic { id: index } => {
let index = *index;
chain.segments.push(Segment::Attr(index));
- last_dyn_attr_id = Some(index);
+ has_dyn_attrs = true
}
}
}
@@ -120,21 +120,19 @@ impl StringCache {
inside_style_tag: true,
});
write!(chain, "\"")?;
- } else if last_dyn_attr_id.is_some() {
+ } else if has_dyn_attrs {
chain.segments.push(Segment::StyleMarker {
inside_style_tag: false,
});
}
// write the id if we are prerendering and this is either a root node or a node with a dynamic attribute
- if prerender {
+ if prerender && (has_dyn_attrs || is_root) {
write!(chain, " data-node-hydration=\"")?;
- if let Some(last_dyn_attr_id) = last_dyn_attr_id {
- chain
- .segments
- .push(Segment::AttributeNodeMarker(last_dyn_attr_id));
+ if has_dyn_attrs {
+ chain.segments.push(Segment::AttributeNodeMarker);
} else if is_root {
- chain.segments.push(Segment::RootNodeMarker(root_idx));
+ chain.segments.push(Segment::RootNodeMarker);
}
write!(chain, "\"")?;
}
@@ -146,7 +144,7 @@ impl StringCache {
// Write the static inner html, or insert a marker if dynamic inner html is possible
if let Some(inner_html) = inner_html {
chain.write_str(inner_html)?;
- } else if last_dyn_attr_id.is_some() {
+ } else if has_dyn_attrs {
chain.segments.push(Segment::InnerHtmlMarker);
}
diff --git a/packages/ssr/src/renderer.rs b/packages/ssr/src/renderer.rs
index 87c882cde..9b0d9e7bb 100644
--- a/packages/ssr/src/renderer.rs
+++ b/packages/ssr/src/renderer.rs
@@ -28,6 +28,9 @@ pub struct Renderer {
/// A cache of templates that have been rendered
template_cache: HashMap<&'static str, Arc>,
+
+ /// The current dynamic node id for hydration
+ dynamic_node_id: usize,
}
impl Renderer {
@@ -54,6 +57,7 @@ impl Renderer {
// We should never ever run into async or errored nodes in SSR
// Error boundaries and suspense boundaries will convert these to sync
if let RenderReturn::Ready(node) = dom.get_scope(scope).unwrap().root_node() {
+ self.dynamic_node_id = 0;
self.render_template(buf, dom, node)?
};
@@ -127,10 +131,8 @@ impl Renderer {
DynamicNode::Text(text) => {
// in SSR, we are concerned that we can't hunt down the right text node since they might get merged
if self.pre_render {
- let node_id = text
- .mounted_element()
- .expect("Text nodes must be mounted before rendering");
- write!(buf, "", node_id.0)?;
+ write!(buf, "", self.dynamic_node_id)?;
+ self.dynamic_node_id += 1;
}
write!(
@@ -149,12 +151,14 @@ impl Renderer {
}
}
- DynamicNode::Placeholder(el) => {
+ DynamicNode::Placeholder(_) => {
if self.pre_render {
- let id = el
- .mounted_element()
- .expect("Elements must be mounted before rendering");
- write!(buf, "", id.0)?;
+ write!(
+ buf,
+ "",
+ self.dynamic_node_id
+ )?;
+ self.dynamic_node_id += 1;
}
}
},
@@ -194,10 +198,10 @@ impl Renderer {
}
}
- Segment::AttributeNodeMarker(idx) => {
- let id = template.dynamic_attrs[*idx].mounted_element();
+ Segment::AttributeNodeMarker => {
// first write the id
- write!(buf, "{}", id.0)?;
+ write!(buf, "{}", self.dynamic_node_id)?;
+ self.dynamic_node_id += 1;
// then write any listeners
for name in accumulated_listeners.drain(..) {
write!(buf, ",{}:", &name[2..])?;
@@ -205,9 +209,9 @@ impl Renderer {
}
}
- Segment::RootNodeMarker(idx) => {
- let id = template.root_ids.borrow()[*idx];
- write!(buf, "{}", id.0)?;
+ Segment::RootNodeMarker => {
+ write!(buf, "{}", self.dynamic_node_id)?;
+ self.dynamic_node_id += 1
}
}
}
diff --git a/packages/ssr/tests/hydration.rs b/packages/ssr/tests/hydration.rs
index 736a1435b..360aa0c5f 100644
--- a/packages/ssr/tests/hydration.rs
+++ b/packages/ssr/tests/hydration.rs
@@ -11,7 +11,7 @@ fn root_ids() {
assert_eq!(
dioxus_ssr::pre_render(&dom),
- r#""#
+ r#""#
);
}
@@ -29,7 +29,7 @@ fn dynamic_attributes() {
assert_eq!(
dioxus_ssr::pre_render(&dom),
- r#"