Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

safeguard against property over look while fusing together data object and routerProps object in the server render #173

Merged
merged 1 commit into from
Aug 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,35 @@ exports.create = function create(createOptions) {
});
return done(err);
} else if (renderProps) {
// define a createElement strategy for react-router that transfers data props to all route "components"
renderProps.createElement = function(Component, routerProps) {
// for any component created by react-router, merge model data with the routerProps
// Other than fusing the data object with the routerProps, there is no way
// to pass data into the routing context of react-router during a server render.
// since we are going to use `assign` to fuse the routerProps and the actual
// data object, we need to make sure that there are no properties between the two object
// with the same name at the root level. (Having two properties with the same name breaks assign.)
// Info on why we need to fuse the two objects?
// --------------------------------------------
// * https://github.com/ngduc/react-setup/issues/10
// * https://github.com/reactjs/react-router/issues/1969
// * http://stackoverflow.com/questions/36137901/react-route-and-server-side-rendering-how-to-render-components-with-data
if (options.settings.env !== 'production') {
var intersection = Object.keys(routerProps).filter(function(elem) {
return Object.keys(data).indexOf(elem) !== -1;
});
if (intersection.length) {
var errMsg = 'Your data object cannot have property(ies) named: "' +
intersection +
'"\n Blacklisted property names that cannot be used: "' +
Object.keys(routerProps) +
'"\n'
throw new Error(errMsg);
}
}

// define a createElement strategy for react-router that transfers data props to all route "components"
// for any component created by react-router, fuse data object with the routerProps
// NOTE: This may be imposing too large of an opinion?
return React.createElement(Component, _.merge({}, data, routerProps));
return React.createElement(Component, _.assign({}, data, routerProps));
};

return done(null, renderAndDecorate(React.createElement(RouterContext, renderProps), data, html));
Expand Down