Skip to content

Commit 4652157

Browse files
committed
fix(runtime-vapor): recover hydration that misses a server range
A component the server rendered with one root has no `<!--{-->` range. When the client renders more roots, the roots after the first read the cursor past that server unit. They replaced the next node, also outside the parent element, or threw. A `v-for` or a teleport that found no server range of its own under the cursor threw as well. When a root reads the cursor past the unit, give the unit the range the server renders for several roots. The other roots then go in front of its close. An empty root that trims the unit leaves its anchor as the unit. A list or a teleport without its range puts an empty one in place of the server node, and its content renders in that range. close #16326 close #16327
1 parent 8e02bdf commit 4652157

8 files changed

Lines changed: 476 additions & 34 deletions

File tree

‎packages/runtime-vapor/__tests__/componentSlots.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,7 +4415,7 @@ describe('component: slots', () => {
44154415
})
44164416
const container = document.createElement('div')
44174417
container.innerHTML =
4418-
'<!--{--><main>content</main><!--[--><!--]--><p>after</p><!--}-->'
4418+
'<!--{--><!--{--><!--{--><main>content</main><!--[--><!--]--><p>after</p><!--}--><!--}--><!--}-->'
44194419

44204420
createVaporSSRApp(
44214421
defineVaporComponent(() => createComponent(Parent)),
@@ -4424,7 +4424,7 @@ describe('component: slots', () => {
44244424
expect(slotBlock).toBeInstanceOf(DynamicFragment)
44254425
expect(slotBlock).not.toBeInstanceOf(SlotFragment)
44264426
expect(container.innerHTML).toBe(
4427-
'<!--{--><main>content</main><!--[--><!--slot--><!--]--><p>after</p><!--}-->',
4427+
'<!--{--><!--{--><!--{--><main>content</main><!--[--><!--slot--><!--]--><p>after</p><!--}--><!--}--><!--}-->',
44284428
)
44294429
expect(`Hydration node mismatch`).not.toHaveBeenWarned()
44304430
expect(`Hydration children mismatch`).not.toHaveBeenWarned()

‎packages/runtime-vapor/__tests__/hydration/mismatch.spec.ts‎

Lines changed: 300 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,13 @@ describe('mismatch handling', () => {
520520

521521
// multiple root mismatch
522522
const { container } = await mountWithHydration(
523-
`<div class="foo bar"></div><span/>`,
523+
`<!--{--><div class="foo bar"></div><span></span><!--}-->`,
524524
`<div :class="data"></div><span/>`,
525525
ref('foo'),
526526
)
527-
expect(container.innerHTML).toBe('<div class="foo bar"></div><span></span>')
527+
expect(container.innerHTML).toBe(
528+
'<!--{--><div class="foo bar"></div><span></span><!--}-->',
529+
)
528530
expect(`Hydration class mismatch`).toHaveBeenWarned()
529531
})
530532

@@ -561,12 +563,12 @@ describe('mismatch handling', () => {
561563

562564
// multiple root mismatch
563565
const { container } = await mountWithHydration(
564-
`<div style="color:red;"></div><span/>`,
566+
`<!--{--><div style="color:red;"></div><span></span><!--}-->`,
565567
`<div :style="data"></div><span/>`,
566568
ref({ color: 'green' }),
567569
)
568570
expect(container.innerHTML).toBe(
569-
'<div style="color:red;"></div><span></span>',
571+
'<!--{--><div style="color:red;"></div><span></span><!--}-->',
570572
)
571573
expect(`Hydration style mismatch`).toHaveBeenWarned()
572574
})
@@ -602,12 +604,12 @@ describe('mismatch handling', () => {
602604

603605
// mismatch with multiple root
604606
const { container } = await mountWithHydration(
605-
`<div style="color:red;"></div><span/>`,
607+
`<!--{--><div style="color:red;"></div><span></span><!--}-->`,
606608
`<div v-show="data.show" :style="data.style"></div><span/>`,
607609
ref({ show: false, style: 'color: red' }),
608610
)
609611
expect(container.innerHTML).toBe(
610-
'<div style="color: red; display: none;"></div><span></span>',
612+
'<!--{--><div style="color: red; display: none;"></div><span></span><!--}-->',
611613
)
612614
expect(`Hydration style mismatch`).toHaveBeenWarned()
613615
})
@@ -2127,15 +2129,15 @@ describe('mismatch recovery drops the server tail of the block', () => {
21272129
target.innerHTML = `<!--teleport start anchor--><p>P</p><i>I</i><!--teleport anchor-->`
21282130
document.body.appendChild(target)
21292131
const { container } = await mountWithHydration(
2130-
'<!--teleport start--><!--teleport end--><u>after</u>',
2132+
'<!--{--><!--teleport start--><!--teleport end--><u>after</u><!--}-->',
21312133
`<teleport to="#tail-target">${client}</teleport><u>after</u>`,
21322134
ref({ x: 'x' }),
21332135
)
21342136
expect(target.innerHTML).toBe(
21352137
'<!--teleport start anchor--><span title="x">A</span><b title="x">B</b><!--teleport anchor-->',
21362138
)
21372139
expect(container.innerHTML).toBe(
2138-
'<!--teleport start--><!--teleport end--><u>after</u>',
2140+
'<!--{--><!--teleport start--><!--teleport end--><u>after</u><!--}-->',
21392141
)
21402142
expect('Hydration node mismatch').toHaveBeenWarned()
21412143
expect('Hydration children mismatch').toHaveBeenWarned()
@@ -3079,3 +3081,293 @@ describe('v-if chain branch the server rendered', () => {
30793081
expect(u.textContent).toBe('U')
30803082
})
30813083
})
3084+
3085+
describe('a server render with fewer roots', () => {
3086+
const card = (inner = '') =>
3087+
`<template><div class="card"><component :is="data.client ? components.Client : components.Server" />${inner}</div><p>sibling</p></template>`
3088+
3089+
async function hydrateRoots(
3090+
code: string,
3091+
client: string,
3092+
data: Record<string, any> = {},
3093+
components: Record<string, string> = {},
3094+
server = '<i>x</i>',
3095+
) {
3096+
const state = ref<Record<string, any>>({ client: true, ...data })
3097+
const { container } = await testHydration(
3098+
code,
3099+
{
3100+
Server: `<template>${server}</template>`,
3101+
Client: `<template>${client}</template>`,
3102+
...components,
3103+
},
3104+
state,
3105+
{ serverData: ref({ ...state.value, client: false }) },
3106+
)
3107+
expect('Hydration node mismatch').toHaveBeenWarnedTimes(1)
3108+
return { container, data: state }
3109+
}
3110+
3111+
test.each([
3112+
[
3113+
'a sibling of the parent element',
3114+
card(),
3115+
'<!--{--><div class="card"><!--{--><i>x</i><span>y</span><!--}--></div><p>sibling</p><!--}-->',
3116+
],
3117+
[
3118+
'no node after the parent element',
3119+
`<template><div class="card"><component :is="data.client ? components.Client : components.Server" /></div></template>`,
3120+
'<div class="card"><!--{--><i>x</i><span>y</span><!--}--></div>',
3121+
],
3122+
[
3123+
'a sibling in the parent element',
3124+
card('<b>after</b>'),
3125+
'<!--{--><div class="card"><!--{--><i>x</i><span>y</span><!--}--><b>after</b></div><p>sibling</p><!--}-->',
3126+
],
3127+
[
3128+
'a nested parent element',
3129+
`<template><section><div class="card"><component :is="data.client ? components.Client : components.Server" /></div></section><p>sibling</p></template>`,
3130+
'<!--{--><section><div class="card"><!--{--><i>x</i><span>y</span><!--}--></div></section><p>sibling</p><!--}-->',
3131+
],
3132+
])('extra roots stay in the parent with %s', async (_, code, html) => {
3133+
const { container } = await hydrateRoots(code, '<i>x</i><span>y</span>')
3134+
expect(container.innerHTML).toBe(html)
3135+
})
3136+
3137+
test.each([
3138+
[
3139+
'three roots',
3140+
'<i>x</i><span>y</span><em>z</em>',
3141+
'<i>x</i><span>y</span><em>z</em>',
3142+
],
3143+
[
3144+
'a slot outlet',
3145+
'<i>x</i><slot><span>y</span></slot>',
3146+
'<i>x</i><span>y</span><!--slot-->',
3147+
],
3148+
[
3149+
'a nested first root',
3150+
'<components.Inner /><span>y</span>',
3151+
'<i>x</i><span>y</span>',
3152+
],
3153+
[
3154+
'a nested extra root',
3155+
'<i>x</i><components.Extra />',
3156+
'<i>x</i><span>y</span>',
3157+
],
3158+
[
3159+
'a nested first root with more roots',
3160+
'<components.More /><span>y</span>',
3161+
'<!--{--><i>x</i><em>z</em><!--}--><span>y</span>',
3162+
],
3163+
])('an extra root with %s', async (_, client, html) => {
3164+
const { container } = await hydrateRoots(
3165+
card(),
3166+
client,
3167+
{},
3168+
{
3169+
Inner: '<template><i>x</i></template>',
3170+
Extra: '<template><span>y</span></template>',
3171+
More: '<template><i>x</i><em>z</em></template>',
3172+
},
3173+
)
3174+
expect(container.innerHTML).toBe(
3175+
`<!--{--><div class="card"><!--{-->${html}<!--}--></div><p>sibling</p><!--}-->`,
3176+
)
3177+
})
3178+
3179+
test.each([false, true])('an extra v-if root, shown: %s', async show => {
3180+
const { container, data } = await hydrateRoots(
3181+
card(),
3182+
'<i>x</i><span v-if="data.show">y</span>',
3183+
{ show },
3184+
)
3185+
const html = (shown: boolean) =>
3186+
`<!--{--><div class="card"><!--{--><i>x</i>${shown ? '<span>y</span>' : ''}<!--if--><!--}--></div><p>sibling</p><!--}-->`
3187+
expect(container.innerHTML).toBe(html(show))
3188+
data.value.show = !show
3189+
await nextTick()
3190+
expect(container.innerHTML).toBe(html(!show))
3191+
})
3192+
3193+
test('an extra v-for root', async () => {
3194+
const { container, data } = await hydrateRoots(
3195+
card(),
3196+
'<i>x</i><span v-for="i in data.list">{{ i }}</span>',
3197+
{ list: [1] },
3198+
)
3199+
expect(container.innerHTML).toBe(
3200+
'<!--{--><div class="card"><!--{--><i>x</i><!--[--><span>1</span><!--]--><!--}--></div><p>sibling</p><!--}-->',
3201+
)
3202+
data.value.list = [2, 3]
3203+
await nextTick()
3204+
expect(container.innerHTML).toBe(
3205+
'<!--{--><div class="card"><!--{--><i>x</i><!--[--><span>2</span><span>3</span><!--]--><!--}--></div><p>sibling</p><!--}-->',
3206+
)
3207+
})
3208+
3209+
test('an extra disabled teleport root', async () => {
3210+
const { container } = await hydrateRoots(
3211+
card(),
3212+
'<i>x</i><Teleport to="body" disabled><span>t</span></Teleport>',
3213+
)
3214+
expect(container.innerHTML).toBe(
3215+
'<!--{--><div class="card"><!--{--><i>x</i><!--teleport start--><span>t</span><!--teleport end--><!--}--></div><p>sibling</p><!--}-->',
3216+
)
3217+
})
3218+
3219+
test.each([
3220+
[
3221+
'a v-for',
3222+
'<span v-for="i in data.list">{{ i }}</span>',
3223+
(list: number[]) =>
3224+
`<!--[-->${list.map(i => `<span>${i}</span>`).join('')}<!--]-->`,
3225+
],
3226+
[
3227+
'a teleport',
3228+
'<Teleport to="body" disabled><span>{{ data.list.join() }}</span></Teleport>',
3229+
(list: number[]) =>
3230+
`<!--teleport start--><span>${list.join()}</span><!--teleport end-->`,
3231+
],
3232+
])(
3233+
'an empty first root over the server unit, then %s',
3234+
async (_, root, html) => {
3235+
const data = ref({ client: true, list: [1] })
3236+
const { container } = await testHydration(
3237+
`<template><div><component :is="data.client ? components.Client : components.Server" /></div></template>`,
3238+
{
3239+
Server: '<template><b>x</b></template>',
3240+
Client: `<template><component :is="null" />${root}</template>`,
3241+
},
3242+
data,
3243+
{ serverData: ref({ client: false, list: [1] }) },
3244+
)
3245+
const expected = (list: number[]) =>
3246+
`<div><!--{--><!--dynamic-component-->${html(list)}<!--}--></div>`
3247+
expect(container.innerHTML).toBe(expected([1]))
3248+
expect('Hydration children mismatch').toHaveBeenWarnedTimes(1)
3249+
expect('Hydration node mismatch').toHaveBeenWarnedTimes(1)
3250+
data.value.list = [1, 2]
3251+
await nextTick()
3252+
expect(container.innerHTML).toBe(expected([1, 2]))
3253+
},
3254+
)
3255+
3256+
test('a later dynamic sibling in the parent element', async () => {
3257+
const { container, data } = await hydrateRoots(
3258+
card('<b v-if="data.show">{{ data.text }}</b>'),
3259+
'<i>x</i><span>y</span>',
3260+
{ show: true, text: 'after' },
3261+
)
3262+
expect(container.innerHTML).toBe(
3263+
'<!--{--><div class="card"><!--{--><i>x</i><span>y</span><!--}--><b>after</b><!--if--></div><p>sibling</p><!--}-->',
3264+
)
3265+
const b = container.querySelector('b')!
3266+
data.value.text = 'updated'
3267+
await nextTick()
3268+
expect(container.querySelector('b')).toBe(b)
3269+
expect(b.textContent).toBe('updated')
3270+
data.value.show = false
3271+
await nextTick()
3272+
expect(container.innerHTML).toBe(
3273+
'<!--{--><div class="card"><!--{--><i>x</i><span>y</span><!--}--><!--if--></div><p>sibling</p><!--}-->',
3274+
)
3275+
})
3276+
})
3277+
3278+
describe('a v-for root the server rendered no list for', () => {
3279+
const list = '<span v-for="i in data.list">{{ i }}</span>'
3280+
const rows = (items: number[]) => items.map(i => `<span>${i}</span>`).join('')
3281+
3282+
async function hydrateList(
3283+
items: number[],
3284+
after = '',
3285+
server = '<b>plain</b>',
3286+
) {
3287+
const data = ref({ client: true, list: items })
3288+
const { container } = await testHydration(
3289+
`<template><div><component :is="data.client ? components.List : components.Server" />${after}</div><button>add</button></template>`,
3290+
{
3291+
Server: `<template>${server}</template>`,
3292+
List: `<template>${list}</template>`,
3293+
},
3294+
data,
3295+
{ serverData: ref({ client: false, list: items }) },
3296+
)
3297+
return { container, data }
3298+
}
3299+
3300+
test.each([[[]], [[1]], [[1, 2]]])('rows: %j', async items => {
3301+
const { container, data } = await hydrateList(items)
3302+
expect(container.innerHTML).toBe(
3303+
`<!--{--><div><!--[-->${rows(items)}<!--]--><!--dynamic-component--></div><button>add</button><!--}-->`,
3304+
)
3305+
expect('Hydration node mismatch').toHaveBeenWarnedTimes(1)
3306+
data.value.list = [...items, 3]
3307+
await nextTick()
3308+
expect(container.innerHTML).toBe(
3309+
`<!--{--><div><!--[-->${rows([...items, 3])}<!--]--><!--dynamic-component--></div><button>add</button><!--}-->`,
3310+
)
3311+
})
3312+
3313+
test.each([
3314+
['a sibling after it', '<p>sibling</p>', '<b>plain</b>'],
3315+
['a server component that renders no node', '', ''],
3316+
])('with %s', async (_, after, server) => {
3317+
const { container, data } = await hydrateList([1], after, server)
3318+
expect(container.innerHTML).toBe(
3319+
`<!--{--><div><!--[--><span>1</span><!--]--><!--dynamic-component-->${after}</div><button>add</button><!--}-->`,
3320+
)
3321+
expect('Hydration node mismatch').toHaveBeenWarnedTimes(1)
3322+
data.value.list = [1, 2]
3323+
await nextTick()
3324+
expect(container.innerHTML).toBe(
3325+
`<!--{--><div><!--[--><span>1</span><span>2</span><!--]--><!--dynamic-component-->${after}</div><button>add</button><!--}-->`,
3326+
)
3327+
})
3328+
3329+
test('in prod', async () => {
3330+
__DEV__ = false
3331+
try {
3332+
const { container, data } = await hydrateList([])
3333+
expect(container.innerHTML).toBe(
3334+
'<!--{--><div><!--[--><!--]--></div><button>add</button><!--}-->',
3335+
)
3336+
data.value.list = [1, 2]
3337+
await nextTick()
3338+
expect(container.innerHTML).toBe(
3339+
'<!--{--><div><!--[--><span>1</span><span>2</span><!--]--></div><button>add</button><!--}-->',
3340+
)
3341+
} finally {
3342+
__DEV__ = true
3343+
}
3344+
})
3345+
})
3346+
3347+
describe('a teleport the server rendered no teleport for', () => {
3348+
test('as the root on another server node', async () => {
3349+
const target = document.createElement('div')
3350+
target.id = 'mismatch-target'
3351+
document.body.appendChild(target)
3352+
const data = ref({ client: true, text: 't' })
3353+
const { container } = await testHydration(
3354+
`<template><div><component :is="data.client ? components.Client : components.Server" /></div><p>sibling</p></template>`,
3355+
{
3356+
Server: '<template><b>plain</b></template>',
3357+
Client:
3358+
'<template><Teleport to="#mismatch-target"><span>{{ data.text }}</span></Teleport></template>',
3359+
},
3360+
data,
3361+
{ serverData: ref({ client: false, text: 't' }) },
3362+
)
3363+
expect(container.innerHTML).toBe(
3364+
'<!--{--><div><!--teleport start--><!--teleport end--><!--dynamic-component--></div><p>sibling</p><!--}-->',
3365+
)
3366+
expect(target.textContent).toBe('t')
3367+
expect('Hydration node mismatch').toHaveBeenWarnedTimes(1)
3368+
data.value.text = 'u'
3369+
await nextTick()
3370+
expect(target.textContent).toBe('u')
3371+
target.remove()
3372+
})
3373+
})

0 commit comments

Comments
 (0)