Skip to content

Instantly share code, notes, and snippets.

@rarous
Last active June 21, 2016 16:06
Show Gist options
  • Save rarous/2a45cb077cfe937b3318 to your computer and use it in GitHub Desktop.
Save rarous/2a45cb077cfe937b3318 to your computer and use it in GitHub Desktop.
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)
}
};
};
@rarous
Copy link
Author

rarous commented Dec 28, 2015

Application class is almost identical to https://gist.github.com/rarous/60dc6aef71a31de8950c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment