Skip to content

Commit 027da6b

Browse files
authored
fix(ssr): dedupe inherited scope ids during vnode rendering (#15005)
close #12159 close #12175
1 parent 024cf06 commit 027da6b

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎packages/server-renderer/__tests__/ssrScopeId.spec.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { createApp, h, mergeProps, withCtx } from 'vue'
1+
import { createApp, createVNode, h, mergeProps, withCtx } from 'vue'
22
import { renderToString } from '../src/renderToString'
3-
import { ssrRenderAttrs, ssrRenderComponent, ssrRenderSlot } from '../src'
3+
import {
4+
ssrRenderAttrs,
5+
ssrRenderComponent,
6+
ssrRenderSlot,
7+
ssrRenderVNode,
8+
} from '../src'
49

510
describe('ssr: scopedId runtime behavior', () => {
611
test('id on component root', async () => {
@@ -269,4 +274,37 @@ describe('ssr: scopedId runtime behavior', () => {
269274
`</div>`,
270275
)
271276
})
277+
278+
// #12159
279+
test('avoid duplicate scopeId through recursive ssr vnode roots', async () => {
280+
let count = 2
281+
282+
const Comp = {
283+
__scopeId: 'comp',
284+
ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
285+
if (--count) {
286+
push(ssrRenderComponent(Comp, attrs, null, parent))
287+
} else {
288+
ssrRenderVNode(push, createVNode('div', attrs, 'vuejs'), parent)
289+
}
290+
},
291+
}
292+
293+
const result = await renderToString(createApp(Comp))
294+
expect(result).toBe(`<div comp>vuejs</div>`)
295+
})
296+
297+
test('avoid duplicate scopeId through recursive render roots', async () => {
298+
let count = 2
299+
300+
const Comp = {
301+
__scopeId: 'comp',
302+
render(this: any) {
303+
return --count ? h(Comp, this.$attrs) : h('div', this.$attrs, 'vuejs')
304+
},
305+
}
306+
307+
const result = await renderToString(createApp(Comp))
308+
expect(result).toBe(`<div comp>vuejs</div>`)
309+
})
272310
})

‎packages/server-renderer/src/render.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ShapeFlags,
2020
escapeHtml,
2121
escapeHtmlComment,
22+
hasOwn,
2223
isArray,
2324
isFunction,
2425
isPromise,
@@ -303,21 +304,36 @@ function renderElementVNode(
303304
openTag += ssrRenderAttrs(props, tag)
304305
}
305306

307+
const renderedScopeIds: string[] = []
308+
const appendScopeId = (id: string) => {
309+
if (
310+
id &&
311+
(!props || !hasOwn(props, id)) &&
312+
!renderedScopeIds.includes(id)
313+
) {
314+
openTag += ` ${id}`
315+
renderedScopeIds.push(id)
316+
}
317+
}
318+
306319
if (scopeId) {
307-
openTag += ` ${scopeId}`
320+
appendScopeId(scopeId)
308321
}
309322
// inherit parent chain scope id if this is the root node
310323
let curParent: ComponentInternalInstance | null = parentComponent
311324
let curVnode = vnode
312325
while (curParent && curVnode === curParent.subTree) {
313326
curVnode = curParent.vnode
314327
if (curVnode.scopeId) {
315-
openTag += ` ${curVnode.scopeId}`
328+
appendScopeId(curVnode.scopeId)
316329
}
317330
curParent = curParent.parent
318331
}
319332
if (slotScopeId) {
320-
openTag += ` ${slotScopeId}`
333+
const slotScopeIdList = slotScopeId.trim().split(' ')
334+
for (let i = 0; i < slotScopeIdList.length; i++) {
335+
appendScopeId(slotScopeIdList[i])
336+
}
321337
}
322338

323339
push(openTag + `>`)

0 commit comments

Comments
 (0)