Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): prevent template from being cached between apps with different options #9724

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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