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

Commit 23f171f

Browse files
committed
Moved async dependency to ContextHelper. Added 'src' to webpack modulesDirectories.
1 parent 87b0fbc commit 23f171f

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"scripts": {
2222
"start": "supervisor dist/server",
2323
"localhost": "sleep 1; which open && open http://localhost:8000",
24-
"browser": "sleep 6; concurrent 'npm run start' 'npm run localhost'",
24+
"browser": "sleep 6; concurrent --kill-others 'npm run start' 'npm run localhost'",
2525
"build-server": "webpack --verbose --colors --display-error-details --config webpack.server.js",
2626
"build-client": "webpack --verbose --colors --display-error-details --config webpack.client.js",
2727
"build": "concurrent 'npm run build-client' 'npm run build-server'",

src/client.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import Router from "react-router";
3-
import ContextHelper from "./helpers/ContextHelper";
4-
import routes from "./views/Routes";
3+
import ContextHelper from "helpers/ContextHelper";
4+
import routes from "views/Routes";
55

66
/**
77
* Enable Accessibility warnings on the client.
@@ -17,10 +17,11 @@ Router.run(routes, Router.HistoryLocation, (Handler) => {
1717
/**
1818
* Get Client Context passed along by the server, and inject it.
1919
*/
20-
const clientContext = ContextHelper.getClientContext(window);
21-
const ContextualHandler = ContextHelper.injectContext(Handler, clientContext);
20+
const context = ContextHelper.getClientContext(window);
2221

23-
React.render(<ContextualHandler />, document.getElementById("react-root"));
22+
ContextHelper.injectContext(Handler, context, (ContextualHandler) => {
23+
React.render(<ContextualHandler />, document.getElementById("react-root"));
24+
});
2425
});
2526

2627
/**

src/helpers/ContextHelper.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Async from "async";
12
import React from "react";
23

34
/**
@@ -17,7 +18,7 @@ const ContextMixin = {
1718
},
1819
getContext (key) {
1920
if (!this.context) {
20-
return null;
21+
return undefined;
2122
}
2223

2324
if (key === undefined) {
@@ -45,14 +46,14 @@ const ContextHelper = {
4546
(k) => context.contextLoaders[k]
4647
);
4748
},
48-
injectContext (Component, context) {
49+
injectContext (Component, context, callbackFn) {
4950
let childContextTypes = {};
5051

5152
Object.keys(context).map((key) => {
5253
childContextTypes[key] = React.PropTypes.any
5354
});
5455

55-
return React.createClass({
56+
const ContextualComponent = React.createClass({
5657
childContextTypes,
5758
getChildContext () {
5859
return context;
@@ -61,6 +62,21 @@ const ContextHelper = {
6162
return <Component />;
6263
}
6364
});
65+
66+
if (__CLIENT__) {
67+
callbackFn(ContextualComponent);
68+
return;
69+
}
70+
71+
/**
72+
* Fake-render the components without output so they can register context loaders.
73+
*/
74+
React.renderToString(<ContextualComponent />);
75+
const contextLoaders = ContextHelper.getContextLoaders(context);
76+
77+
Async.parallel(contextLoaders, (error, result) => {
78+
callbackFn(ContextualComponent);
79+
});
6480
},
6581
getServerContext () {
6682
let serverContext = {};

src/server.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import async from "async";
21
import {Server} from "hapi";
32
import React from "react";
43
import Router from "react-router";
5-
import ContextHelper from "./helpers/ContextHelper";
6-
import routes from "./views/Routes";
4+
import ContextHelper from "helpers/ContextHelper";
5+
import routes from "views/Routes";
76

87
/**
98
* Start Hapi server on port 8000.
@@ -33,29 +32,23 @@ server.ext("onPreResponse", (request, reply) => {
3332
return reply.continue();
3433
}
3534

36-
Router.run(routes, request.path, (Handler) => {
35+
Router.run(routes, request.path, (Handler, router) => {
3736
/**
3837
* Prepare a unique Server Context per request, and inject it.
3938
*/
40-
const serverContext = ContextHelper.getServerContext();
41-
serverContext.request = request;
42-
const ContextualHandler = ContextHelper.injectContext(Handler, serverContext);
39+
const context = ContextHelper.getServerContext();
40+
context.request = request;
41+
context.router = router;
4342

44-
/**
45-
* Fake-render the components without output so they can register context loaders.
46-
*/
47-
React.renderToString(<ContextualHandler />);
48-
const loaders = ContextHelper.getContextLoaders(serverContext);
49-
50-
/**
51-
* Wait for all the registered callbacks and render for real, but this time with data.
52-
*/
53-
async.parallel(loaders, (error, results) => {
43+
ContextHelper.injectContext(Handler, context, (ContextualHandler) => {
44+
/**
45+
* Wait for all the registered callbacks and render for real, but this time with data.
46+
*/
5447
const rendered = React.renderToString(<ContextualHandler />);
55-
const contextData = JSON.stringify(serverContext.contextData);
48+
const contextData = JSON.stringify(context.contextData);
5649
const webserver = process.env.NODE_ENV === "production" ? "" : "//localhost:8080";
5750

58-
const output =
51+
const output = (
5952
`<!doctype html>
6053
<html lang="en-us">
6154
<head>
@@ -68,7 +61,8 @@ server.ext("onPreResponse", (request, reply) => {
6861
<script>window.CONTEXT_DATA = ${contextData};</script>
6962
<script src="${webserver}/dist/client.js"></script>
7063
</body>
71-
</html>`;
64+
</html>`
65+
);
7266

7367
reply(output);
7468
});

src/views/Main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from "react";
22
import InlineCss from "react-inline-css";
33
import Superagent from "superagent";
4-
import ContextHelper from "../helpers/ContextHelper";
4+
import ContextHelper from "helpers/ContextHelper";
55

66
/**
77
* Main React application entry-point for both the server and client.
88
*
99
* @class Main
1010
*/
11-
export default React.createClass({
11+
const Main = React.createClass({
1212
mixins: [
1313
ContextHelper.Mixin
1414
],
@@ -151,3 +151,5 @@ export default React.createClass({
151151
);
152152
}
153153
});
154+
155+
export default Main;

src/views/Routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import Router, {Route, DefaultRoute} from "react-router";
3-
import Main from "./Main";
3+
import Main from "views/Main";
44

55
/**
66
* The React Routes for both the server and the client.

webpack.client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ module.exports = {
2828
]
2929
},
3030
resolve: {
31+
modulesDirectories: [
32+
"src",
33+
"node_modules",
34+
"web_modules"
35+
],
3136
extensions: ["", ".json", ".jsx", ".js"]
37+
},
38+
node: {
39+
__dirname: true,
40+
fs: 'empty'
3241
}
3342
};

webpack.server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ module.exports = {
2525
]
2626
},
2727
resolve: {
28+
modulesDirectories: [
29+
"src",
30+
"node_modules",
31+
"web_modules"
32+
],
2833
extensions: ["", ".json", ".jsx", ".js"]
2934
},
3035
node: {
31-
__dirname: true,
32-
fs: 'empty'
36+
__dirname: true
3337
}
3438
};

0 commit comments

Comments
 (0)