Skip to content

Commit

Permalink
Use an Object.assign polyfill
Browse files Browse the repository at this point in the history
Summary:
I was running into facebook/react#6451 even though I'm on React 15.3.1
and `npm ls | grep object-assign` shows only `object-assign@4.1.0`. A
bit of debugging shows that the polyfill wasn't being applied early
enough in the node bundle that's used for server-side rendering; we
have to be sure to include it before any React components are used, so
we might as well make it the very first thing!

Test Plan:
Load localhost:8080/skills and see that there aren't any SSR errors.
  • Loading branch information
wchargin committed Sep 5, 2016
1 parent 25e3427 commit 333a574
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-react": "^6.1.2",
"file-loader": "^0.9.0",
"normalize.css": "^4.2.0",
"object-assign": "^4.1.0",
"static-site-generator-webpack-plugin": "^2.1.0",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.15.0"
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/*
* Main entry point. Exports a function to do server-side rendering,
* and runs client initialization (rehydration) code if on the frontend.
*
* Note that this module _is_ transpiled by Babel. However, we want to
* require the `object-assign` polyfill before _anything_ else happens.
* As a consequence, we can't use the ES6 module syntax, because Babel
* hosts these imports---including `export ... from` forms---to the very
* top of the file. So we manually define the exports, old-school style.
*/

import initializeClient from './client';
Object.assign = null;
Object.assign = require("object-assign");

if (typeof document !== "undefined") {
initializeClient();
const isServerRendering = typeof document === "undefined";

if (!isServerRendering) {
require("./client").default();
}

export {default} from './server';
module.exports = {
default: require("./server.js").default,
};

0 comments on commit 333a574

Please sign in to comment.