Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ describe('Vapor Mode hydration', () => {
"
<!--{--><div>
<!--[--><span>bar</span><!--if--><!--]-->
<!--slot--></div><!--async component--><!--}-->
</div><!--async component--><!--}-->
"
`)
})
Expand Down
74 changes: 73 additions & 1 deletion packages/runtime-vapor/__tests__/hydration/slots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,7 @@ describe('Vapor Mode hydration', () => {
)
expect(container.innerHTML).toBe(
`<div><!--[--><!--[--><!--[--><b>a</b><!--]--><!--]-->` +
`<!--[--><!--]--><!--slot--><!--]--><p>after</p></div>`,
`<!--[--><!--for--><!--]--><!--]--><p>after</p></div>`,
)

data.value.list[1].tags.push('b')
Expand Down Expand Up @@ -2369,5 +2369,77 @@ describe('Vapor Mode hydration', () => {
await nextTick()
expect(texts()).toEqual([row(1), row(2), row(3)])
})

test.each([
[
'a list and a v-if',
`<li v-for="i in data.list">{{ i }}</li>
<li v-if="data.more">more</li>`,
'1more',
],
[
'a v-if and a list',
`<li v-if="data.more">more</li>
<li v-for="i in data.list">{{ i }}</li>`,
'more1',
],
[
'two lists',
`<li v-for="i in data.list">{{ i }}</li>
<li v-for="i in data.list">{{ i }}</li>`,
'11',
],
])('slot content of empty blocks: %s', async (_, content, text) => {
const data = ref({ list: [] as number[], more: false })
const { container } = await testHydration(
`<template>
<components.List>
${content}
</components.List>
<p>after</p>
</template>`,
{ List: `<template><ul><slot/></ul></template>` },
data,
)
const ul = container.querySelector('ul')!
const p = container.querySelector('p')!
expect(ul.textContent).toBe('')
expect(p.parentNode).toBe(container)

data.value.list = [1]
data.value.more = true
await nextTick()
expect(ul.textContent).toBe(text)
expect(container.querySelector('p')).toBe(p)

data.value.list = []
data.value.more = false
await nextTick()
expect(ul.textContent).toBe('')
expect(container.textContent).toBe('after')
})

test('slot content of empty blocks a space apart', async () => {
const data = ref({ list: [] as number[] })
const { container } = await testHydration(
`<template>
<components.Child>
<i v-for="i in data.list">{{ i }}</i> <b v-for="i in data.list">{{ i }}</b>
</components.Child>
<p>after</p>
</template>`,
{ Child: `<template><div><slot/></div></template>` },
data,
)
const div = container.querySelector('div')!
const p = container.querySelector('p')!
expect(div.innerHTML).toBe(`<!--[--><!--for--> <!--for--><!--]-->`)
expect(`Hydration node mismatch`).not.toHaveBeenWarned()

data.value.list = [1]
await nextTick()
expect(div.textContent).toBe('1 1')
expect(container.querySelector('p')).toBe(p)
})
})
})
4 changes: 2 additions & 2 deletions packages/runtime-vapor/__tests__/hydration/transition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1328,10 +1328,10 @@ describe('Vapor Mode hydration', () => {
data,
)
const ul = container.querySelector('ul')!
expect(ul.innerHTML).toBe(`<!--[--><!--]--><!--slot-->`)
expect(ul.innerHTML).toBe(`<!--[--><!--for--><!--]-->`)
data.value.items.push(3)
await nextTick()
expect(ul.innerHTML).toBe(`<!--[--><li>3</li><!--]--><!--slot-->`)
expect(ul.innerHTML).toBe(`<!--[--><li>3</li><!--for--><!--]-->`)
expect(`Hydration children mismatch`).not.toHaveBeenWarned()
expect(
`Hydration completed but contains mismatches.`,
Expand Down
117 changes: 50 additions & 67 deletions packages/runtime-vapor/src/apiCreateFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ import {
claimFragmentStart,
createFragmentClaim,
currentHydrationNode,
enterHydrationBoundary,
enterHydrationCursor,
hydrateBlock,
insertMissingRange,
isClaimedAnchor,
isComment,
isHydrating,
locateClaimedEnd,
setCurrentHydrationNode,
Expand Down Expand Up @@ -542,79 +539,65 @@ export const createFor = (

function hydrateList(source: ResolvedSource, newLength: number): void {
const claim = hydrationClaim!
let exitHydrationBoundary: (() => void) | undefined

// claiming a close marker as the anchor also bounds the cleanup to it
const reuseBoundaryClose = (close: Node): void => {
parentAnchor = claimAnchor(close)
exitHydrationBoundary = enterHydrationBoundary(parentAnchor)
}

// one owner callback for the list: a closure per row would allocate
let row = 0
const mountRow = () => mount(source, row, parentAnchor).nodes
try {
// a close that already has an owner is not the list's to share
const start = currentHydrationNode!
if (!claim.start && (!isComment(start, ']') || isClaimedAnchor(start))) {
insertMissingRange('[', ']', `v-for fragment`)
claimFragmentStart(claim)
}
for (let i = 0; i < newLength; i++) {
const node = currentHydrationNode!
// an unwrapped row is a single node: its own hydration moves on
let nextNode: Node | null = null
let rowStart: CommentAnchor | null = null
if (node.nodeType === 8) {
const data = (node as Comment).data
if (data === ']') {
// fewer server rows: the rest mount before the close marker
nextNode = claimAnchor(node)
} else if (wrappedRows && data === '[') {
rowStart = node as CommentAnchor
setCurrentHydrationNode(node.nextSibling)
}
}
row = i
hydrateBlock(mountRow)
if (rowStart) {
// the row's blocks consumed its range, so the cursor rests on its
// close marker; only a mismatch pays for the walk from the start
// and for dropping what the row no longer adopts
const close = locateClaimedEnd(rowStart)!
trimHydrationBoundary(close)
nextNode = close.nextSibling

// a list always has a range of its own: the server rendered none here
if (!claim.start) {
insertMissingRange('[', ']', `v-for fragment`)
claimFragmentStart(claim)
}
for (let i = 0; i < newLength; i++) {
const node = currentHydrationNode!
// an unwrapped row is a single node: its own hydration moves on
let nextNode: Node | null = null
let rowStart: CommentAnchor | null = null
if (node.nodeType === 8) {
const data = (node as Comment).data
if (data === ']') {
// fewer server rows: the rest mount before the close marker
nextNode = claimAnchor(node)
} else if (wrappedRows && data === '[') {
rowStart = node as CommentAnchor
setCurrentHydrationNode(node.nextSibling)
}
if (nextNode) setCurrentHydrationNode(nextNode)
}
row = i
hydrateBlock(mountRow)
if (rowStart) {
// the row's blocks consumed its range, so the cursor rests on its
// close marker; only a mismatch pays for the walk from the start
// and for dropping what the row no longer adopts
const close = locateClaimedEnd(rowStart)!
trimHydrationBoundary(close)
nextNode = close.nextSibling
}
if (nextNode) setCurrentHydrationNode(nextNode)
}

if (claim.start) {
// the list owns its SSR range: its close marker is the anchor
reuseBoundaryClose(locateClaimedEnd(claim.start)!)

// optimization: cache the fragment end anchor as $llc (last logical child)
// so that locateChildByLogicalIndex can skip the entire fragment.
// For anchored inserts the unit index is known only while the anchor
// is the cached child; otherwise skipping the install just costs a
// restart walk.
if (_insertionParent) {
const idx = _insertionAnchor
? _insertionParent.$llc === _insertionAnchor
? _insertionParent.$lli
: undefined
: _insertionIndex || 0
if (idx !== undefined) {
setLastLocatedLogicalChild(_insertionParent, parentAnchor, idx)
}
}
} else {
// an empty list the server rendered without a range of its own, in
// the range around it: that range's close is the anchor
reuseBoundaryClose(currentHydrationNode!)
// the close marker of the list's range is the anchor
parentAnchor = claimAnchor(locateClaimedEnd(claim.start!)!)

// optimization: cache the fragment end anchor as $llc (last logical child)
// so that locateChildByLogicalIndex can skip the entire fragment.
// For anchored inserts the unit index is known only while the anchor
// is the cached child; otherwise skipping the install just costs a
// restart walk.
if (_insertionParent) {
const idx = _insertionAnchor
? _insertionParent.$llc === _insertionAnchor
? _insertionParent.$lli
: undefined
: _insertionIndex || 0
if (idx !== undefined) {
setLastLocatedLogicalChild(_insertionParent, parentAnchor, idx)
}
} finally {
exitHydrationBoundary && exitHydrationBoundary()
}

// drop what the server rendered past the client's rows
trimHydrationBoundary(parentAnchor)
}

const update = (
Expand Down
6 changes: 2 additions & 4 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { isTransitionEnabled, isVaporTransition } from './transition'
import { isAsyncComponentEnabled } from './asyncComponentState'
import { renderWithSlotScopeIds } from './scopeId'
import { setElementScopeIds } from './dom/scopeIdStamp'
import { withHydratingSlotBoundary } from './dom/hydrateFragment'
import { hydrateSlotContent } from './dom/hydrateFragment'
import { withOnce } from './once'

export type RawSlots = Record<string, VaporSlot> & {
Expand Down Expand Up @@ -565,9 +565,7 @@ export function createSlot(
// DynamicFragment; during hydration it owns the slot SSR range, so
// empty dynamic roots get its close marker.
if (isHydrating) {
withHydratingSlotBoundary(() =>
outlet.update(render || fallback, key),
)
hydrateSlotContent(outlet, render || fallback, key)
} else {
outlet.update(render || fallback, key)
}
Expand Down
49 changes: 46 additions & 3 deletions packages/runtime-vapor/src/dom/hydrateFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ export function locateSlotEndAnchor(): Node | null {
}

/** Locates the boundary's SSR range and consumes its opening marker. */
export function withHydratingSlotBoundary<R>(fn: () => R): R {
const claim = createFragmentClaim()
locateHydrationNode(claim)
export function withHydratingSlotBoundary<R>(
fn: () => R,
// of a caller that located the range itself
claim?: FragmentClaim,
): R {
if (!claim) locateHydrationNode((claim = createFragmentClaim()))
// An empty range: content adopting its close as an anchor moves the cursor
// past it, so the close cannot be read off the cursor afterwards.
const node = currentHydrationNode
Expand Down Expand Up @@ -479,6 +482,46 @@ export function hydrateDynamicFragmentAnchor(
executeAnchorPlan(frag, resolveDynamicAnchor(frag, isEmpty))
}

/**
* The hydrating half of an outlet that renders through a plain
* DynamicFragment. `<!--[--><!--]-->` is the server rendering nothing, also
* for content of several empty blocks: hydrated against the close, the first
* to take it for an anchor would leave the rest outside the range.
*/
export function hydrateSlotContent(
frag: DynamicFragment,
render: BlockFn | undefined,
key: any,
): void {
const claim = createFragmentClaim()
locateHydrationNode(claim)
const open = claim.start
const close = currentHydrationNode
const update = () => frag.update(render, key)
if (open && open.data === '[' && close && isComment(close, ']')) {
// as on the client, inside the range
frag.anchor = claimAnchor(close)
runWithoutHydration(update)
for (
let node = open.nextSibling!;
node !== close;
node = node.nextSibling!
) {
// more than the anchors and blank text the server leaves out
if (
node.nodeType === 1 ||
(node.nodeType === 3 && (node as Text).data.trim())
) {
warnHydrationNodeMismatch(open, `slot content`)
break
}
}
advanceHydrationNode(close)
} else {
withHydratingSlotBoundary(update, claim)
}
}

/**
* The hydrating half of `SlotFragment.updateSlot`. The range the server left
* says what it rendered, so nothing is tried and taken back:
Expand Down
Loading