-
Notifications
You must be signed in to change notification settings - Fork 14
/
serverRender.js
69 lines (55 loc) · 1.84 KB
/
serverRender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var vdomToHtml = require('vdom-to-html')
var Mount = require('./mount')
var runRender = require('./render')
var router = require('./router')
var StoreCache = require('./storeCache')
var toVdom = require('./toVdom')
module.exports.hasRoute = function (app, url) {
return router.hasRoute(app, url)
}
module.exports.render = function (app, url) {
var renderRequested = false
var cache = new StoreCache()
var mount = new Mount(app, {window: {},
router: router.create({history: new ServerHistory(url)}),
requestRender: function () {
renderRequested = true
}})
mount.serverRenderCache = cache
mount.refreshify = cache.refreshify
function renderUntilAllLoaded (mount, cache, options) {
var maxRenders = typeof options === 'object' && options.hasOwnProperty('maxRenders') ? options.maxRenders : undefined
var renders = typeof options === 'object' && options.hasOwnProperty('renders') ? options.renders : 0
if (renders >= maxRenders) {
throw new Error('page could not load all resources')
}
var vdom
var html
renderRequested = false
runRender(mount, function () {
vdom = toVdom(mount.render())
html = vdomToHtml(vdom)
})
if (cache.loadPromises.length) {
return cache.loaded().then(function () {
return renderUntilAllLoaded(mount, cache, {maxRenders: maxRenders, renders: renders + 1})
})
} else if (renderRequested) {
return renderUntilAllLoaded(mount, cache, {maxRenders: maxRenders, renders: renders + 1})
} else {
return Promise.resolve({
vdom: vdom,
html: html,
data: cache.data
})
}
}
return renderUntilAllLoaded(mount, cache, {maxRenders: 10})
}
function ServerHistory (url) {
this._url = url
}
ServerHistory.prototype.url = function () {
return this._url
}
ServerHistory.prototype.start = function () {}