fix(ssr): avoid scopeId inheritance when recursing components

This commit is contained in:
yangchangtao 2024-10-14 18:54:08 +08:00
parent 35785f3cd7
commit 37bb78a197
2 changed files with 23 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import { createApp, h, mergeProps, withCtx } from 'vue'
import { createApp, createVNode, h, mergeProps, withCtx } from 'vue'
import { renderToString } from '../src/renderToString'
import { ssrRenderAttrs, ssrRenderComponent, ssrRenderSlot } from '../src'
import { renderVNode } from '../src/render'
describe('ssr: scopedId runtime behavior', () => {
test('id on component root', async () => {
@ -269,4 +270,23 @@ describe('ssr: scopedId runtime behavior', () => {
`</div>`,
)
})
// #12159
test('avoid scopeId inheritance when recursing components', async () => {
let count = 2
const Comp = {
__scopeId: 'comp',
ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
if (--count) {
push(ssrRenderComponent(h(Comp), attrs, null, parent))
} else {
renderVNode(push, createVNode('div', attrs, 'vuejs'), parent)
}
},
}
const result = await renderToString(createApp(Comp)) // output: `<div></div>`
expect(result).toBe(`<div comp>vuejs</div>`)
})
})

View File

@ -19,6 +19,7 @@ import {
ShapeFlags,
escapeHtml,
escapeHtmlComment,
hasOwn,
isArray,
isFunction,
isPromise,
@ -303,7 +304,7 @@ function renderElementVNode(
openTag += ssrRenderAttrs(props, tag)
}
if (scopeId) {
if (scopeId && (!props || !hasOwn(props, scopeId))) {
openTag += ` ${scopeId}`
}
// inherit parent chain scope id if this is the root node