Skip to content
Open
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
115 changes: 115 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,121 @@ describe('SSR hydration', () => {
expect((container.firstChild as any)._trueValue).toBe(true)
})

test('preserves pre-hydration user input value for text v-model', async () => {
const App = {
data() {
return {
text: 'test value',
}
},
template: `<div><input v-model="text"><span class="text">{{ text }}</span></div>`,
}

const container = document.createElement('div')
container.innerHTML = await renderToString(h(App))

const input = container.querySelector('input') as HTMLInputElement
input.value = 'edited before hydration'

createSSRApp(App).mount(container)

expect((container.querySelector('input') as HTMLInputElement).value).toBe(
'edited before hydration',
)
await nextTick()
expect(container.querySelector('.text')!.textContent).toBe(
'edited before hydration',
)
})

test('preserves pre-hydration checkbox v-model state', async () => {
const App = {
data() {
return {
checked: false,
}
},
template: `<div><input type="checkbox" v-model="checked"><span class="checked">{{ checked }}</span></div>`,
}

const container = document.createElement('div')
container.innerHTML = await renderToString(h(App))

const input = container.querySelector('input') as HTMLInputElement
input.checked = true

createSSRApp(App).mount(container)

expect((container.querySelector('input') as HTMLInputElement).checked).toBe(
true,
)
await nextTick()
expect(container.querySelector('.checked')!.textContent).toBe('true')
})

test('preserves pre-hydration radio v-model state', async () => {
const App = {
data() {
return {
picked: 'a',
}
},
template: `<div>
<input type="radio" value="a" v-model="picked">
<input type="radio" value="b" v-model="picked">
<span class="picked">{{ picked }}</span>
</div>`,
}

const container = document.createElement('div')
container.innerHTML = await renderToString(h(App))

const radios = container.querySelectorAll<HTMLInputElement>('input')
const a = radios[0]!
const b = radios[1]!
a.checked = false
b.checked = true

createSSRApp(App).mount(container)

expect(
container.querySelectorAll<HTMLInputElement>('input')[1].checked,
).toBe(true)
await nextTick()
expect(container.querySelector('.picked')!.textContent).toBe('b')
})

test('preserves pre-hydration select v-model value', async () => {
const App = {
data() {
return {
selected: 'a',
}
},
template: `<div>
<select v-model="selected">
<option value="a">A</option>
<option value="b">B</option>
</select>
<span class="selected">{{ selected }}</span>
</div>`,
}

const container = document.createElement('div')
container.innerHTML = await renderToString(h(App))

const select = container.querySelector('select') as HTMLSelectElement
select.selectedIndex = 1

createSSRApp(App).mount(container)

expect((container.querySelector('select') as HTMLSelectElement).value).toBe(
'b',
)
await nextTick()
expect(container.querySelector('.selected')!.textContent).toBe('b')
})

test('force hydrate checkbox with indeterminate', () => {
const { container } = mountWithHydration(
'<input type="checkbox" indeterminate>',
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-core/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type DirectiveHook<
binding: DirectiveBinding<Value, Modifiers, Arg>,
vnode: VNode<any, HostElement>,
prevVNode: Prev,
isHydrating?: boolean,
) => void

export type SSRDirectiveHook<
Expand Down Expand Up @@ -172,6 +173,7 @@ export function invokeDirectiveHook(
prevVNode: VNode | null,
instance: ComponentInternalInstance | null,
name: keyof ObjectDirective,
isHydrating = false,
): void {
const bindings = vnode.dirs!
const oldBindings = prevVNode && prevVNode.dirs!
Expand All @@ -193,6 +195,7 @@ export function invokeDirectiveHook(
binding,
vnode,
prevVNode,
isHydrating,
])
resetTracking()
}
Expand Down
7 changes: 4 additions & 3 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export function createHydrationFunctions(
// #5405 in dev, always hydrate children for HMR
if (__DEV__ || forcePatch || patchFlag !== PatchFlags.CACHED) {
if (dirs) {
invokeDirectiveHook(vnode, null, parentComponent, 'created')
invokeDirectiveHook(vnode, null, parentComponent, 'created', true)
}

// handle appear transition
Expand Down Expand Up @@ -537,7 +537,7 @@ export function createHydrationFunctions(
invokeVNodeHook(vnodeHooks, parentComponent, vnode)
}
if (dirs) {
invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount', true)
}
if (
(vnodeHooks = props && props.onVnodeMounted) ||
Expand All @@ -547,7 +547,8 @@ export function createHydrationFunctions(
queueEffectWithSuspense(() => {
vnodeHooks && invokeVNodeHook(vnodeHooks, parentComponent, vnode)
needCallTransitionHooks && transition!.enter(el)
dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
dirs &&
invokeDirectiveHook(vnode, null, parentComponent, 'mounted', true)
}, parentSuspense)
}
}
Expand Down
Loading
Loading