Skip to content

Commit 95c1975

Browse files
fix(compiler-dom): nodes with v-once shouldn't be stringified (#13878)
1 parent 4b71706 commit 95c1975

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ return function render(_ctx, _cache) {
1212
}"
1313
`;
1414

15+
exports[`stringify static html > eligible content + v-once node 1`] = `
16+
"const { setBlockTracking: _setBlockTracking, toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
17+
18+
return function render(_ctx, _cache) {
19+
return (_openBlock(), _createElementBlock("div", null, [
20+
_cache[0] || (
21+
_setBlockTracking(-1, true),
22+
(_cache[0] = _createElementVNode("div", null, [
23+
_createTextVNode(_toDisplayString(_ctx.msg), 1 /* TEXT */)
24+
])).cacheIndex = 0,
25+
_setBlockTracking(1),
26+
_cache[0]
27+
),
28+
_cache[1] || (_cache[1] = _createStaticVNode("<span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span>", 5))
29+
]))
30+
}"
31+
`;
32+
1533
exports[`stringify static html > escape 1`] = `
1634
"const { toDisplayString: _toDisplayString, normalizeClass: _normalizeClass, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
1735

packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,14 @@ describe('stringify static html', () => {
525525

526526
expect(code).toMatchSnapshot()
527527
})
528+
529+
test('eligible content + v-once node', () => {
530+
const { code } = compileWithStringify(
531+
`<div>
532+
<div v-once>{{ msg }}</div>
533+
${repeat(`<span class="foo">foo</span>`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT)}
534+
</div>`,
535+
)
536+
expect(code).toMatchSnapshot()
537+
})
528538
})

packages/compiler-dom/src/transforms/stringifyStatic.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type TextCallNode,
1818
type TransformContext,
1919
createCallExpression,
20+
findDir,
2021
isStaticArgOf,
2122
} from '@vue/compiler-core'
2223
import {
@@ -213,6 +214,11 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
213214
return false
214215
}
215216

217+
// v-once nodes should not be stringified
218+
if (node.type === NodeTypes.ELEMENT && findDir(node, 'once', true)) {
219+
return false
220+
}
221+
216222
if (node.type === NodeTypes.TEXT_CALL) {
217223
return [1, 0]
218224
}

0 commit comments

Comments
 (0)