Skip to content

Commit a1b491d

Browse files
committed
refactor: tweaks
1 parent b8ba13a commit a1b491d

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

packages/runtime-core/__tests__/hydration.spec.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,28 @@ describe('SSR hydration', () => {
393393
)
394394
})
395395

396+
// #6152
397+
test('Teleport (disabled + as component root)', () => {
398+
const { container } = mountWithHydration(
399+
'<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
400+
() => [
401+
h('div', 'Parent fragment'),
402+
h(() =>
403+
h(Teleport, { to: 'body', disabled: true }, [
404+
h('div', 'Teleport content')
405+
])
406+
)
407+
]
408+
)
409+
expect(document.body.innerHTML).toBe('')
410+
expect(container.innerHTML).toBe(
411+
'<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->'
412+
)
413+
expect(
414+
`Hydration completed but contains mismatches.`
415+
).not.toHaveBeenWarned()
416+
})
417+
396418
test('Teleport (as component root)', () => {
397419
const teleportContainer = document.createElement('div')
398420
teleportContainer.id = 'teleport4'
@@ -1083,29 +1105,5 @@ describe('SSR hydration', () => {
10831105
expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
10841106
expect(`Hydration children mismatch`).toHaveBeenWarned()
10851107
})
1086-
// #6152
1087-
test('Teleport is disabled', () => {
1088-
const { container } = mountWithHydration(
1089-
'<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
1090-
() => [
1091-
h('div', 'Parent fragment'),
1092-
h(
1093-
defineComponent(
1094-
() => () =>
1095-
h(Teleport, { to: 'body', disabled: true }, [
1096-
h('div', 'Teleport content')
1097-
])
1098-
)
1099-
)
1100-
]
1101-
)
1102-
expect(document.body.innerHTML).toBe('')
1103-
expect(container.innerHTML).toBe(
1104-
'<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->'
1105-
)
1106-
expect(
1107-
`Hydration completed but contains mismatches.`
1108-
).not.toHaveBeenWarned()
1109-
})
11101108
})
11111109
})

packages/runtime-core/src/hydration.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,19 @@ export function createHydrationFunctions(
227227
optimized
228228
)
229229

230-
// component may be async, so in the case of fragments we cannot rely
231-
// on component's rendered output to determine the end of the fragment
232-
// instead, we do a lookahead to find the end anchor node.
233-
nextNode = isFragmentStart
234-
? locateClosingAnchor(node, '[', ']')
235-
: // #4293 #6152 if teleport start look ahead for teleport end.
236-
isComment(node) && node.data === 'teleport start'
237-
? locateClosingAnchor(node, 'teleport start', 'teleport end')
238-
: nextSibling(node)
230+
// Locate the next node.
231+
if (isFragmentStart) {
232+
// If it's a fragment: since components may be async, we cannot rely
233+
// on component's rendered output to determine the end of the
234+
// fragment. Instead, we do a lookahead to find the end anchor node.
235+
nextNode = locateClosingAnchor(node)
236+
} else if (isComment(node) && node.data === 'teleport start') {
237+
// #4293 #6152
238+
// If a teleport is at component root, look ahead for teleport end.
239+
nextNode = locateClosingAnchor(node, node.data, 'teleport end')
240+
} else {
241+
nextNode = nextSibling(node)
242+
}
239243

240244
// #3787
241245
// if component is async, it may get moved / unmounted before its
@@ -527,7 +531,7 @@ export function createHydrationFunctions(
527531

528532
if (isFragment) {
529533
// remove excessive fragment nodes
530-
const end = locateClosingAnchor(node, '[', ']')
534+
const end = locateClosingAnchor(node)
531535
while (true) {
532536
const next = nextSibling(node)
533537
if (next && next !== end) {
@@ -558,15 +562,15 @@ export function createHydrationFunctions(
558562
// looks ahead for a start and closing comment node
559563
const locateClosingAnchor = (
560564
node: Node | null,
561-
startData: string,
562-
endData: string
565+
open = '[',
566+
close = ']'
563567
): Node | null => {
564568
let match = 0
565569
while (node) {
566570
node = nextSibling(node)
567571
if (node && isComment(node)) {
568-
if (node.data === startData) match++
569-
if (node.data === endData) {
572+
if (node.data === open) match++
573+
if (node.data === close) {
570574
if (match === 0) {
571575
return nextSibling(node)
572576
} else {

0 commit comments

Comments
 (0)