@@ -74,18 +74,19 @@ export class DynamicFragment extends VaporFragment {
7474
7575 if ( this . fallback ) {
7676 // set fallback for nested fragments
77- const isFrag = isFragment ( this . nodes )
78- if ( isFrag ) {
77+ const hasNestedFragment = isFragment ( this . nodes )
78+ if ( hasNestedFragment ) {
7979 setFragmentFallback ( this . nodes as VaporFragment , this . fallback )
8080 }
8181
82- if ( ! isValidBlock ( this . nodes ) ) {
82+ const invalidFragment = findInvalidFragment ( this )
83+ if ( invalidFragment ) {
8384 parent && remove ( this . nodes , parent )
8485 const scope = this . scope || ( this . scope = new EffectScope ( ) )
8586 scope . run ( ( ) => {
86- if ( isFrag ) {
87- // render fragment's fallback
88- renderFragmentFallback ( this . nodes as VaporFragment )
87+ // for nested fragments, render invalid fragment's fallback
88+ if ( hasNestedFragment ) {
89+ renderFragmentFallback ( invalidFragment )
8990 } else {
9091 this . nodes = this . fallback ! ( ) || [ ]
9192 }
@@ -102,9 +103,10 @@ function setFragmentFallback(
102103 fragment : VaporFragment ,
103104 fallback : BlockFn | undefined ,
104105) : void {
105- if ( ! fragment . fallback ) {
106- fragment . fallback = fallback
107- }
106+ // stop recursion if fragment has its own fallback
107+ if ( fragment . fallback ) return
108+
109+ fragment . fallback = fallback
108110 if ( isFragment ( fragment . nodes ) ) {
109111 setFragmentFallback ( fragment . nodes , fallback )
110112 }
@@ -114,17 +116,20 @@ function renderFragmentFallback(fragment: VaporFragment): void {
114116 if ( fragment instanceof ForFragment ) {
115117 fragment . nodes [ 0 ] = [ fragment . fallback ! ( ) || [ ] ] as Block [ ]
116118 } else if ( fragment instanceof DynamicFragment ) {
117- const nodes = fragment . nodes
118- if ( isFragment ( nodes ) ) {
119- renderFragmentFallback ( nodes )
120- } else {
121- fragment . update ( fragment . fallback )
122- }
119+ fragment . update ( fragment . fallback )
123120 } else {
124121 // vdom slots
125122 }
126123}
127124
125+ function findInvalidFragment ( fragment : VaporFragment ) : VaporFragment | null {
126+ if ( isValidBlock ( fragment . nodes ) ) return null
127+
128+ return isFragment ( fragment . nodes )
129+ ? findInvalidFragment ( fragment . nodes ) || fragment
130+ : fragment
131+ }
132+
128133export function isFragment ( val : NonNullable < unknown > ) : val is VaporFragment {
129134 return val instanceof VaporFragment
130135}
0 commit comments