Skip to content

fix(runtime-core): properly diff slot node and fallback node #9213

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
77 changes: 77 additions & 0 deletions packages/runtime-core/__tests__/rendererOptimizedMode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
createApp,
createBlock,
createCommentVNode,
createElementBlock,
createElementVNode,
createTextVNode,
createVNode,
defineComponent,
Expand Down Expand Up @@ -697,6 +699,81 @@ describe('renderer: optimized mode', () => {
)
})

test('force full diff slot and fallback nodes', async () => {
const Comp = {
props: ['show'],
setup(props: any, { slots }: SetupContext) {
return () => {
return (
openBlock(),
createElementBlock('div', null, [
renderSlot(slots, 'default', { hide: !props.show }, () => [
true
? (openBlock(),
(block = createElementBlock(
Fragment,
{ key: 0 },
[createTextVNode('foo')],
PatchFlags.STABLE_FRAGMENT,
)))
: createCommentVNode('v-if', true),
]),
])
)
}
},
}

const show = ref(true)
const app = createApp({
render() {
return (
openBlock(),
createBlock(
Comp,
{ show: show.value },
{
default: withCtx(({ hide }: { hide: boolean }) => [
!hide
? (openBlock(),
createElementBlock(
Fragment,
{ key: 0 },
[
createCommentVNode('comment'),
createElementVNode(
'div',
null,
'bar',
PatchFlags.HOISTED,
),
],
PatchFlags.STABLE_FRAGMENT,
))
: createCommentVNode('v-if', true),
]),
_: SlotFlags.STABLE,
},
PatchFlags.PROPS,
['show'],
)
)
},
})

app.mount(root)
expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
expect(block).toBe(null)

show.value = false
await nextTick()
expect(inner(root)).toBe('<div>foo</div>')

show.value = true
await nextTick()
expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
})

// 3569
test('should force bailout when the user manually calls the slot function', async () => {
const index = ref(0)
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-core/src/helpers/renderSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export function renderSlot(
currentRenderingInstance!.parent.isCE)
) {
if (name !== 'default') props.name = name
return createVNode('slot', props, fallback && fallback())
return createVNode(
'slot',
props,
fallback && fallback(),
fallback ? PatchFlags.BAIL : undefined,
)
}

let slot = slots[name]
Expand Down
14 changes: 9 additions & 5 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ function baseCreateRenderer(
dynamicChildren = null
}

if (dynamicChildren) {
if (optimized && dynamicChildren) {
patchBlockChildren(
n1.dynamicChildren!,
dynamicChildren,
Expand Down Expand Up @@ -1062,11 +1062,14 @@ function baseCreateRenderer(
let { patchFlag, dynamicChildren, slotScopeIds: fragmentSlotScopeIds } = n2

if (
__DEV__ &&
// #5523 dev root fragment may inherit directives
(isHmrUpdating || patchFlag & PatchFlags.DEV_ROOT_FRAGMENT)
(__DEV__ &&
// #5523 dev root fragment may inherit directives
// HMR updated / Dev root fragment (w/ comments), force full diff
(isHmrUpdating ||
(patchFlag > 0 && patchFlag & PatchFlags.DEV_ROOT_FRAGMENT))) ||
// #9200 slot and fallback node has different patchFlag, force full diff
(n1 && n1.patchFlag !== n2.patchFlag)
) {
// HMR updated / Dev root fragment (w/ comments), force full diff
patchFlag = 0
optimized = false
dynamicChildren = null
Expand Down Expand Up @@ -1101,6 +1104,7 @@ function baseCreateRenderer(
)
} else {
if (
optimized &&
patchFlag > 0 &&
patchFlag & PatchFlags.STABLE_FRAGMENT &&
dynamicChildren &&
Expand Down