Skip to content

Commit 53564e0

Browse files
committed
fix(runtime-vapor): use slot fallback on initial render
1 parent 9ab8e4c commit 53564e0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

+37
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,43 @@ describe('component: slots', () => {
469469
expect(html()).toBe('content<!--if--><!--slot-->')
470470
})
471471

472+
test('use fallback on initial render', async () => {
473+
const Child = {
474+
setup() {
475+
return createSlot('default', null, () =>
476+
document.createTextNode('fallback'),
477+
)
478+
},
479+
}
480+
481+
const toggle = ref(false)
482+
483+
const { html } = define({
484+
setup() {
485+
return createComponent(Child, null, {
486+
default: () => {
487+
return createIf(
488+
() => toggle.value,
489+
() => {
490+
return document.createTextNode('content')
491+
},
492+
)
493+
},
494+
})
495+
},
496+
}).render()
497+
498+
expect(html()).toBe('fallback<!--if--><!--slot-->')
499+
500+
toggle.value = true
501+
await nextTick()
502+
expect(html()).toBe('content<!--if--><!--slot-->')
503+
504+
toggle.value = false
505+
await nextTick()
506+
expect(html()).toBe('fallback<!--if--><!--slot-->')
507+
})
508+
472509
test('dynamic slot work with v-if', async () => {
473510
const val = ref('header')
474511
const toggle = ref(false)

packages/runtime-vapor/src/componentSlots.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { EMPTY_OBJ, NO, hasOwn, isArray, isFunction } from '@vue/shared'
2-
import { type Block, type BlockFn, DynamicFragment, insert } from './block'
2+
import {
3+
type Block,
4+
type BlockFn,
5+
DynamicFragment,
6+
insert,
7+
isValidBlock,
8+
} from './block'
39
import { rawPropsProxyHandlers } from './componentProps'
410
import { currentInstance, isRef } from '@vue/runtime-dom'
511
import type { LooseRawProps, VaporComponentInstance } from './component'
@@ -127,7 +133,13 @@ export function createSlot(
127133
(slot._bound = () => {
128134
const slotContent = slot(slotProps)
129135
if (slotContent instanceof DynamicFragment) {
130-
slotContent.fallback = fallback
136+
if (
137+
(slotContent.fallback = fallback) &&
138+
!isValidBlock(slotContent.nodes)
139+
) {
140+
// use fallback if the slot content is invalid
141+
slotContent.update(fallback)
142+
}
131143
}
132144
return slotContent
133145
}),

0 commit comments

Comments
 (0)