Skip to content

Commit 79602f9

Browse files
committed
fix(ssr): respect textContent/innerHTML from getSSRProps in optimized SSR output
close #8112
1 parent 7d473b7 commit 79602f9

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

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

+40-17
Original file line numberDiff line numberDiff line change
@@ -288,55 +288,78 @@ describe('ssr: element', () => {
288288
}></div>\`"
289289
`)
290290
})
291+
})
291292

292-
test('custom dir', () => {
293+
describe('custom directives', () => {
294+
// #8112 should respect textContent / innerHTML from directive getSSRProps
295+
// if the element has no children
296+
test('custom dir without children', () => {
293297
expect(getCompiledString(`<div v-xxx:x.y="z" />`)).toMatchInlineSnapshot(`
298+
"\`<div\${
299+
_ssrRenderAttrs(_temp0 = _ssrGetDirectiveProps(_ctx, _directive_xxx, _ctx.z, "x", { y: true }))
300+
}>\${
301+
("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? ''
302+
}</div>\`"
303+
`)
304+
})
305+
306+
test('custom dir with children', () => {
307+
expect(getCompiledString(`<div v-xxx:x.y="z">hello</div>`))
308+
.toMatchInlineSnapshot(`
294309
"\`<div\${
295310
_ssrRenderAttrs(_ssrGetDirectiveProps(_ctx, _directive_xxx, _ctx.z, "x", { y: true }))
296-
}></div>\`"
311+
}>hello</div>\`"
297312
`)
298313
})
299314

300315
test('custom dir with normal attrs', () => {
301316
expect(getCompiledString(`<div class="foo" v-xxx />`))
302317
.toMatchInlineSnapshot(`
303318
"\`<div\${
304-
_ssrRenderAttrs(_mergeProps({ class: "foo" }, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
305-
}></div>\`"
319+
_ssrRenderAttrs(_temp0 = _mergeProps({ class: "foo" }, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
320+
}>\${
321+
("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? ''
322+
}</div>\`"
306323
`)
307324
})
308325

309326
test('custom dir with v-bind', () => {
310327
expect(getCompiledString(`<div :title="foo" :class="bar" v-xxx />`))
311328
.toMatchInlineSnapshot(`
312-
"\`<div\${
313-
_ssrRenderAttrs(_mergeProps({
314-
title: _ctx.foo,
315-
class: _ctx.bar
316-
}, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
317-
}></div>\`"
318-
`)
329+
"\`<div\${
330+
_ssrRenderAttrs(_temp0 = _mergeProps({
331+
title: _ctx.foo,
332+
class: _ctx.bar
333+
}, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
334+
}>\${
335+
("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? ''
336+
}</div>\`"
337+
`)
319338
})
320339

321340
test('custom dir with object v-bind', () => {
322341
expect(getCompiledString(`<div v-bind="x" v-xxx />`))
323342
.toMatchInlineSnapshot(`
324-
"\`<div\${
325-
_ssrRenderAttrs(_mergeProps(_ctx.x, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
326-
}></div>\`"
327-
`)
343+
"\`<div\${
344+
_ssrRenderAttrs(_temp0 = _mergeProps(_ctx.x, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
345+
}>\${
346+
("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? ''
347+
}</div>\`"
348+
`)
328349
})
329350

330351
test('custom dir with object v-bind + normal bindings', () => {
331352
expect(
332353
getCompiledString(`<div v-bind="x" class="foo" v-xxx title="bar" />`),
333354
).toMatchInlineSnapshot(`
334355
"\`<div\${
335-
_ssrRenderAttrs(_mergeProps(_ctx.x, {
356+
_ssrRenderAttrs(_temp0 = _mergeProps(_ctx.x, {
336357
class: "foo",
337358
title: "bar"
338359
}, _ssrGetDirectiveProps(_ctx, _directive_xxx)))
339-
}></div>\`"
360+
}>\${
361+
("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? ''
362+
}</div>\`"
340363
`)
341364
})
342365
})

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

+19
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
163163
]),
164164
]
165165
}
166+
} else if (directives.length && !node.children.length) {
167+
const tempId = `_temp${context.temps++}`
168+
propsExp.arguments = [
169+
createAssignmentExpression(
170+
createSimpleExpression(tempId, false),
171+
mergedProps,
172+
),
173+
]
174+
rawChildrenMap.set(
175+
node,
176+
createConditionalExpression(
177+
createSimpleExpression(`"textContent" in ${tempId}`, false),
178+
createCallExpression(context.helper(SSR_INTERPOLATE), [
179+
createSimpleExpression(`${tempId}.textContent`, false),
180+
]),
181+
createSimpleExpression(`${tempId}.innerHTML ?? ''`, false),
182+
false,
183+
),
184+
)
166185
}
167186

168187
if (needTagForRuntime) {

0 commit comments

Comments
 (0)