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 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
75 changes: 75 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,79 @@ describe('renderer: optimized mode', () => {
)
})

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

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
1 change: 1 addition & 0 deletions packages/runtime-core/src/helpers/renderSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function renderSlot(
if (slot && (slot as ContextualRenderFn)._c) {
;(slot as ContextualRenderFn)._d = true
}
rendered.isSlotFallback = !validSlotContent
return rendered
}

Expand Down
20 changes: 19 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,25 @@ function baseCreateRenderer(
const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateText(''))!
const fragmentEndAnchor = (n2.anchor = n1 ? n1.anchor : hostCreateText(''))!

let { patchFlag, dynamicChildren, slotScopeIds: fragmentSlotScopeIds } = n2
let {
patchFlag,
dynamicChildren,
slotScopeIds: fragmentSlotScopeIds,
isSlotFallback,
} = n2

// #9200 when patching the slot vnode and the slot fallback vnode
// cannot take the fast path, nor can reuse the old vnode because they
// are from different templates
if (
n1 &&
n1.isSlotFallback !== undefined &&
isSlotFallback !== undefined &&
n1.isSlotFallback !== isSlotFallback
) {
unmount(n1!, parentComponent, parentSuspense, true)
n1 = null
}

if (
__DEV__ &&
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ export interface VNode<
* @internal custom element interception hook
*/
ce?: (instance: ComponentInternalInstance) => void
/**
* @internal is slot fallback node
*/
isSlotFallback?: boolean
}

// Since v-if and v-for are the two possible ways node structure can dynamically
Expand Down