Skip to content

Commit 1d99d61

Browse files
committed
fix(compiler-dom): fix stringify static edge for partially eligible chunks in cached parent
close #11879 close #11890
1 parent 7571f20 commit 1d99d61

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`stringify static html > eligible content (elements > 20) + non-eligible content 1`] = `
4+
"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
5+
6+
return function render(_ctx, _cache) {
7+
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
8+
_createStaticVNode("<span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span>", 20),
9+
_createElementVNode("div", { key: "1" }, "1", -1 /* HOISTED */),
10+
_createStaticVNode("<span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span>", 20)
11+
])))
12+
}"
13+
`;
14+
315
exports[`stringify static html > escape 1`] = `
416
"const { toDisplayString: _toDisplayString, normalizeClass: _normalizeClass, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
517

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,18 @@ describe('stringify static html', () => {
451451
expect(ast.cached).toMatchObject([cachedArrayBailedMatcher()])
452452
expect(code).toMatchSnapshot()
453453
})
454+
455+
test('eligible content (elements > 20) + non-eligible content', () => {
456+
const { code } = compileWithStringify(
457+
`<div>${repeat(
458+
`<span/>`,
459+
StringifyThresholds.NODE_COUNT,
460+
)}<div key="1">1</div>${repeat(
461+
`<span/>`,
462+
StringifyThresholds.NODE_COUNT,
463+
)}</div>`,
464+
)
465+
466+
expect(code).toMatchSnapshot()
467+
})
454468
})

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import {
1616
type TemplateChildNode,
1717
type TextCallNode,
1818
type TransformContext,
19-
type VNodeCall,
20-
createArrayExpression,
2119
createCallExpression,
2220
isStaticArgOf,
2321
} from '@vue/compiler-core'
@@ -106,15 +104,23 @@ export const stringifyStatic: HoistTransform = (children, context, parent) => {
106104
String(currentChunk.length),
107105
])
108106

107+
const deleteCount = currentChunk.length - 1
108+
109109
if (isParentCached) {
110-
;((parent.codegenNode as VNodeCall).children as CacheExpression).value =
111-
createArrayExpression([staticCall])
110+
// if the parent is cached, then `children` is also the value of the
111+
// CacheExpression. Just replace the corresponding range in the cached
112+
// list with staticCall.
113+
children.splice(
114+
currentIndex - currentChunk.length,
115+
currentChunk.length,
116+
// @ts-expect-error
117+
staticCall,
118+
)
112119
} else {
113120
// replace the first node's hoisted expression with the static vnode call
114121
;(currentChunk[0].codegenNode as CacheExpression).value = staticCall
115122
if (currentChunk.length > 1) {
116123
// remove merged nodes from children
117-
const deleteCount = currentChunk.length - 1
118124
children.splice(currentIndex - currentChunk.length + 1, deleteCount)
119125
// also adjust index for the remaining cache items
120126
const cacheIndex = context.cached.indexOf(
@@ -128,9 +134,9 @@ export const stringifyStatic: HoistTransform = (children, context, parent) => {
128134
}
129135
context.cached.splice(cacheIndex - deleteCount + 1, deleteCount)
130136
}
131-
return deleteCount
132137
}
133138
}
139+
return deleteCount
134140
}
135141
return 0
136142
}

0 commit comments

Comments
 (0)