Skip to content

Commit 12255ff

Browse files
committed
refactor: simplify functional renderStatic
1 parent ea3a70b commit 12255ff

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

src/core/instance/render-helpers/render-static.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ export function renderStatic (
1010
isInFor?: boolean
1111
): VNode | Array<VNode> {
1212
// static trees can be rendered once and cached on the contructor options
13-
// so every instance shares the same trees
14-
let options = this.constructor.options
15-
if (this.$options.staticRenderFns !== options.staticRenderFns) {
16-
options = this.$options
17-
}
18-
const trees = options._staticTrees || (options._staticTrees = [])
19-
let tree = trees[index]
13+
// so every instance shares the same cached trees
14+
const renderFns = this.$options.staticRenderFns
15+
const cached = renderFns.cached || (renderFns.cached = [])
16+
let tree = cached[index]
2017
// if has already-rendered static tree and not inside v-for,
2118
// we can reuse the same tree by doing a shallow clone.
2219
if (tree && !isInFor) {
@@ -25,8 +22,7 @@ export function renderStatic (
2522
: cloneVNode(tree)
2623
}
2724
// otherwise, render a fresh tree.
28-
tree = trees[index] =
29-
options.staticRenderFns[index].call(this._renderProxy, null, this)
25+
tree = cached[index] = renderFns[index].call(this._renderProxy, null, this)
3026
markStatic(tree, `__static__${index}`, false)
3127
return tree
3228
}

src/core/instance/render.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import { isUpdatingChildComponent } from './lifecycle'
1717

1818
export function initRender (vm: Component) {
1919
vm._vnode = null // the root of the child tree
20-
const parentVnode = vm.$vnode = vm.$options._parentVnode // the placeholder node in parent tree
20+
const options = vm.$options
21+
const parentVnode = vm.$vnode = options._parentVnode // the placeholder node in parent tree
2122
const renderContext = parentVnode && parentVnode.context
22-
vm.$slots = resolveSlots(vm.$options._renderChildren, renderContext)
23+
vm.$slots = resolveSlots(options._renderChildren, renderContext)
2324
vm.$scopedSlots = emptyObject
2425
// bind the createElement fn to this instance
2526
// so that we get proper render context inside it.
@@ -39,12 +40,12 @@ export function initRender (vm: Component) {
3940
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, () => {
4041
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
4142
}, true)
42-
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, () => {
43+
defineReactive(vm, '$listeners', options._parentListeners || emptyObject, () => {
4344
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
4445
}, true)
4546
} else {
4647
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, null, true)
47-
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, null, true)
48+
defineReactive(vm, '$listeners', options._parentListeners || emptyObject, null, true)
4849
}
4950
}
5051

src/core/vdom/create-functional-component.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ function FunctionalRenderContext (
3838

3939
// support for compiled functional template
4040
if (isCompiled) {
41-
// exposing constructor and $options for renderStatic() because it needs
42-
// to cache the rendered trees on shared options
43-
this.constructor = Ctor
41+
// exposing $options for renderStatic()
4442
this.$options = options
4543
// pre-resolve slots for renderSlot()
4644
this.$slots = this.slots()

0 commit comments

Comments
 (0)