Skip to content

Commit c65b805

Browse files
committed
fix(compiler-ssr): only inject fallthrough attrs for root transition/keep-alive
1 parent c03459b commit c65b805

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

packages/compiler-ssr/__tests__/ssrComponent.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,25 @@ describe('ssr: components', () => {
290290
}"
291291
`)
292292

293+
// should inject attrs if root with coomments
294+
expect(compile(`<!--root--><transition><div/></transition>`).code)
295+
.toMatchInlineSnapshot(`
296+
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
297+
298+
return function ssrRender(_ctx, _push, _parent, _attrs) {
299+
_push(\`<!--[--><!--root--><div\${_ssrRenderAttrs(_attrs)}></div><!--]-->\`)
300+
}"
301+
`)
302+
303+
// should not inject attrs if not root
304+
expect(compile(`<div/><transition><div/></transition>`).code)
305+
.toMatchInlineSnapshot(`
306+
"
307+
return function ssrRender(_ctx, _push, _parent, _attrs) {
308+
_push(\`<!--[--><div></div><div></div><!--]-->\`)
309+
}"
310+
`)
311+
293312
expect(compile(`<keep-alive><foo/></keep-alive>`).code)
294313
.toMatchInlineSnapshot(`
295314
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")

packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import {
1111
isBuiltInType
1212
} from '@vue/compiler-dom'
1313

14+
const filterChild = (node: ParentNode) =>
15+
node.children.filter(n => n.type !== NodeTypes.COMMENT)
16+
1417
const hasSingleChild = (node: ParentNode): boolean =>
15-
node.children.filter(n => n.type !== NodeTypes.COMMENT).length === 1
18+
filterChild(node).length === 1
1619

1720
export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
1821
// _attrs is provided as a function argument.
@@ -28,10 +31,13 @@ export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
2831
(isBuiltInType(node.tag, 'Transition') ||
2932
isBuiltInType(node.tag, 'KeepAlive'))
3033
) {
31-
if (hasSingleChild(node)) {
32-
injectFallthroughAttrs(node.children[0])
34+
const rootChildren = filterChild(context.root)
35+
if (rootChildren.length === 1 && rootChildren[0] === node) {
36+
if (hasSingleChild(node)) {
37+
injectFallthroughAttrs(node.children[0])
38+
}
39+
return
3340
}
34-
return
3541
}
3642

3743
const parent = context.parent

0 commit comments

Comments
 (0)