Skip to content

Commit 6bf9772

Browse files
committed
feat(ssr): inheritAttrs support in SSR
1 parent 1bf98b0 commit 6bf9772

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ export default function renderAttrs (node: VNodeWithData): string {
1717
let attrs = node.data.attrs
1818
let res = ''
1919

20-
let parent = node.parent
21-
while (isDef(parent)) {
22-
if (isDef(parent.data) && isDef(parent.data.attrs)) {
23-
attrs = Object.assign({}, attrs, parent.data.attrs)
20+
const opts = node.parent && node.parent.componentOptions
21+
if (isUndef(opts) || opts.Ctor.options.inheritAttrs !== false) {
22+
let parent = node.parent
23+
while (isDef(parent)) {
24+
if (isDef(parent.data) && isDef(parent.data.attrs)) {
25+
attrs = Object.assign({}, attrs, parent.data.attrs)
26+
}
27+
parent = parent.parent
2428
}
25-
parent = parent.parent
2629
}
2730

2831
if (isUndef(attrs)) {

test/ssr/ssr-string.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,21 @@ describe('SSR: renderToString', () => {
880880
done()
881881
})
882882
})
883+
884+
it('with inheritAttrs: false + $attrs', done => {
885+
renderVmWithOptions({
886+
template: `<foo id="a"/>`,
887+
components: {
888+
foo: {
889+
inheritAttrs: false,
890+
template: `<div><div v-bind="$attrs"></div></div>`
891+
}
892+
}
893+
}, res => {
894+
expect(res).toBe(`<div data-server-rendered="true"><div id="a"></div></div>`)
895+
done()
896+
})
897+
})
883898
})
884899

885900
function renderVmWithOptions (options, cb) {

0 commit comments

Comments
 (0)