@@ -74,18 +74,19 @@ export class DynamicFragment extends VaporFragment {
74
74
75
75
if ( this . fallback ) {
76
76
// set fallback for nested fragments
77
- const isFrag = isFragment ( this . nodes )
78
- if ( isFrag ) {
77
+ const hasNestedFragment = isFragment ( this . nodes )
78
+ if ( hasNestedFragment ) {
79
79
setFragmentFallback ( this . nodes as VaporFragment , this . fallback )
80
80
}
81
81
82
- if ( ! isValidBlock ( this . nodes ) ) {
82
+ const invalidFragment = findInvalidFragment ( this )
83
+ if ( invalidFragment ) {
83
84
parent && remove ( this . nodes , parent )
84
85
const scope = this . scope || ( this . scope = new EffectScope ( ) )
85
86
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 )
89
90
} else {
90
91
this . nodes = this . fallback ! ( ) || [ ]
91
92
}
@@ -102,9 +103,10 @@ function setFragmentFallback(
102
103
fragment : VaporFragment ,
103
104
fallback : BlockFn | undefined ,
104
105
) : 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
108
110
if ( isFragment ( fragment . nodes ) ) {
109
111
setFragmentFallback ( fragment . nodes , fallback )
110
112
}
@@ -114,17 +116,20 @@ function renderFragmentFallback(fragment: VaporFragment): void {
114
116
if ( fragment instanceof ForFragment ) {
115
117
fragment . nodes [ 0 ] = [ fragment . fallback ! ( ) || [ ] ] as Block [ ]
116
118
} 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 )
123
120
} else {
124
121
// vdom slots
125
122
}
126
123
}
127
124
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
+
128
133
export function isFragment ( val : NonNullable < unknown > ) : val is VaporFragment {
129
134
return val instanceof VaporFragment
130
135
}
0 commit comments