Skip to content

Commit 3b04547

Browse files
committed
refactor render
1 parent 4e40666 commit 3b04547

File tree

1 file changed

+89
-74
lines changed

1 file changed

+89
-74
lines changed

src/server/render.js

Lines changed: 89 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -37,89 +37,88 @@ const normalizeRender = vm => {
3737
}
3838

3939
function renderNode (node, isRoot, context) {
40-
const { write, next, userContext } = context
4140
if (isDef(node.componentOptions)) {
42-
// check cache hit
43-
const Ctor = node.componentOptions.Ctor
44-
const getKey = Ctor.options.serverCacheKey
45-
const name = Ctor.options.name
46-
47-
// exposed by vue-loader, need to call this if cache hit because
48-
// component lifecycle hooks will not be called.
49-
const registerComponent = Ctor.options._ssrRegister
50-
if (write.caching && isDef(registerComponent)) {
51-
write.componentBuffer[write.componentBuffer.length - 1].add(registerComponent)
52-
}
53-
54-
const cache = context.cache
55-
if (isDef(getKey) && isDef(cache) && isDef(name)) {
56-
const key = name + '::' + getKey(node.componentOptions.propsData)
57-
const { has, get } = context
58-
if (isDef(has)) {
59-
(has: any)(key, hit => {
60-
if (hit === true && isDef(get)) {
61-
(get: any)(key, res => {
62-
registerComponent && registerComponent(userContext)
63-
res.components.forEach(register => register(userContext))
64-
write(res.html, next)
65-
})
66-
} else {
67-
renderComponentWithCache(node, isRoot, key, context)
68-
}
69-
})
70-
} else if (isDef(get)) {
71-
(get: any)(key, res => {
72-
if (isDef(res)) {
73-
registerComponent && registerComponent(userContext)
74-
res.components.forEach(register => register(userContext))
75-
write(res.html, next)
76-
} else {
77-
renderComponentWithCache(node, isRoot, key, context)
78-
}
79-
})
80-
}
81-
} else {
82-
if (isDef(getKey) && isUndef(cache)) {
83-
warnOnce(
84-
`[vue-server-renderer] Component ${
85-
Ctor.options.name || '(anonymous)'
86-
} implemented serverCacheKey, ` +
87-
'but no cache was provided to the renderer.'
88-
)
89-
}
90-
if (isDef(getKey) && isUndef(name)) {
91-
warnOnce(
92-
`[vue-server-renderer] Components that implement "serverCacheKey" ` +
93-
`must also define a unique "name" option.`
94-
)
95-
}
96-
renderComponent(node, isRoot, context)
97-
}
41+
renderComponent(node, isRoot, context)
9842
} else {
9943
if (isDef(node.tag)) {
10044
renderElement(node, isRoot, context)
10145
} else if (isTrue(node.isComment)) {
102-
write(`<!--${node.text}-->`, next)
46+
context.write(
47+
`<!--${node.text}-->`,
48+
context.next
49+
)
10350
} else {
104-
write(node.raw ? node.text : escape(String(node.text)), next)
51+
context.write(
52+
node.raw ? node.text : escape(String(node.text)),
53+
context.next
54+
)
10555
}
10656
}
10757
}
10858

10959
function renderComponent (node, isRoot, context) {
110-
const prevActive = context.activeInstance
111-
const child = context.activeInstance = createComponentInstanceForVnode(
112-
node,
113-
context.activeInstance
114-
)
115-
normalizeRender(child)
116-
const childNode = child._render()
117-
childNode.parent = node
118-
context.renderStates.push({
119-
type: 'Component',
120-
prevActive
121-
})
122-
renderNode(childNode, isRoot, context)
60+
const { write, next, userContext } = context
61+
62+
// check cache hit
63+
const Ctor = node.componentOptions.Ctor
64+
const getKey = Ctor.options.serverCacheKey
65+
const name = Ctor.options.name
66+
67+
// exposed by vue-loader, need to call this if cache hit because
68+
// component lifecycle hooks will not be called.
69+
const registerComponent = Ctor.options._ssrRegister
70+
if (write.caching && isDef(registerComponent)) {
71+
write.componentBuffer[write.componentBuffer.length - 1].add(registerComponent)
72+
}
73+
74+
const cache = context.cache
75+
if (isDef(getKey) && isDef(cache) && isDef(name)) {
76+
const key = name + '::' + getKey(node.componentOptions.propsData)
77+
const { has, get } = context
78+
if (isDef(has)) {
79+
(has: any)(key, hit => {
80+
if (hit === true && isDef(get)) {
81+
(get: any)(key, res => {
82+
if (isDef(registerComponent)) {
83+
registerComponent(userContext)
84+
}
85+
res.components.forEach(register => register(userContext))
86+
write(res.html, next)
87+
})
88+
} else {
89+
renderComponentWithCache(node, isRoot, key, context)
90+
}
91+
})
92+
} else if (isDef(get)) {
93+
(get: any)(key, res => {
94+
if (isDef(res)) {
95+
if (isDef(registerComponent)) {
96+
registerComponent(userContext)
97+
}
98+
res.components.forEach(register => register(userContext))
99+
write(res.html, next)
100+
} else {
101+
renderComponentWithCache(node, isRoot, key, context)
102+
}
103+
})
104+
}
105+
} else {
106+
if (isDef(getKey) && isUndef(cache)) {
107+
warnOnce(
108+
`[vue-server-renderer] Component ${
109+
Ctor.options.name || '(anonymous)'
110+
} implemented serverCacheKey, ` +
111+
'but no cache was provided to the renderer.'
112+
)
113+
}
114+
if (isDef(getKey) && isUndef(name)) {
115+
warnOnce(
116+
`[vue-server-renderer] Components that implement "serverCacheKey" ` +
117+
`must also define a unique "name" option.`
118+
)
119+
}
120+
renderComponentInner(node, isRoot, context)
121+
}
123122
}
124123

125124
function renderComponentWithCache (node, isRoot, key, context) {
@@ -136,7 +135,23 @@ function renderComponentWithCache (node, isRoot, key, context) {
136135
bufferIndex,
137136
componentBuffer
138137
})
139-
renderComponent(node, isRoot, context)
138+
renderComponentInner(node, isRoot, context)
139+
}
140+
141+
function renderComponentInner (node, isRoot, context) {
142+
const prevActive = context.activeInstance
143+
const child = context.activeInstance = createComponentInstanceForVnode(
144+
node,
145+
context.activeInstance
146+
)
147+
normalizeRender(child)
148+
const childNode = child._render()
149+
childNode.parent = node
150+
context.renderStates.push({
151+
type: 'Component',
152+
prevActive
153+
})
154+
renderNode(childNode, isRoot, context)
140155
}
141156

142157
function renderElement (el, isRoot, context) {
@@ -166,7 +181,7 @@ function renderElement (el, isRoot, context) {
166181

167182
function hasAncestorData (node: VNode) {
168183
const parentNode = node.parent
169-
return parentNode && (parentNode.data || hasAncestorData(parentNode))
184+
return isDef(parentNode) && (isDef(parentNode.data) || hasAncestorData(parentNode))
170185
}
171186

172187
function getVShowDirectiveInfo (node: VNode): ?VNodeDirective {

0 commit comments

Comments
 (0)