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

Asynchronously init choo stores before list routes #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/graph-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ function node (state, createEdge) {
return self.emit('error', 'documents', entry, ssr.error)
}

// TODO: don't pass a callback here - super hard to reason about. Find a
// different way instead. Perhaps a prototype with methods on it instead?
self.emit('ssr', { success: true, renderRoute: documentifyRoute })

var fonts = extractFonts(state.assets)
var list = ssr.routes

mapLimit(list, WRITE_CONCURRENCY, documentifyRoute, function (err) {
if (err) return self.emit(err)
createEdge('list', Buffer.from(list.join(',')))
ssr.listRoutes(function (err, list) {
if (err) return self.emit('ssr', { success: false, error: ssr.error })

// TODO: don't pass a callback here - super hard to reason about. Find a
// different way instead. Perhaps a prototype with methods on it instead?
self.emit('ssr', { success: true, renderRoute: documentifyRoute })

mapLimit(list, WRITE_CONCURRENCY, documentifyRoute, function (err) {
if (err) return self.emit(err)
createEdge('list', Buffer.from(list.join(',')))
})
})

function documentifyRoute (route, done) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"devDependencies": {
"a-module-with-babelrc": "^1.0.0",
"assert-html": "^1.1.5",
"choo": "^6.8.0",
"choo": "^6.10.3",
"choo-devtools": "^2.3.3",
"choo-service-worker": "^2.4.0",
"rimraf": "^2.6.2",
Expand Down
24 changes: 18 additions & 6 deletions ssr/choo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ module.exports.is = function (app) {
app.router.router._trie)
}

module.exports.listRoutes = function (app) {
var keys = getAllRoutes(app.router.router)
return Object.keys(keys).filter(function (key) {
return !/\/:/.test(key) // Server rendering partials is tricky.
module.exports.listRoutes = function (app, cb) {
var state = { events: app._events }
state._experimental_prefetch = []

app._stores.forEach(function (initStore) {
initStore(state)
})

Promise.all(state._experimental_prefetch).then(getRoutes, getRoutes)

function getRoutes () {
delete state._experimental_prefetch
var keys = getAllRoutes(app.router.router)
cb(null, Object.keys(keys).filter(function (key) {
return !/\/:/.test(key) // Server rendering partials is tricky.
}))
}
}

// Do a double render pass - the first time around we wait for promises to be
Expand Down Expand Up @@ -74,8 +86,8 @@ module.exports.render = function (app, route, cb) {
delete state._experimental_prefetch // State needs to be serializable.
var res = { state: state }
if (body) res.body = body
if (app.state.title) res.title = app.state.title
if (app.state.language) res.language = app.state.language
if (state.title) res.title = state.title
if (state.language) res.language = state.language
if (app.selector) res.selector = app.selector
cb(null, res)
}
Expand Down
11 changes: 5 additions & 6 deletions ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = class ServerRender {
this.app = this._requireApp(this.entry)

this.appType = this._getAppType(this.app)
this.routes = this._listRoutes(this.app)
this.entry = entry
this.error = null

Expand All @@ -34,6 +33,11 @@ module.exports = class ServerRender {
}
}

listRoutes (cb) {
if (this.appType === 'choo') return choo.listRoutes(this.app, cb)
return cb(null, ['/'])
}

_getAppType (app) {
if (choo.is(app)) return 'choo'
else return 'default'
Expand All @@ -51,11 +55,6 @@ module.exports = class ServerRender {
}
}
}

_listRoutes (app) {
if (this.appType === 'choo') return choo.listRoutes(this.app)
return ['/']
}
}

// Clear the cache, and require the file again.
Expand Down