Last active
June 21, 2016 16:06
-
-
Save rarous/2a45cb077cfe937b3318 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const updateState = app => m => app.updateData(app.state.withMutations(m)); | |
const cleanup = app => ::app.dispose; | |
export default function(apiUrl, gtmKey) { | |
const api = apiFactory(apiUrl); | |
const data = fromJS(appState); | |
return async (req, res, next) => { | |
try { | |
const app = new Application(data); | |
const routes = defRoutes(app); | |
const location = req.url; | |
const {redirectLocation, renderProps} = await match({routes, location}); | |
if (redirectLocation) | |
return res.redirect(301, redirectLocation.pathname + redirectLocation.search); | |
else if (error) | |
return next(error); | |
else if (renderProps == null) | |
return res.status(404).send('Not found'); | |
const context = {location, params: renderProps.params, isActive: renderProps.history.isActive}; | |
await loadData(api, context, getAuthCookie(req, res)) | |
.then(updateState(app)) | |
.then(render(res, renderProps, gtmKey, app)) | |
.then(cleanup(app)); | |
} | |
catch (err) { | |
next(err) | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Application class is almost identical to https://gist.github.com/rarous/60dc6aef71a31de8950c