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

Commit b599fa8

Browse files
author
David Zukowski
committed
fix(server): server entry point now throws if react router returns no initial state (fixes bad urls breaking server)
1 parent 0453f09 commit b599fa8

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

server/middleware/render-route.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
const router = require('../../dist/server'),
1+
const runRouter = require('../../dist/server'),
22
USE_CACHE = process.env.NODE_ENV === 'production';
33

44
function renderIntoTemplate (template, content) {
55
return template.replace('${content}', content);
66
}
77

88
module.exports = function makeRenderRouteMiddleware (template) {
9-
return function *renderRouteMiddleware () {
10-
const rendered = yield router(this.request);
9+
return function *renderRouteMiddleware (next) {
10+
try {
11+
const rendered = yield runRouter(this.request);
1112

12-
this.body = renderIntoTemplate(template, rendered);
13+
this.body = renderIntoTemplate(template, rendered);
14+
} catch (e) {
15+
console.log(e);
16+
yield next;
17+
}
1318
};
1419
};

src/entry-points/server.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ export default function render (request) {
88
return function renderThunk (callback) {
99
const location = new Location(request.path, request.query);
1010

11-
Router.run(routes, location, function (error, initialState, transition) {
12-
try {
11+
try {
12+
Router.run(routes, location, function (error, initialState, transition) {
13+
if (!initialState) {
14+
throw new Error(
15+
`Could not render ${request.path}: no initial state returned.`
16+
);
17+
}
18+
1319
const rendered = React.renderToString(
1420
<App initialState={initialState} />
1521
);
16-
1722
callback(null, rendered);
18-
} catch (e) {
19-
callback(e);
20-
}
21-
});
23+
});
24+
} catch (e) {
25+
callback(e);
26+
}
2227
};
2328
}

0 commit comments

Comments
 (0)