Skip to content

Commit b1351be

Browse files
committed
support _scopeId in SSR
1 parent b0ad94f commit b1351be

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

src/platforms/web/server/modules/attrs.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ import {
66
isFalsyAttrValue
77
} from 'web/util/attrs'
88

9-
export default function renderAttrs (node: VNodeWithData): ?string {
10-
if (node.data.attrs || node.data.staticAttrs) {
11-
return (
12-
render(node.data.staticAttrs) +
13-
render(node.data.attrs)
14-
)
9+
export default function renderAttrs (node: VNodeWithData): string {
10+
let res = ''
11+
if (node.data.staticAttrs) {
12+
res += render(node.data.staticAttrs)
13+
}
14+
if (node.data.attrs) {
15+
res += render(node.data.attrs)
1516
}
17+
return res
1618
}
1719

18-
function render (attrs: ?{ [key: string]: any }): string {
20+
function render (attrs: { [key: string]: any }): string {
1921
let res = ''
20-
if (!attrs) {
21-
return res
22-
}
2322
for (const key in attrs) {
2423
if (key === 'style') {
2524
// leave it to the style module

src/server/render.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export function createRenderFunction (
1818
isRoot: boolean
1919
) {
2020
if (node.componentOptions) {
21-
const child = createComponentInstanceForVnode(node)
22-
renderNode(child._render(), write, next, isRoot)
21+
const child = createComponentInstanceForVnode(node)._render()
22+
child.parent = node
23+
renderNode(child, write, next, isRoot)
2324
} else {
2425
if (node.tag) {
2526
renderElement(node, write, next, isRoot)
@@ -91,6 +92,14 @@ export function createRenderFunction (
9192
}
9293
}
9394
}
95+
// attach scoped CSS ID
96+
while (node) {
97+
const scopeId = node.context.$options._scopeId
98+
if (scopeId) {
99+
markup += ` ${scopeId}`
100+
}
101+
node = node.parent
102+
}
94103
return markup + '>'
95104
}
96105

test/ssr/ssr-string.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,31 @@ describe('SSR: renderToString', () => {
414414
})
415415
})
416416

417+
it('_scopeId', done => {
418+
renderVmWithOptions({
419+
_scopeId: '_v-parent',
420+
template: '<div id="foo"><p><child></child></p></div>',
421+
components: {
422+
child: {
423+
_scopeId: '_v-child',
424+
render () {
425+
const h = this.$createElement
426+
return h('div', null, [h('span', null, ['foo'])])
427+
}
428+
}
429+
}
430+
}, result => {
431+
expect(result).toContain(
432+
'<div id="foo" server-rendered="true" _v-parent>' +
433+
'<p _v-parent>' +
434+
'<div _v-child _v-parent><span _v-child>foo</span></div>' +
435+
'</p>' +
436+
'</div>'
437+
)
438+
done()
439+
})
440+
})
441+
417442
it('should catch error', done => {
418443
renderToString(new Vue({
419444
render () {

0 commit comments

Comments
 (0)