Просмотр исходного кода

refactor: only create anchor for forwarded slots in ssr vnode-based slot

daiwei 10 месяцев назад
Родитель
Сommit
d5b5108549
2 измененных файлов с 13 добавлено и 24 удалено
  1. 5 14
      packages/runtime-vapor/src/block.ts
  2. 8 10
      packages/runtime-vapor/src/fragment.ts

+ 5 - 14
packages/runtime-vapor/src/block.ts

@@ -14,7 +14,11 @@ import {
 } from '@vue/runtime-dom'
 import { isHydrating } from './dom/hydration'
 import { getInheritedScopeIds } from '@vue/runtime-dom'
-import { DynamicFragment, type VaporFragment, isFragment } from './fragment'
+import {
+  type DynamicFragment,
+  type VaporFragment,
+  isFragment,
+} from './fragment'
 
 export interface TransitionOptions {
   $key?: any
@@ -165,19 +169,6 @@ export function normalizeAnchor(node: Block): Node | undefined {
   }
 }
 
-export function findLastChild(node: Block): Node | undefined | null {
-  if (node && node instanceof Node) {
-    return node
-  } else if (isArray(node)) {
-    return findLastChild(node[node.length - 1])
-  } else if (isVaporComponent(node)) {
-    return findLastChild(node.block!)
-  } else {
-    if (node instanceof DynamicFragment) return node.anchor
-    return findLastChild(node.nodes!)
-  }
-}
-
 /**
  * dev / test only
  */

+ 8 - 10
packages/runtime-vapor/src/fragment.ts

@@ -5,7 +5,6 @@ import {
   type BlockFn,
   type TransitionOptions,
   type VaporTransitionHooks,
-  findLastChild,
   insert,
   isValidBlock,
   remove,
@@ -153,17 +152,16 @@ export class DynamicFragment extends VaporFragment {
       this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, '')!
     } else {
       this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, label)!
-      if (!this.anchor && label === 'slot') {
-        // fallback to fragment end anchor for
-        this.anchor = locateVaporFragmentAnchor(currentHydrationNode!, ']')!
-      }
 
-      // anchors are not present in ssr slot vnode fallback
-      if (!this.anchor) {
-        const { parentNode, nextSibling } = findLastChild(this.nodes)!
+      // forwarded slots anchors are not present in ssr vnode-based slot
+      if (
+        !this.anchor &&
+        this.nodes instanceof DynamicFragment &&
+        this.nodes.forwarded
+      ) {
+        const { parentNode, nextSibling } = this.nodes.anchor
         parentNode!.insertBefore(
-          // TODO use empty text node in PROD?
-          (this.anchor = createComment(label)),
+          (this.anchor = __DEV__ ? createComment(label) : createTextNode()),
           nextSibling,
         )
       }