Skip to content

Commit 2480678

Browse files
gbjmaccesch
authored andcommitted
fix: improve marker-node filtering when using islands router (closes leptos-rs#4443) (leptos-rs#4446)
1 parent eeda4d1 commit 2480678

File tree

4 files changed

+38
-76
lines changed

4 files changed

+38
-76
lines changed

tachys/src/hydration.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use std::cell::Cell;
77
use std::{cell::RefCell, panic::Location, rc::Rc};
88
use web_sys::{Comment, Element, Node, Text};
99

10+
#[cfg(feature = "mark_branches")]
11+
const COMMENT_NODE: u16 = 8;
12+
1013
/// Hydration works by walking over the DOM, adding interactivity as needed.
1114
///
1215
/// This cursor tracks the location in the DOM that is currently being hydrated. Each that type
@@ -43,13 +46,27 @@ where
4346
///
4447
/// Does nothing if there is no child.
4548
pub fn child(&self) {
46-
//crate::log("advancing to next child of ");
47-
//Rndr::log_node(&self.current());
4849
let mut inner = self.0.borrow_mut();
4950
if let Some(node) = Rndr::first_child(&inner) {
5051
*inner = node;
5152
}
52-
//drop(inner);
53+
54+
#[cfg(feature = "mark_branches")]
55+
{
56+
while inner.node_type() == COMMENT_NODE {
57+
if let Some(content) = inner.text_content() {
58+
if content.starts_with("bo") || content.starts_with("bc") {
59+
if let Some(sibling) = Rndr::next_sibling(&inner) {
60+
*inner = sibling;
61+
continue;
62+
}
63+
}
64+
}
65+
66+
break;
67+
}
68+
}
69+
// //drop(inner);
5370
//crate::log(">> which is ");
5471
//Rndr::log_node(&self.current());
5572
}
@@ -58,12 +75,25 @@ where
5875
///
5976
/// Does nothing if there is no sibling.
6077
pub fn sibling(&self) {
61-
//crate::log("advancing to next sibling of ");
62-
//Rndr::log_node(&self.current());
6378
let mut inner = self.0.borrow_mut();
6479
if let Some(node) = Rndr::next_sibling(&inner) {
6580
*inner = node;
6681
}
82+
83+
#[cfg(feature = "mark_branches")]
84+
{
85+
while inner.node_type() == COMMENT_NODE {
86+
if let Some(content) = inner.text_content() {
87+
if content.starts_with("bo") || content.starts_with("bc") {
88+
if let Some(sibling) = Rndr::next_sibling(&inner) {
89+
*inner = sibling;
90+
continue;
91+
}
92+
}
93+
}
94+
break;
95+
}
96+
}
6797
//drop(inner);
6898
//crate::log(">> which is ");
6999
//Rndr::log_node(&self.current());

tachys/src/view/any_view.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,7 @@ impl RenderHtml for AnyView {
575575
#[cfg(feature = "hydrate")]
576576
{
577577
if FROM_SERVER {
578-
if cfg!(feature = "mark_branches") {
579-
cursor.advance_to_placeholder(position);
580-
}
581-
let state =
582-
(self.hydrate_from_server)(self.value, cursor, position);
583-
if cfg!(feature = "mark_branches") {
584-
cursor.advance_to_placeholder(position);
585-
}
586-
state
578+
(self.hydrate_from_server)(self.value, cursor, position)
587579
} else {
588580
panic!(
589581
"hydrating AnyView from inside a ViewTemplate is not \
@@ -609,14 +601,8 @@ impl RenderHtml for AnyView {
609601
) -> Self::State {
610602
#[cfg(feature = "hydrate")]
611603
{
612-
if cfg!(feature = "mark_branches") {
613-
cursor.advance_to_placeholder(position);
614-
}
615604
let state =
616605
(self.hydrate_async)(self.value, cursor, position).await;
617-
if cfg!(feature = "mark_branches") {
618-
cursor.advance_to_placeholder(position);
619-
}
620606
state
621607
}
622608
#[cfg(not(feature = "hydrate"))]

tachys/src/view/either.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -411,43 +411,29 @@ where
411411
cursor: &Cursor,
412412
position: &PositionState,
413413
) -> Self::State {
414-
if cfg!(feature = "mark_branches") {
415-
cursor.advance_to_placeholder(position);
416-
}
417-
let state = match self {
414+
match self {
418415
Either::Left(left) => {
419416
Either::Left(left.hydrate::<FROM_SERVER>(cursor, position))
420417
}
421418
Either::Right(right) => {
422419
Either::Right(right.hydrate::<FROM_SERVER>(cursor, position))
423420
}
424-
};
425-
if cfg!(feature = "mark_branches") {
426-
cursor.advance_to_placeholder(position);
427421
}
428-
state
429422
}
430423

431424
async fn hydrate_async(
432425
self,
433426
cursor: &Cursor,
434427
position: &PositionState,
435428
) -> Self::State {
436-
if cfg!(feature = "mark_branches") {
437-
cursor.advance_to_placeholder(position);
438-
}
439-
let state = match self {
429+
match self {
440430
Either::Left(left) => {
441431
Either::Left(left.hydrate_async(cursor, position).await)
442432
}
443433
Either::Right(right) => {
444434
Either::Right(right.hydrate_async(cursor, position).await)
445435
}
446-
};
447-
if cfg!(feature = "mark_branches") {
448-
cursor.advance_to_placeholder(position);
449436
}
450-
state
451437
}
452438

453439
fn into_owned(self) -> Self::Owned {
@@ -973,17 +959,11 @@ macro_rules! tuples {
973959
cursor: &Cursor,
974960
position: &PositionState,
975961
) -> Self::State {
976-
if cfg!(feature = "mark_branches") {
977-
cursor.advance_to_placeholder(position);
978-
}
979962
let state = match self {
980963
$([<EitherOf $num>]::$ty(this) => {
981964
[<EitherOf $num>]::$ty(this.hydrate::<FROM_SERVER>(cursor, position))
982965
})*
983966
};
984-
if cfg!(feature = "mark_branches") {
985-
cursor.advance_to_placeholder(position);
986-
}
987967

988968
Self::State { state }
989969
}
@@ -993,17 +973,11 @@ macro_rules! tuples {
993973
cursor: &Cursor,
994974
position: &PositionState,
995975
) -> Self::State {
996-
if cfg!(feature = "mark_branches") {
997-
cursor.advance_to_placeholder(position);
998-
}
999976
let state = match self {
1000977
$([<EitherOf $num>]::$ty(this) => {
1001978
[<EitherOf $num>]::$ty(this.hydrate_async(cursor, position).await)
1002979
})*
1003980
};
1004-
if cfg!(feature = "mark_branches") {
1005-
cursor.advance_to_placeholder(position);
1006-
}
1007981

1008982
Self::State { state }
1009983
}

tachys/src/view/keyed.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,6 @@ where
322322
cursor: &Cursor,
323323
position: &PositionState,
324324
) -> Self::State {
325-
if cfg!(feature = "mark_branches") {
326-
cursor.advance_to_placeholder(position);
327-
}
328-
329325
// get parent and position
330326
let current = cursor.current();
331327
let parent = if position.get() == Position::FirstChild {
@@ -346,22 +342,12 @@ where
346342
for (index, item) in items.enumerate() {
347343
hashed_items.insert((self.key_fn)(&item));
348344
let (set_index, view) = (self.view_fn)(index, item);
349-
if cfg!(feature = "mark_branches") {
350-
cursor.advance_to_placeholder(position);
351-
}
352345
let item = view.hydrate::<FROM_SERVER>(cursor, position);
353-
if cfg!(feature = "mark_branches") {
354-
cursor.advance_to_placeholder(position);
355-
}
356346
rendered_items.push(Some((set_index, item)));
357347
}
358348
let marker = cursor.next_placeholder(position);
359349
position.set(Position::NextChild);
360350

361-
if cfg!(feature = "mark_branches") {
362-
cursor.advance_to_placeholder(position);
363-
}
364-
365351
KeyedState {
366352
parent: Some(parent),
367353
marker,
@@ -375,10 +361,6 @@ where
375361
cursor: &Cursor,
376362
position: &PositionState,
377363
) -> Self::State {
378-
if cfg!(feature = "mark_branches") {
379-
cursor.advance_to_placeholder(position);
380-
}
381-
382364
// get parent and position
383365
let current = cursor.current();
384366
let parent = if position.get() == Position::FirstChild {
@@ -399,22 +381,12 @@ where
399381
for (index, item) in items.enumerate() {
400382
hashed_items.insert((self.key_fn)(&item));
401383
let (set_index, view) = (self.view_fn)(index, item);
402-
if cfg!(feature = "mark_branches") {
403-
cursor.advance_to_placeholder(position);
404-
}
405384
let item = view.hydrate_async(cursor, position).await;
406-
if cfg!(feature = "mark_branches") {
407-
cursor.advance_to_placeholder(position);
408-
}
409385
rendered_items.push(Some((set_index, item)));
410386
}
411387
let marker = cursor.next_placeholder(position);
412388
position.set(Position::NextChild);
413389

414-
if cfg!(feature = "mark_branches") {
415-
cursor.advance_to_placeholder(position);
416-
}
417-
418390
KeyedState {
419391
parent: Some(parent),
420392
marker,

0 commit comments

Comments
 (0)