Skip to content

Commit 5213ec7

Browse files
committed
helperify ssr
1 parent 841fe60 commit 5213ec7

File tree

10 files changed

+64
-51
lines changed

10 files changed

+64
-51
lines changed

src/platforms/web/server/modules/attrs.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import { escape } from 'he'
44

5+
import {
6+
isDef,
7+
isUndef
8+
} from 'shared/util'
9+
510
import {
611
isBooleanAttr,
712
isEnumeratedAttr,
@@ -12,15 +17,15 @@ export default function renderAttrs (node: VNodeWithData): string {
1217
let attrs = node.data.attrs
1318
let res = ''
1419

15-
let parent = node.parent
16-
while (parent) {
17-
if (parent.data && parent.data.attrs) {
20+
let parent: any = node.parent
21+
while (isDef(parent)) {
22+
if (isDef(parent.data) && isDef(parent.data.attrs)) {
1823
attrs = Object.assign({}, attrs, parent.data.attrs)
1924
}
2025
parent = parent.parent
2126
}
2227

23-
if (!attrs) {
28+
if (isUndef(attrs)) {
2429
return res
2530
}
2631

src/platforms/web/server/modules/class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { genClassForVnode } from 'web/util/index'
55

66
export default function renderClass (node: VNodeWithData): ?string {
77
const classList = genClassForVnode(node)
8-
if (classList) {
8+
if (classList !== '') {
99
return ` class="${escape(classList)}"`
1010
}
1111
}

src/platforms/web/server/modules/dom-props.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
import VNode from 'core/vdom/vnode'
44
import { renderAttr } from './attrs'
5+
import { isDef, isUndef } from 'shared/util'
56
import { propsToAttrMap, isRenderableAttr } from '../util'
67

78
export default function renderDOMProps (node: VNodeWithData): string {
89
let props = node.data.domProps
910
let res = ''
1011

11-
let parent = node.parent
12-
while (parent) {
12+
let parent: any = node.parent
13+
while (isDef(parent)) {
1314
if (parent.data && parent.data.domProps) {
1415
props = Object.assign({}, props, parent.data.domProps)
1516
}
1617
parent = parent.parent
1718
}
1819

19-
if (!props) {
20+
if (isUndef(props)) {
2021
return res
2122
}
2223

23-
const attrs = node.data.attrs
24+
const attrs: any = node.data.attrs
2425
for (const key in props) {
2526
if (key === 'innerHTML') {
2627
setText(node, props[key], true)
@@ -30,7 +31,7 @@ export default function renderDOMProps (node: VNodeWithData): string {
3031
const attr = propsToAttrMap[key] || key.toLowerCase()
3132
if (isRenderableAttr(attr) &&
3233
// avoid rendering double-bound props/attrs twice
33-
!(attrs && attrs[attr])) {
34+
!(isDef(attrs) && isDef(attrs[attr]))) {
3435
res += renderAttr(attr, props[key])
3536
}
3637
}

src/platforms/web/server/modules/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function genStyleText (vnode: VNode): string {
1515

1616
export default function renderStyle (vnode: VNodeWithData): ?string {
1717
const styleText = genStyleText(vnode)
18-
if (styleText) {
18+
if (styleText !== '') {
1919
return ` style=${JSON.stringify(escape(styleText))}`
2020
}
2121
}

src/platforms/web/util/class.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* @flow */
22

3-
import { isObject } from 'shared/util'
3+
import { isDef, isUndef, isObject } from 'shared/util'
44

55
export function genClassForVnode (vnode: VNode): string {
66
let data = vnode.data
77
let parentNode = vnode
88
let childNode = vnode
9-
while (childNode.componentInstance) {
9+
while (isDef(childNode.componentInstance)) {
1010
childNode = childNode.componentInstance._vnode
1111
if (childNode.data) {
1212
data = mergeClassData(childNode.data, data)
1313
}
1414
}
15-
while ((parentNode = parentNode.parent)) {
15+
while (isDef(parentNode = parentNode.parent)) {
1616
if (parentNode.data) {
1717
data = mergeClassData(data, parentNode.data)
1818
}
@@ -26,7 +26,7 @@ function mergeClassData (child: VNodeData, parent: VNodeData): {
2626
} {
2727
return {
2828
staticClass: concat(child.staticClass, parent.staticClass),
29-
class: child.class
29+
class: isDef(child.class)
3030
? [child.class, parent.class]
3131
: parent.class
3232
}
@@ -35,7 +35,7 @@ function mergeClassData (child: VNodeData, parent: VNodeData): {
3535
function genClassFromData (data: Object): string {
3636
const dynamicClass = data.class
3737
const staticClass = data.staticClass
38-
if (staticClass || dynamicClass) {
38+
if (isDef(staticClass) || isDef(dynamicClass)) {
3939
return concat(staticClass, stringifyClass(dynamicClass))
4040
}
4141
/* istanbul ignore next */
@@ -47,18 +47,18 @@ export function concat (a: ?string, b: ?string): string {
4747
}
4848

4949
export function stringifyClass (value: any): string {
50-
let res = ''
51-
if (!value) {
52-
return res
50+
if (isUndef(value)) {
51+
return ''
5352
}
5453
if (typeof value === 'string') {
5554
return value
5655
}
56+
let res = ''
5757
if (Array.isArray(value)) {
5858
let stringified
5959
for (let i = 0, l = value.length; i < l; i++) {
60-
if (value[i]) {
61-
if ((stringified = stringifyClass(value[i]))) {
60+
if (isDef(value[i])) {
61+
if (isDef(stringified = stringifyClass(value[i])) && stringified !== '') {
6262
res += stringified + ' '
6363
}
6464
}

src/server/create-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function createRenderer ({
5959
let result = ''
6060
const write = createWriteFunction(text => {
6161
result += text
62+
return false
6263
}, done)
6364
try {
6465
render(component, write, () => {

src/server/render-context.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* @flow */
22

3+
import { isUndef } from 'shared/util'
4+
35
type RenderState = {
46
type: 'Element';
57
rendered: number;
@@ -57,7 +59,7 @@ export class RenderContext {
5759

5860
next () {
5961
const lastState = this.renderStates[this.renderStates.length - 1]
60-
if (!lastState) {
62+
if (isUndef(lastState)) {
6163
return this.done()
6264
}
6365
switch (lastState.type) {
@@ -99,7 +101,7 @@ export class RenderContext {
99101

100102
function normalizeAsync (cache, method) {
101103
const fn = cache[method]
102-
if (!fn) {
104+
if (isUndef(fn)) {
103105
return
104106
} else if (fn.length > 1) {
105107
return (key, cb) => fn.call(cache, key, cb)

src/server/render-stream.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const stream = require('stream')
1212

13+
import { isTrue, isUndef } from 'shared/util'
1314
import { createWriteFunction } from './write'
1415

1516
export default class RenderStream extends stream.Readable {
@@ -35,6 +36,7 @@ export default class RenderStream extends stream.Readable {
3536
this.pushBySize(n)
3637
return true // we will decide when to call next
3738
}
39+
return false
3840
}, err => {
3941
this.emit('error', err)
4042
})
@@ -73,15 +75,15 @@ export default class RenderStream extends stream.Readable {
7375
// it's possible that the last chunk added bumped the buffer up to > 2 * n,
7476
// which means we will need to go through multiple read calls to drain it
7577
// down to < n.
76-
if (this.done) {
78+
if (isTrue(this.done)) {
7779
this.push(null)
7880
return
7981
}
8082
if (this.buffer.length >= n) {
8183
this.pushBySize(n)
8284
return
8385
}
84-
if (!this.next) {
86+
if (isUndef(this.next)) {
8587
// start the rendering chain.
8688
this.tryRender()
8789
} else {

src/server/render.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { RenderContext } from './render-context'
66
import { compileToFunctions } from 'web/compiler/index'
77
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
88

9+
import { isDef, isUndef, isTrue } from 'shared/util'
10+
911
let warned = Object.create(null)
1012
const warnOnce = msg => {
1113
if (!warned[msg]) {
@@ -17,7 +19,7 @@ const warnOnce = msg => {
1719
const compilationCache = Object.create(null)
1820
const normalizeRender = vm => {
1921
const { render, template } = vm.$options
20-
if (!render) {
22+
if (isUndef(render)) {
2123
if (template) {
2224
const renderFns = (
2325
compilationCache[template] ||
@@ -36,42 +38,42 @@ const normalizeRender = vm => {
3638

3739
function renderNode (node, isRoot, context) {
3840
const { write, next } = context
39-
if (node.componentOptions) {
41+
if (isDef(node.componentOptions)) {
4042
// check cache hit
4143
const Ctor = node.componentOptions.Ctor
4244
const getKey = Ctor.options.serverCacheKey
4345
const name = Ctor.options.name
4446
const cache = context.cache
45-
if (getKey && cache && name) {
47+
if (isDef(getKey) && isDef(cache) && isDef(name)) {
4648
const key = name + '::' + getKey(node.componentOptions.propsData)
4749
const { has, get } = context
48-
if (has) {
49-
has(key, hit => {
50-
if (hit && get) {
51-
get(key, res => write(res, next))
50+
if (isDef(has)) {
51+
(has: any)(key, hit => {
52+
if (hit === true && isDef(get)) {
53+
(get: any)(key, res => write(res, next))
5254
} else {
5355
renderComponentWithCache(node, isRoot, key, context)
5456
}
5557
})
56-
} else if (get) {
57-
get(key, res => {
58-
if (res) {
58+
} else if (isDef(get)) {
59+
(get: any)(key, res => {
60+
if (isDef(res)) {
5961
write(res, next)
6062
} else {
6163
renderComponentWithCache(node, isRoot, key, context)
6264
}
6365
})
6466
}
6567
} else {
66-
if (getKey && !cache) {
68+
if (isDef(getKey) && isUndef(cache)) {
6769
warnOnce(
6870
`[vue-server-renderer] Component ${
6971
Ctor.options.name || '(anonymous)'
7072
} implemented serverCacheKey, ` +
7173
'but no cache was provided to the renderer.'
7274
)
7375
}
74-
if (getKey && !name) {
76+
if (isDef(getKey) && isUndef(name)) {
7577
warnOnce(
7678
`[vue-server-renderer] Components that implement "serverCacheKey" ` +
7779
`must also define a unique "name" option.`
@@ -80,9 +82,9 @@ function renderNode (node, isRoot, context) {
8082
renderComponent(node, isRoot, context)
8183
}
8284
} else {
83-
if (node.tag) {
85+
if (isDef(node.tag)) {
8486
renderElement(node, isRoot, context)
85-
} else if (node.isComment) {
87+
} else if (isTrue(node.isComment)) {
8688
write(`<!--${node.text}-->`, next)
8789
} else {
8890
write(node.raw ? node.text : escape(String(node.text)), next)
@@ -116,7 +118,7 @@ function renderComponentWithCache (node, isRoot, key, context) {
116118
}
117119

118120
function renderElement (el, isRoot, context) {
119-
if (isRoot) {
121+
if (isTrue(isRoot)) {
120122
if (!el.data) el.data = {}
121123
if (!el.data.attrs) el.data.attrs = {}
122124
el.data.attrs[SSR_ATTR] = 'true'
@@ -126,7 +128,7 @@ function renderElement (el, isRoot, context) {
126128
const { write, next } = context
127129
if (context.isUnaryTag(el.tag)) {
128130
write(startTag, next)
129-
} else if (!el.children || !el.children.length) {
131+
} else if (isUndef(el.children) || el.children.length === 0) {
130132
write(startTag + endTag, next)
131133
} else {
132134
const children: Array<VNode> = el.children
@@ -149,7 +151,7 @@ function getVShowDirectiveInfo (node: VNode): ?VNodeDirective {
149151
let dir: VNodeDirective
150152
let tmp
151153

152-
while (node) {
154+
while (isDef(node)) {
153155
if (node.data && node.data.directives) {
154156
tmp = node.data.directives.find(dir => dir.name === 'show')
155157
if (tmp) {
@@ -167,10 +169,10 @@ function renderStartingTag (node: VNode, context) {
167169

168170
// construct synthetic data for module processing
169171
// because modules like style also produce code by parent VNode data
170-
if (!node.data && hasAncestorData(node)) {
172+
if (isUndef(node.data) && hasAncestorData(node)) {
171173
node.data = {}
172174
}
173-
if (node.data) {
175+
if (isDef(node.data)) {
174176
// check directives
175177
const dirs = node.data.directives
176178
if (dirs) {
@@ -202,13 +204,13 @@ function renderStartingTag (node: VNode, context) {
202204
// attach scoped CSS ID
203205
let scopeId
204206
const activeInstance = context.activeInstance
205-
if (activeInstance &&
207+
if (isDef(activeInstance) &&
206208
activeInstance !== node.context &&
207-
(scopeId = activeInstance.$options._scopeId)) {
208-
markup += ` ${scopeId}`
209+
isDef(scopeId = activeInstance.$options._scopeId)) {
210+
markup += ` ${(scopeId: any)}`
209211
}
210-
while (node) {
211-
if ((scopeId = node.context.$options._scopeId)) {
212+
while (isDef(node)) {
213+
if (isDef(scopeId = node.context.$options._scopeId)) {
212214
markup += ` ${scopeId}`
213215
}
214216
node = node.parent

src/server/write.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const MAX_STACK_DEPTH = 1000
44

55
export function createWriteFunction (
6-
write: (text: string, next: Function) => ?boolean,
6+
write: (text: string, next: Function) => boolean,
77
onError: Function
88
): Function {
99
let stackDepth = 0
@@ -12,7 +12,7 @@ export function createWriteFunction (
1212
cachedWrite.cacheBuffer[cachedWrite.cacheBuffer.length - 1] += text
1313
}
1414
const waitForNext = write(text, next)
15-
if (!waitForNext) {
15+
if (waitForNext !== true) {
1616
if (stackDepth >= MAX_STACK_DEPTH) {
1717
process.nextTick(() => {
1818
try { next() } catch (e) {

0 commit comments

Comments
 (0)