perf: support only attaching slot scope ids when necessary
This is done by adding the `slotted: false` option to: - compiler-dom - compiler-ssr - compiler-sfc (forwarded to template compiler) At runtime, only slotted component will render slot fragments with slot scope Ids. For SSR, only slotted component will add slot scope Ids to rendered slot content. This should improve both runtime performance and reduce SSR rendered markup size. Note: requires SFC tooling (e.g. `vue-loader` and `vite`) to pass on the `slotted` option from the SFC descriptoer to the `compileTemplate` call.
This commit is contained in:
@@ -40,7 +40,7 @@ describe('scopeId runtime support', () => {
|
||||
const Child = {
|
||||
__scopeId: 'child',
|
||||
render(this: any) {
|
||||
return h('div', renderSlot(this.$slots, 'default'))
|
||||
return h('div', renderSlot(this.$slots, 'default', {}, undefined, true))
|
||||
}
|
||||
}
|
||||
const Child2 = {
|
||||
@@ -92,7 +92,9 @@ describe('scopeId runtime support', () => {
|
||||
render(this: any) {
|
||||
// <Wrapper><slot/></Wrapper>
|
||||
return h(Wrapper, null, {
|
||||
default: withCtx(() => [renderSlot(this.$slots, 'default')])
|
||||
default: withCtx(() => [
|
||||
renderSlot(this.$slots, 'default', {}, undefined, true)
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -118,8 +120,8 @@ describe('scopeId runtime support', () => {
|
||||
render(h(Root), root)
|
||||
expect(serializeInner(root)).toBe(
|
||||
`<div class="wrapper" wrapper slotted root>` +
|
||||
`<div root wrapper-s slotted-s>hoisted</div>` +
|
||||
`<div root wrapper-s slotted-s>dynamic</div>` +
|
||||
`<div root slotted-s>hoisted</div>` +
|
||||
`<div root slotted-s>dynamic</div>` +
|
||||
`</div>`
|
||||
)
|
||||
|
||||
@@ -144,9 +146,9 @@ describe('scopeId runtime support', () => {
|
||||
render(h(Root2), root2)
|
||||
expect(serializeInner(root2)).toBe(
|
||||
`<div class="wrapper" wrapper slotted root>` +
|
||||
`<div class="wrapper" wrapper root wrapper-s slotted-s>` +
|
||||
`<div root wrapper-s>hoisted</div>` +
|
||||
`<div root wrapper-s>dynamic</div>` +
|
||||
`<div class="wrapper" wrapper root slotted-s>` +
|
||||
`<div root>hoisted</div>` +
|
||||
`<div root>dynamic</div>` +
|
||||
`</div>` +
|
||||
`</div>`
|
||||
)
|
||||
|
||||
@@ -25,7 +25,8 @@ export function renderSlot(
|
||||
props: Data = {},
|
||||
// this is not a user-facing function, so the fallback is always generated by
|
||||
// the compiler and guaranteed to be a function returning an array
|
||||
fallback?: () => VNodeArrayChildren
|
||||
fallback?: () => VNodeArrayChildren,
|
||||
hasSlotted?: boolean
|
||||
): VNode {
|
||||
let slot = slots[name]
|
||||
|
||||
@@ -53,8 +54,7 @@ export function renderSlot(
|
||||
? PatchFlags.STABLE_FRAGMENT
|
||||
: PatchFlags.BAIL
|
||||
)
|
||||
// TODO (optimization) only add slot scope id if :slotted is used
|
||||
if (rendered.scopeId) {
|
||||
if (hasSlotted && rendered.scopeId) {
|
||||
rendered.slotScopeIds = [rendered.scopeId + '-s']
|
||||
}
|
||||
isRenderingCompiledSlot--
|
||||
|
||||
Reference in New Issue
Block a user