From cd6ab519d0ed76872748e1f4342cd5fe8968ec90 Mon Sep 17 00:00:00 2001 From: Bulat Aikaev Date: Fri, 15 Nov 2024 09:02:50 -0500 Subject: [PATCH] fix(ssr): ensure v-text updates correctly with custom directives in SSR output (#12311) close #12309 --- .../compiler-ssr/__tests__/ssrElement.spec.ts | 33 +++++++++++++++ .../src/transforms/ssrTransformElement.ts | 41 +++++++++++-------- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/packages/compiler-ssr/__tests__/ssrElement.spec.ts b/packages/compiler-ssr/__tests__/ssrElement.spec.ts index 723ef7b3592..f1d509acfb0 100644 --- a/packages/compiler-ssr/__tests__/ssrElement.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrElement.spec.ts @@ -337,6 +337,39 @@ describe('ssr: element', () => { `) }) + test('custom dir with v-text', () => { + expect(getCompiledString(`
`)) + .toMatchInlineSnapshot(` + "\`\${ + _ssrInterpolate(_ctx.foo) + }
\`" + `) + }) + + test('custom dir with v-text and normal attrs', () => { + expect(getCompiledString(`
`)) + .toMatchInlineSnapshot(` + "\`\${ + _ssrInterpolate(_ctx.foo) + }
\`" + `) + }) + + test('mulptiple custom dirs with v-text', () => { + expect(getCompiledString(`
`)) + .toMatchInlineSnapshot(` + "\`\${ + _ssrInterpolate(_ctx.foo) + }
\`" + `) + }) + test('custom dir with object v-bind', () => { expect(getCompiledString(`
`)) .toMatchInlineSnapshot(` diff --git a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts index 6a028953ed6..4a12b0f7ba7 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts @@ -28,6 +28,7 @@ import { createSequenceExpression, createSimpleExpression, createTemplateLiteral, + findDir, hasDynamicKeyVBind, isStaticArgOf, isStaticExp, @@ -164,24 +165,28 @@ export const ssrTransformElement: NodeTransform = (node, context) => { ] } } else if (directives.length && !node.children.length) { - const tempId = `_temp${context.temps++}` - propsExp.arguments = [ - createAssignmentExpression( - createSimpleExpression(tempId, false), - mergedProps, - ), - ] - rawChildrenMap.set( - node, - createConditionalExpression( - createSimpleExpression(`"textContent" in ${tempId}`, false), - createCallExpression(context.helper(SSR_INTERPOLATE), [ - createSimpleExpression(`${tempId}.textContent`, false), - ]), - createSimpleExpression(`${tempId}.innerHTML ?? ''`, false), - false, - ), - ) + // v-text directive has higher priority than the merged props + const vText = findDir(node, 'text') + if (!vText) { + const tempId = `_temp${context.temps++}` + propsExp.arguments = [ + createAssignmentExpression( + createSimpleExpression(tempId, false), + mergedProps, + ), + ] + rawChildrenMap.set( + node, + createConditionalExpression( + createSimpleExpression(`"textContent" in ${tempId}`, false), + createCallExpression(context.helper(SSR_INTERPOLATE), [ + createSimpleExpression(`${tempId}.textContent`, false), + ]), + createSimpleExpression(`${tempId}.innerHTML ?? ''`, false), + false, + ), + ) + } } if (needTagForRuntime) {