Skip to content

Commit a65fb5e

Browse files
committed
Fix express server rendering
* Had to work around no shared store support * See shakacode/react_on_rails#504
1 parent a953b02 commit a65fb5e

File tree

7 files changed

+80
-19
lines changed

7 files changed

+80
-19
lines changed

Procfile.dev

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Basic procfile for dev work.
2-
# Runs all processes. Development is faster if you pick one of the other Procfiles if you don't need
3-
# some of the processes: Procfile.hot or Procfile.express
2+
# Development is faster if you pick one of the other Procfiles if you don't need
3+
# the processes to create the test files. Thus, run the `Procfile.hot` one instead
44

55
# Development rails requires both rails and rails-assets
66
# (and rails-server-assets if server rendering)
@@ -14,8 +14,3 @@ rails-static-client-assets: sh -c 'npm run build:dev:client'
1414

1515
# Render static client assets. Remove if not server rendering
1616
rails-static-server-assets: sh -c 'npm run build:dev:server'
17-
18-
# Run an express server if you want to mock out your endpoints. No Rails involved!
19-
# Disable this if you are not using it.
20-
# It's a great way to prototype UI especially with non-Rails developers!
21-
express: sh -c 'HOT_PORT=4000 npm start'

Procfile.express

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# Use this if you want to mock out your endpoints.
33
# It's a great way to prototype UI especially with non-Rails developers!
44
# You can still run tests, and they will build the webpack file for each test run.
5-
# Hot reloading of JS and CSS is enabled!
5+
# Hot reloading of JS and CSS is enabled via the webpack-dev-server
6+
7+
# UPDATE: 2016-07-31
8+
# We no longer recommend using an express server with Rails. It's simply not necessary because:
9+
# 1. Rails can hot reload
10+
# 2. There's extra maintenance in keeping this synchronized.
11+
# 3. React on Rails does not have a shared_store JS rendering:
12+
# https://github.com/shakacode/react_on_rails/issues/504
613

714
express: sh -c 'HOT_PORT=4000 npm start'

README.md

