Skip to content

Commit a88cd1c

Browse files
committed
Bump react_on_rails gem version to 1.0.3
Add server-bundle.js to .gitignore Add server webpack command to Procfile.dev Create webpack.server.config.js for server side rendering. Add react_on_rails config initializer file Rename App.jsx to ServerApp.jsx to use for App server-side Add clientGlobals.jsx entry point for client-side rendering Add serverGlobals.jsx entry point for server-side rendering Add ClientApp.jsx for client-side App
1 parent 800d18f commit a88cd1c

13 files changed

+80
-8
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/public/assets
1818
client-bundle.js
1919
client-bundle.js.map
20+
server-bundle.js
21+
server-bundle.js.map
2022
.env
2123
node_modules
2224
npm-debug.log

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ gem "rails-html-sanitizer"
3939
# Use Unicorn as the app server
4040
gem "unicorn"
4141

42-
gem "react_on_rails", "~> 0.1.1"
42+
gem "react_on_rails", "~> 0.1.3"
43+
gem "therubyracer"
4344

4445
gem "autoprefixer-rails"
4546

Gemfile.lock

+8-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ GEM
123123
kgio (2.9.3)
124124
launchy (2.4.3)
125125
addressable (~> 2.3)
126+
libv8 (3.16.14.11)
126127
loofah (2.0.2)
127128
nokogiri (>= 1.5.9)
128129
mail (2.6.3)
@@ -180,9 +181,10 @@ GEM
180181
raindrops (0.15.0)
181182
rake (10.4.2)
182183
rdoc (4.2.0)
183-
react_on_rails (0.1.1)
184+
react_on_rails (0.1.3)
184185
execjs (~> 2.5)
185186
rails (~> 4.2)
187+
ref (2.0.0)
186188
rest-client (1.8.0)
187189
http-cookie (>= 1.0.2, < 2.0)
188190
mime-types (>= 1.16, < 3.0)
@@ -252,6 +254,9 @@ GEM
252254
term-ansicolor (1.3.2)
253255
tins (~> 1.0)
254256
terminal-table (1.5.2)
257+
therubyracer (0.12.2)
258+
libv8 (~> 3.16.14.0)
259+
ref
255260
thor (0.19.1)
256261
thread_safe (0.3.5)
257262
tilt (1.4.1)
@@ -307,7 +312,7 @@ DEPENDENCIES
307312
rails-html-sanitizer
308313
rails_12factor
309314
rainbow
310-
react_on_rails (~> 0.1.1)
315+
react_on_rails (~> 0.1.3)
311316
rspec-rails
312317
rubocop
313318
ruby-lint
@@ -317,6 +322,7 @@ DEPENDENCIES
317322
spring
318323
spring-commands-rspec
319324
sqlite3
325+
therubyracer
320326
turbolinks
321327
uglifier (~> 2.7.2)
322328
unicorn

Procfile.dev

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
web: rails s -p 4000
22
client: sh -c 'cd client && $(npm bin)/webpack -w --config webpack.rails.config.js'
3+
server: sh -c 'cd client && $(npm bin)/webpack -w --config webpack.server.config.js'
34
hot: sh -c 'cd client && node server.js'

app/views/pages/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
</li>
1717
</ul>
1818
<hr/>
19-
<%= react_component('App', {}, prerender: false) %>
19+
<%= react_component('App', {}, generator_function: true, prerender: true) %>

client/assets/javascripts/App.jsx renamed to client/assets/javascripts/ClientApp.jsx

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ const App = () => {
1313
return reactComponent;
1414
};
1515

16-
window.App = App;
17-
1816
// Export is needed for the hot reload server
1917
export default App;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { Provider } from 'react-redux';
3+
4+
import CommentScreen from './components/CommentScreen';
5+
import CommentStore from './stores/CommentStore';
6+
7+
const App = () => {
8+
const reactComponent = (
9+
<Provider store={CommentStore}>
10+
{() => <CommentScreen />}
11+
</Provider>
12+
);
13+
return reactComponent;
14+
};
15+
16+
// Export is needed for the hot reload server
17+
export default App;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import App from './ClientApp';
2+
3+
window.App = App;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Example of React + Redux
2+
// Shows the mapping from the exported object to the name used by the server rendering.
3+
import App from './ServerApp';
4+
5+
// We can use the node global object for exposing.
6+
// NodeJs: https://nodejs.org/api/globals.html#globals_global
7+
global.App = App;

client/webpack.common.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66

77
// the project dir
88
context: __dirname,
9-
entry: ['jquery', 'jquery-ujs', './assets/javascripts/App'],
9+
entry: ['jquery', 'jquery-ujs'],
1010

1111
resolve: {
1212
root: [

client/webpack.rails.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ config.output = {
1212
};
1313

1414
// You can add entry points specific to rails here
15-
config.entry.push('./scripts/rails_only');
15+
config.entry.push('./scripts/rails_only', './assets/javascripts/clientGlobals');
1616

1717
// See webpack.common.config for adding modules common to both the webpack dev server and rails
1818

client/webpack.server.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const config = require('./webpack.common.config');
2+
3+
config.output = {
4+
filename: 'server-bundle.js',
5+
path: '../app/assets/javascripts/generated',
6+
7+
// CRITICAL for enabling Rails to find the globally exposed variables.
8+
libaryTarget: 'this',
9+
};
10+
11+
config.entry.push('./assets/javascripts/serverGlobals');
12+
13+
config.module.loaders.push(
14+
15+
{ loader: 'babel-loader' },
16+
17+
// require Resolve must go first
18+
// 1. React must be exposed (BOILERPLATE)
19+
{ test: require.resolve('react'), loader: 'expose?React' }
20+
);
21+
module.exports = config;

config/initializers/react_on_rails.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Shown below are the defaults for configuration
2+
ReactOnRails.configure do |config|
3+
# Client bundles are configured in application.js
4+
5+
# Server bundle is a single file for all server rendering of components.
6+
# If you wish to use render_js in your views without any file, set this to "" to avoid warnings.
7+
config.server_bundle_js_file = "app/assets/javascripts/generated/server-bundle.js" # This is the default
8+
9+
# Below options can be overriden by passing to the helper method.
10+
config.prerender = true # default is false
11+
config.generator_function = true # default is false, meaning that you expose ReactComponents directly
12+
config.trace = Rails.env.development? # default is true for development, off otherwise
13+
14+
# For server rendering. This can be set to false so that server side messages are discarded.
15+
config.replay_console = true # Default is true. Be cautious about turning this off.
16+
end

0 commit comments

Comments
 (0)