From fd20b6d8862bf0d4e55858d134eb7e4d7ccb0769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20T=C3=B6rnqvist?= Date: Thu, 22 Mar 2018 13:31:47 +0100 Subject: [PATCH] Init choo stores before list routes --- lib/graph-document.js | 19 +++++++++++-------- package.json | 2 +- ssr/choo.js | 24 ++++++++++++++++++------ ssr/index.js | 11 +++++------ 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/graph-document.js b/lib/graph-document.js index 2a6e5d9f..e9644619 100644 --- a/lib/graph-document.js +++ b/lib/graph-document.js @@ -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) { diff --git a/package.json b/package.json index 05c6a5d4..f329ceb6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/ssr/choo.js b/ssr/choo.js index 8aea293f..a62bda31 100644 --- a/ssr/choo.js +++ b/ssr/choo.js @@ -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 @@ -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) } diff --git a/ssr/index.js b/ssr/index.js index 0a6b92a4..85998ce6 100644 --- a/ssr/index.js +++ b/ssr/index.js @@ -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 @@ -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' @@ -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.