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(keep-alive): cache what is really needed not the whole VNode… #9962

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 38 additions & 13 deletions src/core/components/keep-alive.js
Expand Up @@ -3,7 +3,13 @@
import { isRegExp, remove } from 'shared/util'
import { getFirstComponentChild } from 'core/vdom/helpers/index'

type VNodeCache = { [key: string]: ?VNode };
type CacheEntry = {
name: ?string;
tag: ?string;
componentInstance: Component;
};

type CacheEntryMap = { [key: string]: ?CacheEntry };

function getComponentName (opts: ?VNodeComponentOptions): ?string {
return opts && (opts.Ctor.options.name || opts.tag)
Expand All @@ -24,9 +30,9 @@ function matches (pattern: string | RegExp | Array<string>, name: string): boole
function pruneCache (keepAliveInstance: any, filter: Function) {
const { cache, keys, _vnode } = keepAliveInstance
for (const key in cache) {
const cachedNode: ?VNode = cache[key]
if (cachedNode) {
const name: ?string = getComponentName(cachedNode.componentOptions)
const entry: ?CacheEntry = cache[key]
if (entry) {
const name: ?string = entry.name
if (name && !filter(name)) {
pruneCacheEntry(cache, key, keys, _vnode)
}
Expand All @@ -35,14 +41,14 @@ function pruneCache (keepAliveInstance: any, filter: Function) {
}

function pruneCacheEntry (
cache: VNodeCache,
cache: CacheEntryMap,
key: string,
keys: Array<string>,
current?: VNode
) {
const cached = cache[key]
if (cached && (!current || cached.tag !== current.tag)) {
cached.componentInstance.$destroy()
const entry: ?CacheEntry = cache[key]
if (entry && (!current || entry.tag !== current.tag)) {
entry.componentInstance.$destroy()
}
cache[key] = null
remove(keys, key)
Expand Down Expand Up @@ -72,6 +78,10 @@ export default {
},

mounted () {
if (this.putEntry) {
this.putEntry()
this.putEntry = null
}
this.$watch('include', val => {
pruneCache(this, name => matches(val, name))
})
Expand All @@ -80,6 +90,13 @@ export default {
})
},

updated () {
if (this.putEntry) {
this.putEntry()
this.putEntry = null
}
},

render () {
const slot = this.$slots.default
const vnode: VNode = getFirstComponentChild(slot)
Expand Down Expand Up @@ -109,11 +126,19 @@ export default {
remove(keys, key)
keys.push(key)
} else {
cache[key] = vnode
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
// put entry until component is instantiated
this.putEntry = () => {
const { tag, componentInstance } = vnode
cache[key] = {
name,
tag,
componentInstance
}
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
}
}
}

Expand Down