-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Express server at port 3001 to render components
The express server will take the requested path and serialized state as input and will return the HTML representation as well as the state embedded in script tags. You will want to use something like supervisor to restart the server on component changes. Technically we could have used Webpack to hotload the changes, but I couldn't figure it out
- Loading branch information
Showing
5 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
web: rails s | ||
webpack: webpack-dev-server --config app/assets/webpack.config.js --progress --inline --module-bind --hot | ||
node: supervisor -e node,js,coffee,jsx,cjsx app/assets/javascripts/component_render_server.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var renderToString = require('enhanced-require')(module, { | ||
resolve: { | ||
extensions: ['', '.js', '.jsx', '.cjsx', '.coffee'], | ||
modulesDirectories: ["node_modules", "javascripts"] | ||
}, | ||
|
||
module: { | ||
loaders: [ | ||
{ test: /\.js?$/, loaders: []}, | ||
{ test: /\.jsx?$/, loaders: ['jsx-loader'] }, | ||
{ test: /\.cjsx$/, loaders: ["react-hot", "coffee", "cjsx"]}, | ||
{ test: /\.coffee$/, loader: "coffee"}, | ||
{ test: /\.json$/, loader: "json-loader"} | ||
] | ||
} | ||
})('render_to_string') | ||
|
||
var express = require('express') | ||
var app = express() | ||
|
||
app.use(require("body-parser").json()); | ||
app.post('/', function (req, res) { | ||
renderToString(req.body.path, req.body.serializedStoreState, function(s){ | ||
res.end(s) | ||
}); | ||
}) | ||
|
||
var server = app.listen(3001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
React = require('react') | ||
Router = require('react-router') | ||
routes = require('./routes') | ||
|
||
Flux = require('flux') | ||
FluxComponent = require('flummox/component') | ||
|
||
renderToString = (route, serializedStoreState, callback) -> | ||
flux = new Flux() | ||
flux.deserialize(serializedStoreState) | ||
|
||
Router.run routes, route, (Handler, state) -> | ||
html = React.renderToString(<FluxComponent flux={flux} render={ => <Handler />}></FluxComponent>) | ||
embeddedStoreState = "<script>" | ||
embeddedStoreState += "window.serializedStoreState = " + JSON.stringify(serializedStoreState) | ||
embeddedStoreState += "</script>" | ||
callback (html + embeddedStoreState) | ||
|
||
module.exports = renderToString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters