Skip to content

Commit c248b9f

Browse files
authored
Merge pull request #471 from reactjs/ssr-js-refactor
refactor(ServerRendering) simpler server rendering assets
2 parents 1647845 + 81cc0c9 commit c248b9f

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ where you will put your components
5151
//= require react_ujs
5252
//= require components
5353
```
54+
- create a `server_rendering.js` manifest file and precompile it with `config/initializers/react_server_rendering.rb`. (Use `--skip-server-rendering` if you don't want this.)
5455

5556
## Usage
5657

@@ -160,8 +161,8 @@ _(It will also be mounted by the UJS on page load.)_
160161

161162
There are some requirements for this to work:
162163

163-
- `react-rails` must load your code. By convention, it uses `components.js`, which was created
164-
by the install task. This file must include your components _and_ their dependencies (eg, Underscore.js).
164+
- `react-rails` must load your code. By convention, it uses `server_rendering.js`, which was created
165+
by the install task. This file must include React, ReactDOMServer, your components _and_ their dependencies (eg, Underscore.js).
165166
- Your components must be accessible in the global scope.
166167
If you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:
167168

@@ -185,7 +186,7 @@ MyApp::Application.configure do
185186
config.react.server_renderer_timeout ||= 20 # seconds
186187
config.react.server_renderer = React::ServerRendering::SprocketsRenderer
187188
config.react.server_renderer_options = {
188-
files: ["react-server.js", "components.js"], # files to load for prerendering
189+
files: ["server_rendering.js"], # files to load for prerendering
189190
replay_console: true, # if true, console.* will be replayed client-side
190191
}
191192
end

lib/generators/react/install_generator.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ class InstallGenerator < ::Rails::Generators::Base
1111
default: false,
1212
desc: 'Skip Git keeps'
1313

14+
class_option :skip_server_rendering,
15+
type: :boolean,
16+
default: false,
17+
desc: "Don't generate server_rendering.js or config/initializers/react_server_rendering.rb"
18+
1419
def create_directory
1520
empty_directory 'app/assets/javascripts/components'
16-
create_file 'app/assets/javascripts/components/.gitkeep' unless options[:skip_git]
21+
if !options[:skip_git]
22+
create_file 'app/assets/javascripts/components/.gitkeep'
23+
end
1724
end
1825

1926
def inject_react
@@ -48,6 +55,14 @@ def create_components
4855
create_file components_file, components_js
4956
end
5057

58+
def create_server_rendering
59+
return if options[:skip_server_rendering]
60+
manifest_path = "app/assets/javascripts/server_rendering.js"
61+
template("server_rendering.js", manifest_path)
62+
initializer_path = "config/initializers/react_server_rendering.rb"
63+
template("react_server_rendering.rb", initializer_path)
64+
end
65+
5166
private
5267

5368
def manifest
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# To render React components in production, precompile the server rendering manifest:
2+
Rails.application.config.assets.precompile += ["server_rendering.js"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//= require react-server
2+
//= require ./components
3+
//
4+
// By default, this file is loaded for server-side rendering.
5+
// It should require your components and any dependencies.

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SprocketsRenderer < ExecJSRenderer
1616

1717
def initialize(options={})
1818
@replay_console = options.fetch(:replay_console, true)
19-
filenames = options.fetch(:files, ["react-server.js", "components.js"])
19+
filenames = options.fetch(:files, ["server_rendering.js"])
2020
js_code = CONSOLE_POLYFILL.dup
2121
js_code << TIMEOUT_POLYFILL.dup
2222
js_code << options.fetch(:code, '')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//= require react-server
2+
//= require ./components

test/generators/install_generator_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ def copy_directory(dir)
6262
assert_application_file_modified
6363
end
6464

65+
test "creates server_rendering.js with default requires" do
66+
run_generator
67+
server_rendering_file_path = "app/assets/javascripts/server_rendering.js"
68+
assert_file server_rendering_file_path, %r{//= require react-server\n}
69+
assert_file server_rendering_file_path, %r{//= require ./components\n}
70+
end
71+
72+
test "creates server rendering initializer" do
73+
run_generator
74+
initializer_path = "config/initializers/react_server_rendering.rb"
75+
assert_file(initializer_path, %r{Rails.application.config.assets.precompile \+= \["server_rendering.js"\]})
76+
end
77+
78+
test "skipping server rendering" do
79+
run_generator %w(--skip-server-rendering)
80+
assert_no_file "config/initializers/react_server_rendering.rb"
81+
assert_no_file "app/assets/javascripts/server_rendering.js"
82+
end
83+
6584
def init_application_js(content)
6685
FileUtils.mkdir_p destination_root + '/app/assets/javascripts/'
6786
File.write destination_root + '/app/assets/javascripts/application.js', content

0 commit comments

Comments
 (0)