+19-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ By Justin Gordon and the Shaka Code Team, [www.shakacode.com](http://www.shakaco
2323
An outdated full tutorial article behind of the motivation of this system can be found at: [Fast Rich Client Rails Development With Webpack and the ES6 Transpiler](http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/). Note, this source code repository is way ahead of the tutorial.
2424

2525
# NEWS
26-
We have not yet updated the `react_on_rails` gem generators for the following tasks. We're looking for help to migrate this, if you're interested in contributing to the project. *The tutorial* refers to this repo. The following changes have resulted in lots of differences for the webpack files and visual assets:
26+
We have not yet updated the `react_on_rails` gem generators for the following tasks, and we probably never will. *The tutorial* refers to this repo. The following changes have resulted in lots of differences for the webpack files and visual assets:
2727

2828
1. NOTE: Any references to localhost:3000 *might* need to use 0.0.0.0:3000 until Puma fixes an issue regarding this.
2929
1. **Handling of Sass and Bootstrap**: The tutorial uses CSS modules via Webpack. This is totally different than the older way of having Rails handle Sass/Bootstrap, and having NPM/Webpack handle the Webpack Dev Server. The tutorial now has NPM handle all visual assets. We are using this technique on a new app, and it's awesome!
@@ -81,9 +81,6 @@ See package.json and Gemfile for versions
8181
1. `foreman start -f Procfile.hot`
8282
1. Open a browser tab to http://localhost:3000 for the Rails app example with HOT RELOADING
8383
2. Try Hot Reloading steps below!
84-
1. `foreman start -f Procfile.express`
85-
1. Open a browser tab to http://localhost:4000 for the Hot Module Replacement Example just using an express server (no Rails involved). This is good for fast prototyping of React components. However, this setup is not as useful now that we have hot reloading working for Rails!
86-
2. Try Hot Reloading steps below!
8784
1. `foreman start -f Procfile.static`
8885
1. Open a browser tab to http://localhost:3000 for the Rails app example.
8986
2. When you make changes, you have to refresh the browser page.
@@ -98,7 +95,6 @@ See package.json and Gemfile for versions
9895
1. See all npm commands: `npm run`
9996
1. Start all development processes: `foreman start -f Procfile.dev`
10097
1. Start all Rails only development processes: `foreman start -f Procfile.hot`
101-
1. Start development without Rails, using the Webpack Dev Server only: `npm start` (or `foreman start -f Procfile.express`)
10298

10399

104100
# Javascript development without Rails using the Webpack Dev Server
@@ -138,7 +134,7 @@ line in the `rails_helper.rb` file. If you are using this project as an example
138134
## Config Files
139135

140136
- `webpack.client.base.config.js`: Common **client** configuration file to minimize code duplication for `webpack.client.rails.build.config`, `webpack.client.rails.hot.config`, `webpack.client.express.config`
141-
- `webpack.client.express.config.js`: Webpack configuration for [client/server-express.js](client/server-express.js)
137+
- `webpack.client.express.config.js`: Webpack configuration for Express server [client/server-express.js](client/server-express.js)
142138
- `webpack.client.rails.build.config.js`: Client side js bundle for deployment and tests.
143139
- `webpack.client.rails.hot.config.js`: Webpack Dev Server bundler for serving rails assets on port 3500, used by [client/server-rails-hot.js](client/server-rails-hot.js), for hot reloading JS and CSS within Rails.
144140
- `webpack.server.rails.build.config.js`: Server side js bundle, used by server rendering.
@@ -180,11 +176,26 @@ bundle exec foreman start -f <Procfile>
180176
1. [`Procfile.dev`](Procfile.dev): Starts the Webpack Dev Server and Rails with Hot Reloading.
181177
2. [`Procfile.hot`](Procfile.hot): Starts the Rails server and the webpack server to provide hot reloading of assets, JavaScript and CSS.
182178
3. [`Procfile.static`](Procfile.static): Starts the Rails server and generates static assets that are used for tests.
183-
4. [`Procfile.express`](Procfile.express): Starts only the Webpack Dev Server.
184179
5. [`Procfile.spec`](Procfile.spec): Starts webpack to create the static files for tests. **Good to know:** If you want to start `rails s` separately to debug in `pry`, then run `Procfile.spec` to generate the assets and run `rails s` in a separate console.
185180
6. [`Procfile.static.trace`](Procfile.static.trace): Same as `Procfile.static` but prints tracing information useful for debugging server rendering.
181+
4. [`Procfile.express`](Procfile.express): Starts only the Webpack Dev Server for rendering your components with only an Express server.
182+
183+
In general, you want to avoid running more webpack watch processes than you need.
184+
185+
## Rendering With an Express Server
186+
UPDATE: 2016-07-31
187+
188+
We no longer recommend using an express server with Rails. It's simply not necessary because:
189+
190+
1. Rails can hot reload
191+
2. There's extra maintenance in keeping this synchronized.
192+
3. React on Rails does not have a shared_store JS rendering, per [issue #504](https://github.com/shakacode/react_on_rails/issues/504)
193+
194+
### Here's how to run the express server setup
186195

187-
In general, you want to avoid running more webpack watch processes than you need. The `Procfile.dev`, for example, runs both the express server (Webpack dev server) and the Rails hot assets reloading server.
196+
1. `foreman start -f Procfile.express`
197+
2. Open a browser tab to http://localhost:4000 for the Hot Module Replacement Example just using an express server (no Rails involved). This is good for fast prototyping of React components. However, this setup is not as useful now that we have hot reloading working for Rails!
198+
3. Try Hot Reloading steps below!
188199

189200
## Contributors
190201
[The Shaka Code team!](http://www.shakacode.com/about/), led by [Justin Gordon](https://github.com/justin808/), along with with many others. See [contributors.md](docs/contributors.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Compare to ../ServerRouterApp.jsx
2+
import React from 'react';
3+
import { Provider } from 'react-redux';
4+
import ReactOnRails from 'react-on-rails';
5+
import { Router, browserHistory } from 'react-router';
6+
import routes from '../routes/routes';
7+
import { syncHistoryWithStore } from 'react-router-redux';
8+
9+
// Because of https://github.com/shakacode/react_on_rails/issues/504
10+
// we need to skip using a shared store for the express server startup.
11+
import createStore from '../store/routerCommentsStore';
12+
13+
const RouterAppExpress = (_props, _railsContext) => {
14+
15+
// See comment above
16+
const store = createStore(_props);
17+
18+
// Create an enhanced history that syncs navigation events with the store
19+
const history = syncHistoryWithStore(
20+
browserHistory,
21+
store
22+
);
23+
24+
return (
25+
<Provider store={store}>
26+
<Router history={history} children={routes} />
27+
</Provider>
28+
);
29+
};
30+
31+
ReactOnRails.register({
32+
RouterAppExpress,
33+
});

client/index.pug

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,16 @@ html
1010

1111
script(src="vendor-bundle.js")
1212
script(src="app-bundle.js")
13+
14+
// When this is implemented: https://github.com/shakacode/react_on_rails/issues/504
15+
// Then you can do this, to match up with what's in the Rails view:
16+
// script.
17+
// ReactOnRails.reduxStore('routerCommentsStore', !{props});
18+
// script.
19+
// ReactOnRails.render('RouterApp', !{}, 'app');
20+
//
21+
// Instead, we'll pass the props and add RouterAppExpress as an entry point to the
22+
// webpack.client.express.config.js
23+
1324
script.
14-
ReactOnRails.render("RouterApp", !{props}, 'app');
25+
ReactOnRails.render('RouterAppExpress', !{props}, 'app');

client/server-express.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ server.app.use(bodyParser.urlencoded({ extended: true }));
3131
server.app.get('/comments.json', (req, res) => {
3232
sleep.sleep(1);
3333
res.setHeader('Content-Type', 'application/json');
34-
res.send(JSON.stringify(comments));
34+
res.send(JSON.stringify({ comments }));
3535
});
3636

3737
server.app.post('/comments.json', (req, res) => {
@@ -49,7 +49,7 @@ server.app.post('/comments.json', (req, res) => {
4949

5050
server.app.use('/', (req, res) => {
5151
var locals = {
52-
props: JSON.stringify(comments),
52+
props: JSON.stringify({ comments }),
5353
};
5454
var layout = `${process.cwd()}/index.pug`;
5555
var html = pug.compileFile(layout, { pretty: true })(locals);

client/webpack.client.express.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const hotPort = process.env.HOT_PORT || 4000;
1010
config.entry.vendor.push('bootstrap-loader');
1111
config.entry.app.push(
1212

13+
// Shouldn't be necessary:
14+
// https://github.com/shakacode/react_on_rails/issues/504
15+
'./app/bundles/comments/startup/ClientRouterAppExpress',
16+
1317
// Webpack dev server
1418
`webpack-dev-server/client?http://localhost:${hotPort}`,
1519
'webpack/hot/dev-server'

0 commit comments

Comments
 (0)