Skip to content

Commit 31cc225

Browse files
committed
wip: create anchor for DynamicFragment in ssr slot vnode fallback
1 parent 7126238 commit 31cc225

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

packages/runtime-vapor/src/fragment.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class DynamicFragment extends VaporFragment {
7474

7575
update(render?: BlockFn, key: any = render): void {
7676
if (key === this.current) {
77-
if (isHydrating) this.hydrate(this.anchorLabel!, true)
77+
if (isHydrating && this.anchorLabel) this.hydrate(this.anchorLabel!, true)
7878
return
7979
}
8080
this.current = key
@@ -155,30 +155,33 @@ export class DynamicFragment extends VaporFragment {
155155
}
156156

157157
hydrate(label: string, isEmpty: boolean = false): void {
158-
if (!label && isEmpty) return
159-
160-
// for `v-if="false"` the node will be an empty comment, use it as the anchor.
158+
// for `v-if="false"`, the node will be an empty comment, use it as the anchor.
161159
// otherwise, find next sibling vapor fragment anchor
162160
if (label === 'if' && isEmpty) {
163161
this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, '')!
164162
} else {
165-
let anchor = locateVaporFragmentAnchor(currentHydrationNode!, label)!
166-
if (!anchor) {
167-
// TODO: comment anchors are not included in ssr slot vnode fallback
163+
this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, label)!
164+
if (!this.anchor) {
165+
// comment anchors are not included in ssr slot vnode fallback
168166
if (label === 'slot') {
169167
// fallback to fragment end anchor for
170-
anchor = locateVaporFragmentAnchor(currentHydrationNode!, ']')!
171-
} else if (label === 'if') {
168+
this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, ']')!
169+
} else {
170+
// create anchor
171+
const { parentNode, nextSibling } = currentHydrationNode!
172+
parentNode!.insertBefore(
173+
(this.anchor = __DEV__ ? createComment(label) : createTextNode()),
174+
nextSibling,
175+
)
172176
}
173177
}
174-
if (anchor) {
175-
this.anchor = anchor
176-
} else if (__DEV__) {
177-
// this should not happen
178-
throw new Error(`${label} fragment anchor node was not found.`)
179-
}
180178
}
181-
advanceHydrationNode(this.anchor)
179+
180+
if (this.anchor) {
181+
advanceHydrationNode(this.anchor)
182+
} else if (__DEV__) {
183+
throw new Error(`${label} fragment anchor node was not found.`)
184+
}
182185
}
183186
}
184187

0 commit comments

Comments
 (0)