Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

Commit

Permalink
fix(server): server entry point now throws if react router returns no…
Browse files Browse the repository at this point in the history
… initial state (fixes bad urls breaking server)
  • Loading branch information
David Zukowski committed Aug 16, 2015
1 parent 0453f09 commit b599fa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
13 changes: 9 additions & 4 deletions server/middleware/render-route.js
@@ -1,14 +1,19 @@
const router = require('../../dist/server'),
const runRouter = require('../../dist/server'),
USE_CACHE = process.env.NODE_ENV === 'production';

function renderIntoTemplate (template, content) {
return template.replace('${content}', content);
}

module.exports = function makeRenderRouteMiddleware (template) {
return function *renderRouteMiddleware () {
const rendered = yield router(this.request);
return function *renderRouteMiddleware (next) {
try {
const rendered = yield runRouter(this.request);

this.body = renderIntoTemplate(template, rendered);
this.body = renderIntoTemplate(template, rendered);
} catch (e) {
console.log(e);
yield next;
}
};
};
19 changes: 12 additions & 7 deletions src/entry-points/server.js
Expand Up @@ -8,16 +8,21 @@ export default function render (request) {
return function renderThunk (callback) {
const location = new Location(request.path, request.query);

Router.run(routes, location, function (error, initialState, transition) {
try {
try {
Router.run(routes, location, function (error, initialState, transition) {
if (!initialState) {
throw new Error(
`Could not render ${request.path}: no initial state returned.`
);
}

const rendered = React.renderToString(
<App initialState={initialState} />
);

callback(null, rendered);
} catch (e) {
callback(e);
}
});
});
} catch (e) {
callback(e);
}
};
}

0 comments on commit b599fa8

Please sign in to comment.