Skip to content

Commit

Permalink
fix(app): prevent template from being cached between apps with differ…
Browse files Browse the repository at this point in the history
…ent options (#9724)

close #9618
  • Loading branch information
pikax authored Dec 4, 2023
1 parent f2f5f76 commit ec71585
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ import { initDev } from './dev'
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom'
import { isString, NOOP, generateCodeFrame, extend } from '@vue/shared'
import {
isString,
NOOP,
generateCodeFrame,
extend,
EMPTY_OBJ
} from '@vue/shared'
import { InternalRenderFunction } from 'packages/runtime-core/src/component'

if (__DEV__) {
initDev()
}

const compileCache: Record<string, RenderFunction> = Object.create(null)
const compileCache = new WeakMap<
CompilerOptions,
Record<string, RenderFunction>
>()

function getCache(options?: CompilerOptions) {
let c = compileCache.get(options ?? EMPTY_OBJ)
if (!c) {
c = Object.create(null) as Record<string, RenderFunction>
compileCache.set(options ?? EMPTY_OBJ, c)
}
return c
}

function compileToFunction(
template: string | HTMLElement,
Expand All @@ -27,7 +45,8 @@ function compileToFunction(
}

const key = template
const cached = compileCache[key]
const cache = getCache(options)
const cached = cache[key]
if (cached) {
return cached
}
Expand Down Expand Up @@ -84,7 +103,7 @@ function compileToFunction(
// mark the function as runtime compiled
;(render as InternalRenderFunction)._rc = true

return (compileCache[key] = render)
return (cache[key] = render)
}

registerRuntimeCompiler(compileToFunction)
Expand Down

0 comments on commit ec71585

Please sign in to comment.