Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydration): force patch dynamic props when hydrating #9083

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2173,5 +2173,30 @@ describe('SSR hydration', () => {
)
expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
})

// #9033
test('force patch dynamic props when hydrating', () => {
const timestamp = Date.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this test needs to use Date.now()? It may be more 'real', but it also makes the test less reliable. The two calls to Date.now() might yield the same number, or they might not. Couldn't the same functionality be tested using fixed values instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no problem with using Date.now(), but it makes more sense to use a fixed value.

let timestampCur = 0
const { container } = mountWithHydration(
`<div><div>${timestamp}</div></div>`,
() => {
timestampCur = Date.now()
return (
openBlock(),
createElementBlock('div', null, [
createElementVNode(
'div',
{ innerHTML: timestampCur },
null,
8 /* PROPS */,
['innerHTML'],
),
])
)
},
)
expect(container.innerHTML).toBe(`<div><div>${timestampCur}</div></div>`)
})
})
})
17 changes: 14 additions & 3 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,21 @@ export function createHydrationFunctions(
optimized: boolean,
) => {
optimized = optimized || !!vnode.dynamicChildren
const { type, props, patchFlag, shapeFlag, dirs, transition } = vnode
const {
type,
dynamicProps,
props,
patchFlag,
shapeFlag,
dirs,
transition,
} = vnode
// #4006 for form elements with non-string v-model value bindings
// e.g. <option :value="obj">, <input type="checkbox" :true-value="1">
// #7476 <input indeterminate>
const forcePatch = type === 'input' || type === 'option'
// #9033 force patch dynamic props when hydrating
// e.g. <div v-html="timestamp" /> & (let timestamp = Date.now();)
const forcePatch = type === 'input' || type === 'option' || dynamicProps
// skip props & children if this is hoisted static nodes
// #5405 in dev, always hydrate children for HMR
if (__DEV__ || forcePatch || patchFlag !== PatchFlags.CACHED) {
Expand Down Expand Up @@ -483,7 +493,8 @@ export function createHydrationFunctions(
(isOn(key) && !isReservedProp(key)) ||
// force hydrate v-bind with .prop modifiers
key[0] === '.' ||
isCustomElement
isCustomElement ||
(dynamicProps && dynamicProps.includes(key))
) {
patchProp(el, key, null, props[key], undefined, parentComponent)
}
Expand Down