Skip to content

Commit

Permalink
using ReactDOM.hydrate to bootstrap SSR content (#787)
Browse files Browse the repository at this point in the history
* using `ReactDOM.hydrate` when content is rendered server side

* freeze on yeoman-test@1.7.0 since 1.7.1 contains breaking changes

* Revert "freeze on yeoman-test@1.7.0 since 1.7.1 contains breaking changes"

This reverts commit 705c798.

* fixing unit tests, changed bad-routes to do not have return statement,
returning string is a valid syntax with React 16

* re-added react and react-dom as peer dependency for redux-router package

* remove direct react dependency from PWA template, switch to newer react-notify-toast that uses React 16
  • Loading branch information
romanlv authored and jchip committed May 17, 2018
1 parent d13afed commit 27bb19b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/electrode-redux-router-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"babel-preset-stage-0": "^6.0.15",
"babel-register": "^6.5.2",
"electrode-archetype-njs-module-dev": "^2.1.0",
"react": "^15.3.1 || ^0.14.8",
"react-dom": "^15.3.1 || ^0.14.8",
"react": "^16.0.0 || ^15.3.1 || ^0.14.8",
"react-dom": "^16.0.0 || ^15.3.1 || ^0.14.8",
"xstdout": "^0.1.1"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions packages/electrode-redux-router-engine/test/bad-routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Route, IndexRoute } from "react-router";

class Home extends React.Component {
render () {
return "Home";
}
}

class Page extends React.Component {
render () {
return "Page";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("redux-router-engine", function () {
intercept.restore();
expect(result.status).to.equal(500);
expect(result._err.message)
.to.contain("Page.render(): A valid React element (or null) must be returned");
.to.contain("Nothing was returned from render");
});
});

Expand Down Expand Up @@ -177,7 +177,7 @@ describe("redux-router-engine", function () {
testReq.url.path = "/test";

return engine.render(testReq).then((result) => {
expect(result.html).to.contain("data-reactid");
expect(result.html).to.contain("data-reactroot");
});
});

Expand All @@ -186,7 +186,7 @@ describe("redux-router-engine", function () {
testReq.url.path = "/test";

return engine.render(testReq).then((result) => {
expect(result.html).to.not.contain("data-reactid");
expect(result.html).to.not.contain("data-reactroot");
});
});

Expand Down Expand Up @@ -223,7 +223,7 @@ describe("redux-router-engine", function () {
testReq.url.path = "/test";

return engine.render(testReq, { withIds: false }).then((result) => {
expect(result.html).to.not.contain("data-reactid");
expect(result.html).to.not.contain("data-reactroot");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"koa-router": "^5.4.0",
"koa-send": "^3.2.0",
"koa-static": "^2.0.0", <% } if (isPWA) { %>
"react-notify-toast": "^0.4.0",<% } if (isAutoSSR) {%>
"react-notify-toast": "^0.4.1",<% } if (isAutoSSR) {%>
"electrode-auto-ssr": "^1.0.0", <% } %>
"lodash": "^4.10.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//

import React from "react";
import { render } from "react-dom";
import { render, hydrate } from "react-dom";
import { routes } from "./routes";
import { Router, browserHistory } from "react-router";
import { createStore } from "redux";
Expand All @@ -29,11 +29,14 @@ require.ensure(

window.webappStart = () => {
const initialState = window.__PRELOADED_STATE__;
const jsContent = document.querySelector(".js-content");
const reactStart = (initialState && jsContent.innerHTML) ? hydrate : render;

const store = createStore(rootReducer, initialState);
render(
reactStart(
<Provider store={store}>
<Router history={browserHistory}>{routes}</Router>
</Provider>,
document.querySelector(".js-content")
jsContent
);
};
2 changes: 1 addition & 1 deletion samples/universal-react-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"es6-promise": "^4.0.5",
"isomorphic-fetch": "^2.2.1",
"mongojs": "^2.4.0",
"react-notify-toast": "^0.4.0",
"react-notify-toast": "^0.4.1",
"uuid": "^3.0.1"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions samples/universal-react-node/src/client/app.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import {render} from "react-dom";
import {render, hydrate} from "react-dom";
import {routes} from "./routes";
import {Router, browserHistory} from "react-router";
import {createStore, compose, applyMiddleware} from "redux";
Expand All @@ -23,14 +23,17 @@ const enhancer = compose(

window.webappStart = () => {
const initialState = window.__PRELOADED_STATE__;
const jsContent = document.querySelector(".js-content");

const store = createStore(rootReducer, initialState, enhancer);
render(
const reactStart = (initialState && jsContent.innerHTML) ? hydrate : render;
reactStart(
<Provider store={store}>
<div>
<Router history={browserHistory}>{routes}</Router>
<DevTools />
</div>
</Provider>,
document.querySelector(".js-content")
jsContent
);
};

0 comments on commit 27bb19b

Please sign in to comment.