Skip to content

Commit ec71585

Browse files
authored
fix(app): prevent template from being cached between apps with different options (#9724)
close #9618
1 parent f2f5f76 commit ec71585

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/vue/src/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@ import { initDev } from './dev'
44
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
55
import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
66
import * as runtimeDom from '@vue/runtime-dom'
7-
import { isString, NOOP, generateCodeFrame, extend } from '@vue/shared'
7+
import {
8+
isString,
9+
NOOP,
10+
generateCodeFrame,
11+
extend,
12+
EMPTY_OBJ
13+
} from '@vue/shared'
814
import { InternalRenderFunction } from 'packages/runtime-core/src/component'
915

1016
if (__DEV__) {
1117
initDev()
1218
}
1319

14-
const compileCache: Record<string, RenderFunction> = Object.create(null)
20+
const compileCache = new WeakMap<
21+
CompilerOptions,
22+
Record<string, RenderFunction>
23+
>()
24+
25+
function getCache(options?: CompilerOptions) {
26+
let c = compileCache.get(options ?? EMPTY_OBJ)
27+
if (!c) {
28+
c = Object.create(null) as Record<string, RenderFunction>
29+
compileCache.set(options ?? EMPTY_OBJ, c)
30+
}
31+
return c
32+
}
1533

1634
function compileToFunction(
1735
template: string | HTMLElement,
@@ -27,7 +45,8 @@ function compileToFunction(
2745
}
2846

2947
const key = template
30-
const cached = compileCache[key]
48+
const cache = getCache(options)
49+
const cached = cache[key]
3150
if (cached) {
3251
return cached
3352
}
@@ -84,7 +103,7 @@ function compileToFunction(
84103
// mark the function as runtime compiled
85104
;(render as InternalRenderFunction)._rc = true
86105

87-
return (compileCache[key] = render)
106+
return (cache[key] = render)
88107
}
89108

90109
registerRuntimeCompiler(compileToFunction)

0 commit comments

Comments
 (0)