fix(ssr): don't render v-if comments in TransitionGroup (#6732)

close #6715
This commit is contained in:
Jonas 2024-04-15 15:26:19 +02:00 committed by GitHub
parent 2ec06fd6c8
commit 5a9626708e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 5 deletions

View File

@ -82,8 +82,6 @@ describe('transition-group', () => {
}) })
if (_ctx.ok) { if (_ctx.ok) {
_push(\`<div>ok</div>\`) _push(\`<div>ok</div>\`)
} else {
_push(\`<!---->\`)
} }
_push(\`<!--]-->\`) _push(\`<!--]-->\`)
}" }"

View File

@ -141,6 +141,7 @@ export function processChildren(
context: SSRTransformContext, context: SSRTransformContext,
asFragment = false, asFragment = false,
disableNestedFragments = false, disableNestedFragments = false,
disableCommentAsIfAlternate = false,
) { ) {
if (asFragment) { if (asFragment) {
context.pushStringPart(`<!--[-->`) context.pushStringPart(`<!--[-->`)
@ -191,7 +192,12 @@ export function processChildren(
) )
break break
case NodeTypes.IF: case NodeTypes.IF:
ssrProcessIf(child, context, disableNestedFragments) ssrProcessIf(
child,
context,
disableNestedFragments,
disableCommentAsIfAlternate,
)
break break
case NodeTypes.FOR: case NodeTypes.FOR:
ssrProcessFor(child, context, disableNestedFragments) ssrProcessFor(child, context, disableNestedFragments)

View File

@ -87,6 +87,13 @@ export function ssrProcessTransitionGroup(
* by disabling nested fragment wrappers from being generated. * by disabling nested fragment wrappers from being generated.
*/ */
true, true,
/**
* TransitionGroup filters out comment children at runtime and thus
* doesn't expect comments to be present during hydration. We need to
* account for that by disabling the empty comment that is otherwise
* rendered for a falsy v-if that has no v-else specified. (#6715)
*/
true,
) )
context.pushStringPart(`</`) context.pushStringPart(`</`)
context.pushStringPart(tag.exp!) context.pushStringPart(tag.exp!)
@ -106,6 +113,6 @@ export function ssrProcessTransitionGroup(
} }
} else { } else {
// fragment // fragment
processChildren(node, context, true, true) processChildren(node, context, true, true, true)
} }
} }

View File

@ -26,6 +26,7 @@ export function ssrProcessIf(
node: IfNode, node: IfNode,
context: SSRTransformContext, context: SSRTransformContext,
disableNestedFragments = false, disableNestedFragments = false,
disableCommentAsIfAlternate = false,
) { ) {
const [rootBranch] = node.branches const [rootBranch] = node.branches
const ifStatement = createIfStatement( const ifStatement = createIfStatement(
@ -54,7 +55,7 @@ export function ssrProcessIf(
} }
} }
if (!currentIf.alternate) { if (!currentIf.alternate && !disableCommentAsIfAlternate) {
currentIf.alternate = createBlockStatement([ currentIf.alternate = createBlockStatement([
createCallExpression(`_push`, ['`<!---->`']), createCallExpression(`_push`, ['`<!---->`']),
]) ])