react_on_rails 13.3.4 → 13.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +2 -2
- data/.github/ISSUE_TEMPLATE/bug_report.md +1 -1
- data/.github/workflows/lint-js-and-ruby.yml +2 -2
- data/.github/workflows/main.yml +5 -5
- data/.github/workflows/rspec-package-specs.yml +1 -1
- data/CHANGELOG.md +86 -65
- data/Gemfile.development_dependencies +2 -3
- data/README.md +5 -0
- data/docs/additional-details/recommended-project-structure.md +6 -6
- data/docs/additional-details/tips-for-usage-with-sp6.md +15 -0
- data/docs/deployment/heroku-deployment.md +15 -11
- data/docs/getting-started.md +28 -27
- data/docs/guides/configuration.md +17 -17
- data/docs/guides/file-system-based-automated-bundle-generation.md +42 -50
- data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +10 -10
- data/docs/guides/how-react-on-rails-works.md +1 -1
- data/docs/guides/installation-into-an-existing-rails-app.md +4 -4
- data/docs/guides/rails-webpacker-react-integration-options.md +4 -4
- data/docs/guides/react-on-rails-overview.md +6 -7
- data/docs/guides/rspec-configuration.md +11 -11
- data/docs/guides/tutorial.md +11 -14
- data/docs/guides/upgrading-react-on-rails.md +2 -2
- data/docs/guides/webpack-configuration.md +14 -14
- data/docs/home.md +3 -3
- data/docs/javascript/asset-pipeline.md +4 -4
- data/docs/javascript/converting-from-custom-webpack-config-to-rails-webpacker-config.md +8 -8
- data/docs/javascript/server-rendering-tips.md +1 -1
- data/docs/javascript/troubleshooting-when-using-shakapacker.md +77 -0
- data/docs/outdated/rails-assets-relative-paths.md +1 -1
- data/docs/outdated/rails-assets.md +1 -1
- data/docs/react-on-rails-pro/react-on-rails-pro.md +1 -1
- data/lib/generators/react_on_rails/adapt_for_older_shakapacker_generator.rb +41 -0
- data/lib/generators/react_on_rails/base_generator.rb +3 -47
- data/lib/generators/react_on_rails/generator_messages.rb +40 -0
- data/lib/generators/react_on_rails/install_generator.rb +21 -0
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +2 -2
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +2 -2
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +2 -2
- data/lib/generators/react_on_rails/templates/base/base/config/{webpacker.yml → shakapacker.yml} +3 -3
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +3 -1
- data/lib/react_on_rails/helper.rb +23 -11
- data/lib/react_on_rails/packs_generator.rb +2 -0
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +1 -3
- data/lib/react_on_rails/version.rb +1 -1
- data/package.json +1 -1
- metadata +6 -3
data/docs/getting-started.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Note, the best way to understand how to use ReactOnRails is to study a few simple examples. You can do a quick demo setup, either on your existing app or on a new Rails app.
|
4
4
|
|
5
|
+
This documentation assumes the usage of ReactOnRails with Shakapacker 7. For installation on Shakapacker 6, check [tips for usage with Shakapacker 6](https://www.shakacode.com/react-on-rails/docs/aditional-details/tips-for-usage-with-sp6) first.
|
6
|
+
|
5
7
|
1. Do the quick [tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/).
|
6
8
|
2. Add React on Rails to an existing Rails app per [the instructions](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).
|
7
9
|
3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy), a simple, no DB example.
|
@@ -9,38 +11,37 @@ Note, the best way to understand how to use ReactOnRails is to study a few simpl
|
|
9
11
|
|
10
12
|
## Basic Installation
|
11
13
|
|
12
|
-
|
13
|
-
generated using `rails new my_app --javascript=webpack`). If your
|
14
|
-
application is not yet set up to use webpacker, please see
|
15
|
-
[the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).*
|
14
|
+
You need a Rails application with Shakapacker installed and configured on it. Check [Shakapacker documentation](https://github.com/shakacode/shakapacker) for more details but typically you need the following steps:
|
16
15
|
|
17
|
-
|
16
|
+
```bash
|
17
|
+
rails new PROJECT_NAME --skip-javascript
|
18
|
+
cd PROJECT_NAME
|
19
|
+
bundle add shakapacker --strict
|
20
|
+
rails shakapacker:install
|
21
|
+
```
|
18
22
|
|
19
|
-
|
20
|
-
bundle add shakapacker --strict
|
21
|
-
bundle add react_on_rails --strict
|
22
|
-
```
|
23
|
+
You may need to check [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/) if you have an already working Rails application.
|
23
24
|
|
24
|
-
|
25
|
+
1. Add the `react_on_rails` gem to Gemfile:
|
25
26
|
|
26
27
|
```bash
|
27
|
-
|
28
|
+
bundle add react_on_rails --strict
|
28
29
|
```
|
29
30
|
|
30
|
-
|
31
|
+
Commit this to git (or else you cannot run the generator in the next step unless you pass the option `--ignore-warnings`).
|
31
32
|
|
32
|
-
|
33
|
+
2. Run the install generator:
|
33
34
|
|
34
35
|
```bash
|
35
36
|
rails generate react_on_rails:install
|
36
37
|
```
|
37
38
|
|
38
|
-
|
39
|
+
3. Start the app:
|
39
40
|
|
40
41
|
- Run `./bin/dev` for HMR
|
41
42
|
- Run `./bin/dev-static` for statically created bundles (no HMR)
|
42
43
|
|
43
|
-
|
44
|
+
4. Visit http://localhost:3000/hello_world.
|
44
45
|
|
45
46
|
|
46
47
|
### Turning on server rendering
|
@@ -60,7 +61,7 @@ Below is the line where you turn server rendering on by setting `prerender` to t
|
|
60
61
|
```
|
61
62
|
|
62
63
|
Note, if you got an error in your console regarding "ReferenceError: window is not defined",
|
63
|
-
then you need to edit `config/
|
64
|
+
then you need to edit `config/shakapacker.yml` and set `hmr: false` and `inline: false`.
|
64
65
|
See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
|
65
66
|
issue.
|
66
67
|
|
@@ -69,18 +70,18 @@ issue.
|
|
69
70
|
### Configuration
|
70
71
|
|
71
72
|
* Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](https://www.shakacode.com/react-on-rails/docs/guides/configuration/) for documentation of all configuration options.
|
72
|
-
* Configure `config/
|
73
|
-
* Most apps should rely on the
|
73
|
+
* Configure `config/shakapacker.yml`. If you used the generator and the default Shakapacker setup, you don't need to touch this file. If you are customizing your setup, then consult the [spec/dummy/config/shakapacker.yml](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/shakapacker.yml) example or the official default [shakapacker.yml](https://github.com/shakacode/shakapacker/blob/master/lib/install/config/shakapacker.yml).
|
74
|
+
* Most apps should rely on the Shakapacker setup for Webpack. Shakapacker v6+ includes support for webpack version 5.
|
74
75
|
|
75
76
|
## Including your React Component on your Rails Views
|
76
77
|
|
77
|
-
- React
|
78
|
+
- React components are rendered via your Rails Views. Here's an ERB sample:
|
78
79
|
|
79
80
|
```erb
|
80
81
|
<%= react_component("HelloWorld", props: @some_props) %>
|
81
82
|
```
|
82
83
|
|
83
|
-
- **Server-Side Rendering**: Your
|
84
|
+
- **Server-Side Rendering**: Your React component is first rendered into HTML on the server. Use the **prerender** option:
|
84
85
|
|
85
86
|
```erb
|
86
87
|
<%= react_component("HelloWorld", props: @some_props, prerender: true) %>
|
@@ -146,13 +147,13 @@ For details on techniques to use different code for client and server rendering,
|
|
146
147
|
|
147
148
|
## Specifying Your React Components: Register directly or use render-functions
|
148
149
|
|
149
|
-
You have two ways to specify your React components. You can either register the React component (either function or class component) directly, or you can create a function that returns a React component, which we using the name of a "render-function". Creating a render-function allows:
|
150
|
+
You have two ways to specify your React components. You can either register the React component (either function or class component) directly, or you can create a function that returns a React component, which we using the name of a "render-function". Creating a render-function allows you to:
|
150
151
|
|
151
|
-
1.
|
152
|
-
2.
|
153
|
-
3.
|
152
|
+
1. Access to the `railsContext`. See the [documentation for the railsContext](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) in terms of why you might need it. You **need** a Render-Function to access the `railsContext`.
|
153
|
+
2. Use the passed-in props to initialize a redux store or set up `react-router`.
|
154
|
+
3. Return different components depending on what's in the props.
|
154
155
|
|
155
|
-
Note, the return value of a **Render-Function** should be either a React Function or Class Component
|
156
|
+
Note, the return value of a **Render-Function** should be either a React Function or Class Component or an object representing server rendering results.
|
156
157
|
|
157
158
|
**Do not return a React Element (JSX).**
|
158
159
|
|
@@ -174,9 +175,9 @@ Thus, there is no difference between registering a React Function Component or c
|
|
174
175
|
|
175
176
|
## react_component_hash for Render-Functions
|
176
177
|
|
177
|
-
Another reason to use a Render-Function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: { renderedHtml, redirectLocation, error }
|
178
|
+
Another reason to use a Render-Function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: `{ renderedHtml, redirectLocation, error }`. Make sure you use this function with `react_component_hash`.
|
178
179
|
|
179
|
-
For server rendering, if you wish to return multiple HTML strings from a Render-Function, you may return an Object from your Render-Function with a single top-level property of `renderedHtml`. Inside this Object, place a key called `componentHtml`, along with any other needed keys. An example scenario of this is when you are using side effects libraries like [React Helmet](https://github.com/nfl/react-helmet). Your Ruby code will get this Object as a Hash containing keys componentHtml and any other custom keys that you added:
|
180
|
+
For server rendering, if you wish to return multiple HTML strings from a Render-Function, you may return an Object from your Render-Function with a single top-level property of `renderedHtml`. Inside this Object, place a key called `componentHtml`, along with any other needed keys. An example scenario of this is when you are using side effects libraries like [React Helmet](https://github.com/nfl/react-helmet). Your Ruby code will get this Object as a Hash containing keys `componentHtml` and any other custom keys that you added:
|
180
181
|
|
181
182
|
```js
|
182
183
|
{ renderedHtml: { componentHtml, customKey1, customKey2} }
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Here is the full set of config options. This file is `/config/initializers/react_on_rails.rb`
|
2
2
|
|
3
|
-
First, you should have a `/config/
|
3
|
+
First, you should have a `/config/shakapacker.yml` setup.
|
4
4
|
|
5
|
-
Here is the setup when using the recommended `/client` directory for your node_modules and source files:
|
5
|
+
Here is the setup when using the recommended `/client` directory for your `node_modules` and source files:
|
6
6
|
|
7
7
|
```yaml
|
8
8
|
# Note: Base output directory of /public is assumed for static files
|
@@ -23,7 +23,7 @@ development:
|
|
23
23
|
|
24
24
|
test:
|
25
25
|
<<: *default
|
26
|
-
# Ensure that
|
26
|
+
# Ensure that shakapacker invokes webpack to build files for tests if not using the
|
27
27
|
# ReactOnRails rspec helper.
|
28
28
|
compile: true
|
29
29
|
|
@@ -69,19 +69,19 @@ ReactOnRails.configure do |config|
|
|
69
69
|
config.random_dom_id = true # default
|
70
70
|
|
71
71
|
# defaults to "" (top level)
|
72
|
-
config.node_modules_location = "client" # If using
|
72
|
+
config.node_modules_location = "client" # If using Shakapacker you should use "".
|
73
73
|
|
74
|
-
# If you're using the standard
|
74
|
+
# If you're using the standard Shakapacker configuration of webpack, then Shakapacker
|
75
75
|
# will automatically modify or create an assets:precompile task to build your assets. If so,
|
76
76
|
# set this value to nil. Alternatively, you can specify `config.build_production_command`
|
77
77
|
# to have react_on_rails invoke a command for you during assets:precompile.
|
78
78
|
# The command is either a script or a module containing a class method `call`
|
79
79
|
# In this example, the module BuildProductionCommand would have a class method `call`.
|
80
|
-
config.build_production_command = "RAILS_ENV=production bin/
|
80
|
+
config.build_production_command = "RAILS_ENV=production bin/shakapacker"
|
81
81
|
|
82
82
|
# NOTE:
|
83
|
-
# When specifying `build_production_command`, you need to disable `rails/
|
84
|
-
# configuration by setting `
|
83
|
+
# When specifying `build_production_command`, you need to disable `rails/shakapacker`
|
84
|
+
# configuration by setting `shakapacker_precompile: false` in your `config/shakapacker.yml` file.
|
85
85
|
|
86
86
|
# See bottom for an example of the BuildProductionCommand module.
|
87
87
|
# config.build_production_command = BuildProductionCommand
|
@@ -118,8 +118,8 @@ ReactOnRails.configure do |config|
|
|
118
118
|
# Normally, you have different bundles for client and server, thus, the default is false.
|
119
119
|
# Furthermore, if you are not hashing the server bundle (not in the manifest.json), then React on Rails
|
120
120
|
# will only look for the server bundle to be created in the typical file location, typically by
|
121
|
-
# a `
|
122
|
-
# If true, ensure that in config/
|
121
|
+
# a `shakapacker --watch` process.
|
122
|
+
# If true, ensure that in config/shakapacker.yml that you have both dev_server.hmr and
|
123
123
|
# dev_server.inline set to false.
|
124
124
|
config.same_bundle_for_client_and_server = false
|
125
125
|
|
@@ -220,9 +220,9 @@ ReactOnRails.configure do |config|
|
|
220
220
|
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
221
221
|
#
|
222
222
|
# NOTE:
|
223
|
-
# Instead of using this test helper, you may ensure fresh test files using
|
224
|
-
# 1. Have `config/
|
225
|
-
# 2. Set the compile option to true in config/
|
223
|
+
# Instead of using this test helper, you may ensure fresh test files using Shakapacker via:
|
224
|
+
# 1. Have `config/webpack/test.js` exporting an array of objects to configure both client and server bundles.
|
225
|
+
# 2. Set the compile option to true in config/shakapacker.yml for env test
|
226
226
|
################################################################################
|
227
227
|
|
228
228
|
# If you are using this in your spec_helper.rb (or rails_helper.rb):
|
@@ -232,15 +232,15 @@ ReactOnRails.configure do |config|
|
|
232
232
|
# with rspec then this controls what yarn command is run
|
233
233
|
# to automatically refresh your webpack assets on every test run.
|
234
234
|
#
|
235
|
-
config.build_test_command = "RAILS_ENV=test bin/
|
235
|
+
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
|
236
236
|
|
237
237
|
# CONFIGURE YOUR SOURCE FILES
|
238
238
|
# The test helper needs to know where your JavaScript files exist. The value is configured
|
239
|
-
# by your config/
|
239
|
+
# by your config/shakapacker.yml source_path:
|
240
240
|
# source_path: client/app # if using recommended /client directory
|
241
241
|
#
|
242
242
|
# Define the files we need to check for webpack compilation when running tests.
|
243
|
-
# The default is `%w( manifest.json )` as will be sufficient for most
|
243
|
+
# The default is `%w( manifest.json )` as will be sufficient for most shakapacker builds.
|
244
244
|
# However, if you are generating a server bundle that is NOT hashed (present in manifest.json),
|
245
245
|
# then include the file in this list like this:
|
246
246
|
config.webpack_generated_files = %w( server-bundle.js manifest.json )
|
@@ -267,7 +267,7 @@ module BuildProductionCommand
|
|
267
267
|
include FileUtils
|
268
268
|
# The call method will be called during assets:precompile
|
269
269
|
def self.call
|
270
|
-
sh "bin/
|
270
|
+
sh "bin/shakapacker"
|
271
271
|
end
|
272
272
|
end
|
273
273
|
```
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# File-System-Based Automated Bundle Generation
|
2
2
|
|
3
|
-
To use the automated bundle generation feature introduced in React on Rails v13.1.0, please upgrade to use [Shakapacker v6.5.1](https://github.com/shakacode/shakapacker/tree/v6.5.1) at least. If you are currently using
|
3
|
+
To use the automated bundle generation feature introduced in React on Rails v13.1.0, please upgrade to use [Shakapacker v6.5.1](https://github.com/shakacode/shakapacker/tree/v6.5.1) at least. If you are currently using Webpacker, please follow the migration steps available [v6 upgrade](https://github.com/shakacode/shakapacker/blob/master/docs/v6_upgrade.md). Then upgrade to Shakapacker 7 using [v7 upgrade](https://github.com/shakacode/shakapacker/blob/master/docs/v7_upgrade.md) guide.
|
4
4
|
|
5
5
|
## Configuration
|
6
6
|
|
7
7
|
### Enable nested_entries for Shakapacker
|
8
|
-
To use the automated bundle generation feature, set `nested_entries: true` in the `
|
8
|
+
To use the automated bundle generation feature, set `nested_entries: true` in the `shakapacker.yml` file like this.
|
9
9
|
The generated files will go in a nested directory.
|
10
10
|
|
11
11
|
```yml
|
@@ -44,14 +44,14 @@ React on Rails automatically generates pack files for components to be registere
|
|
44
44
|
app/javascript/packs/generated
|
45
45
|
```
|
46
46
|
|
47
|
-
*Note: the directory might be different depending on the `source_entry_path` in `config/
|
47
|
+
*Note: the directory might be different depending on the `source_entry_path` in `config/shakapacker.yml`.*
|
48
48
|
|
49
49
|
## Usage
|
50
50
|
|
51
51
|
### Basic usage
|
52
52
|
|
53
53
|
#### Background
|
54
|
-
If the `
|
54
|
+
If the `shakapacker.yml` file is configured as instructed [here](https://github.com/shakacode/shakapacker#configuration-and-code), with the following configurations
|
55
55
|
|
56
56
|
```yml
|
57
57
|
default: &default
|
@@ -131,56 +131,56 @@ For example, if you wanted to utilize our file-system based entrypoint generatio
|
|
131
131
|
1. Remove generated entrypoints from parameters passed directly to `javascript_pack_tag` and `stylesheet_pack_tag`.
|
132
132
|
2. Remove generated entrypoints from parameters passed directly to `append_javascript_pack_tag` and `append_stylesheet_pack_tag`.
|
133
133
|
|
134
|
-
Your layout would now contain:
|
134
|
+
Your layout would now contain:
|
135
135
|
|
136
|
-
```erb
|
137
|
-
|
138
|
-
|
139
|
-
```
|
136
|
+
```erb
|
137
|
+
<%= javascript_pack_tag('BarComponentTwo') %>
|
138
|
+
<%= stylesheet_pack_tag('BarComponentTwo') %>
|
139
|
+
```
|
140
140
|
|
141
141
|
3. Create a directory structure where the components that you want to be auto-generated are within `ReactOnRails.configuration.components_subdirectory`, which should be a subdirectory of `Shakapacker.config.source_path`:
|
142
142
|
|
143
|
-
```
|
144
|
-
app/javascript:
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
```
|
143
|
+
```
|
144
|
+
app/javascript:
|
145
|
+
└── packs:
|
146
|
+
│ └── BarComponentTwo.jsx # Internally uses ReactOnRails.register
|
147
|
+
└── src:
|
148
|
+
│ └── Foo
|
149
|
+
│ │ └── ...
|
150
|
+
│ │ └── ror_components # configured as `components_subdirectory`
|
151
|
+
│ │ └── FooComponentOne.jsx
|
152
|
+
│ └── Bar
|
153
|
+
│ │ └── ...
|
154
|
+
│ │ └── ror_components # configured as `components_subdirectory`
|
155
|
+
│ │ │ └── BarComponentOne.jsx
|
156
|
+
│ │ └── something_else
|
157
|
+
│ │ │ └── BarComponentTwo.jsx
|
158
|
+
```
|
159
159
|
|
160
160
|
4. You no longer need to register the React components within the `ReactOnRails.configuration.components_subdirectory` nor directly add their bundles. For example you can have a Rails view using three components:
|
161
161
|
|
162
|
-
```erb
|
163
|
-
<% append_javascript_pack('BarComponentTwo')
|
162
|
+
```erb
|
163
|
+
<% append_javascript_pack('BarComponentTwo') %>
|
164
164
|
<%= react_component("FooComponentOne", {}, auto_load_bundle: true) %>
|
165
165
|
<%= react_component("BarComponentOne", {}, auto_load_bundle: true) %>
|
166
166
|
<%= react_component("BarComponentTwo", {}) %>
|
167
|
-
```
|
167
|
+
```
|
168
168
|
|
169
|
-
If a component uses multiple HTML strings for server rendering, the [`react_component_hash`](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api/#react_component_hash) view helper can be used on the Rails view, as illustrated below.
|
169
|
+
If a component uses multiple HTML strings for server rendering, the [`react_component_hash`](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api/#react_component_hash) view helper can be used on the Rails view, as illustrated below.
|
170
170
|
|
171
|
-
```erb
|
172
|
-
<% foo_component_one_data = react_component_hash("FooComponentOne",
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
<% content_for :title do %>
|
178
|
-
|
179
|
-
<% end %>
|
180
|
-
<%= foo_component_one_data["componentHtml"] %>
|
181
|
-
```
|
171
|
+
```erb
|
172
|
+
<% foo_component_one_data = react_component_hash("FooComponentOne",
|
173
|
+
prerender: true,
|
174
|
+
auto_load_bundle: true
|
175
|
+
props: {}
|
176
|
+
) %>
|
177
|
+
<% content_for :title do %>
|
178
|
+
<%= foo_component_one_data['title'] %>
|
179
|
+
<% end %>
|
180
|
+
<%= foo_component_one_data["componentHtml"] %>
|
181
|
+
```
|
182
182
|
|
183
|
-
The default value of the `auto_load_bundle` parameter can be specified by setting `config.auto_load_bundle` in `config/initializers/react_on_rails.rb` and thus removed from each call to `react_component`.
|
183
|
+
The default value of the `auto_load_bundle` parameter can be specified by setting `config.auto_load_bundle` in `config/initializers/react_on_rails.rb` and thus removed from each call to `react_component`.
|
184
184
|
|
185
185
|
### Server Rendering and Client Rendering Components
|
186
186
|
|
@@ -190,16 +190,8 @@ Once generated, all server entrypoints will be imported into a file named `[Reac
|
|
190
190
|
|
191
191
|
*Note: If specifying separate definitions for client and server rendering, please make sure to delete the generalized `ComponentName.jsx` file.*
|
192
192
|
|
193
|
-
### Integrating auto-bundling into CI workflows
|
194
|
-
|
195
|
-
Currently, ReactOnRails contains conditional logic that checks for the existence of generated entrypoint files whenever `react_component` or one of its derivative methods are called. If a generated entrypoint of the same name as provided to `react_component` or `react_component_hash` exists, then an `append_javascript_pack` call is made automatically.
|
196
|
-
|
197
|
-
This means that `rake react_on_rails:generate_packs` or its programmatic equivalent must be run as a prerequisite to any sort of test or spec that would result in `react_component` or `react_component_hash` being called, even if the generated entrypoint files have already been bundled in a previous workflow/job.
|
198
|
-
|
199
|
-
Caching of the generated entrypoints between workflow/jobs should also resolve this issue.
|
200
|
-
|
201
193
|
### Using Automated Bundle Generation Feature with already defined packs
|
202
194
|
|
203
|
-
|
195
|
+
As of version 13.3.4, bundles inside of directories that match `config.components_subdirectory` will be automatically added as entrypoints, while bundles outside of those directories will have to be manually added to the Shakapacker.config.source_entry_path or Webpack's `entry` rules.
|
204
196
|
|
205
197
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# HMR and Hot Reloading with the webpack-dev-server
|
2
2
|
|
3
|
-
The webpack-dev-server provides:
|
3
|
+
The `webpack-dev-server` provides:
|
4
4
|
|
5
5
|
1. Speedy compilation of client side assets
|
6
6
|
2. Optional HMR which means that the page will reload automatically when after
|
@@ -11,17 +11,17 @@ The webpack-dev-server provides:
|
|
11
11
|
For use with webpack, see **Client Side rendering and HMR using react-refresh-webpack-plugin** section bellow or visit [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) for additional details.
|
12
12
|
|
13
13
|
If you are ***not*** using server-side rendering (***not*** using `prerender: true`),
|
14
|
-
then you can follow all the regular docs for using the `bin/
|
14
|
+
then you can follow all the regular docs for using the `bin/shakapacker-dev-server`
|
15
15
|
during development.
|
16
16
|
|
17
|
-
# Server Side Rendering with the Default
|
17
|
+
# Server Side Rendering with the Default shakacode/shakapacker bin/shakapacker-dev-server
|
18
18
|
|
19
19
|
If you are using server-side rendering, then you have a couple options. The
|
20
20
|
recommended technique is to have a different webpack configuration for server
|
21
21
|
rendering.
|
22
22
|
|
23
23
|
## If you use the same Webpack setup for your server and client bundles
|
24
|
-
If you do use the webpack-dev-server for prerendering, be sure to set the
|
24
|
+
If you do use the `webpack-dev-server` for prerendering, be sure to set the
|
25
25
|
`config/initializers/react_on_rails.rb` setting of
|
26
26
|
|
27
27
|
```
|
@@ -36,13 +36,13 @@ This must also be false.
|
|
36
36
|
|
37
37
|
If you don't configure these two to false, you'll see errors like:
|
38
38
|
|
39
|
-
* "ReferenceError: window is not defined" (if hmr is true)
|
40
|
-
* "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
|
39
|
+
* `"ReferenceError: window is not defined" (if hmr is true)`
|
40
|
+
* `"TypeError: Cannot read property 'prototype' of undefined" (if inline is true)`
|
41
41
|
|
42
42
|
# Client Side rendering with HMR using react-refresh-webpack-plugin
|
43
43
|
## Basic installation
|
44
|
-
To enable HMR functionality you have to use `./bin/
|
45
|
-
1. In `config/
|
44
|
+
To enable HMR functionality you have to use `./bin/shakapacker-dev-server`
|
45
|
+
1. In `config/shakapacker.yml` set **hmr** and **inline** `dev_server` properties to true.
|
46
46
|
```
|
47
47
|
dev_server:
|
48
48
|
https: false
|
@@ -57,7 +57,7 @@ To enable HMR functionality you have to use `./bin/webpacker-dev-server`
|
|
57
57
|
2. Add react refresh packages:
|
58
58
|
` yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
|
59
59
|
|
60
|
-
3. HMR is for use with the webpack-dev-server
|
60
|
+
3. HMR is for use with the `webpack-dev-server`, so we only add this for the `webpack-dev-server`.
|
61
61
|
```
|
62
62
|
const { devServer } = require('shakapacker')
|
63
63
|
|
@@ -75,7 +75,7 @@ To enable HMR functionality you have to use `./bin/webpacker-dev-server`
|
|
75
75
|
)
|
76
76
|
}
|
77
77
|
```
|
78
|
-
We added overlay.sockedPort option in `ReactRefreshWebpackPlugin` to match the webpack dev-server port specified in config/
|
78
|
+
We added overlay.sockedPort option in `ReactRefreshWebpackPlugin` to match the webpack dev-server port specified in `config/shakapacker.yml`. Thats way we make sockjs works properly and suppress error in browser console `GET http://localhost:[port]/sockjs-node/info?t=[xxxxxxxxxx] 404 (Not Found)`.
|
79
79
|
|
80
80
|
4. Add react-refresh plugin in `babel.config.js`
|
81
81
|
```
|
@@ -32,7 +32,7 @@ Each time you change your client code, you will need to re-generate the bundles
|
|
32
32
|
|
33
33
|
For example, you might create a [Procfile.dev](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/Procfile.dev).
|
34
34
|
|
35
|
-
On production deployments that use asset precompilation, such as Heroku deployments, `shakapacker`, by default, will automatically run webpack to build your JavaScript bundles, running the command `bin/
|
35
|
+
On production deployments that use asset precompilation, such as Heroku deployments, `shakapacker`, by default, will automatically run webpack to build your JavaScript bundles, running the command `bin/shakapacker` in your app.
|
36
36
|
|
37
37
|
However, if you want to run a custom command to run webpack to build your bundles, then you will:
|
38
38
|
1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](https://www.shakacode.com/react-on-rails/docs/guides/configuration/)
|
@@ -7,21 +7,21 @@
|
|
7
7
|
1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `package.json` file.
|
8
8
|
|
9
9
|
```ruby
|
10
|
+
gem "shakapacker", "7.0.1" # Use the latest and the exact version
|
10
11
|
gem "react_on_rails", "13.3.1" # Use the latest and the exact version
|
11
|
-
gem "shakapacker", "6.5.5"
|
12
12
|
```
|
13
13
|
|
14
14
|
Or use `bundle add`:
|
15
15
|
|
16
16
|
```bash
|
17
|
+
bundle add shakapacker --version=7.0.1 --strict
|
17
18
|
bundle add react_on_rails --version=13.3.1 --strict
|
18
|
-
bundle add shakapacker --version=6.5.5 --strict
|
19
19
|
```
|
20
20
|
|
21
|
-
2. Run the following 2 commands to install
|
21
|
+
2. Run the following 2 commands to install Shakapacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install Webpacker with React per the instructions [here](https://github.com/rails/webpacker).
|
22
22
|
|
23
23
|
```bash
|
24
|
-
rails
|
24
|
+
rails shakapacker:install
|
25
25
|
```
|
26
26
|
|
27
27
|
3. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
@@ -5,7 +5,7 @@ have your app make a second round trip to the Rails server to get initialization
|
|
5
5
|
|
6
6
|
**Server-Side Rendering (SSR)** results in Rails rendering HTML for your React components. The main reasons to use SSR are better SEO and pages display more quickly.
|
7
7
|
|
8
|
-
These gems provide advanced integration of React with [
|
8
|
+
These gems provide advanced integration of React with [shakacode/shakapacker](https://github.com/shakacode/shakapacker):
|
9
9
|
|
10
10
|
| Gem | Props Hydration | Server-Side-Rendering (SSR) | SSR with HMR | SSR with React-Router | SSR with Code Splitting | Node SSR |
|
11
11
|
| --- | --------------- | --- | --------------------- | ----------------------| ------------------------|----|
|
@@ -89,9 +89,9 @@ module.exports = commonWebpackConfig;
|
|
89
89
|
## HMR and React Hot Reloading
|
90
90
|
|
91
91
|
Before turning HMR on, consider upgrading to the latest stable gems and packages:
|
92
|
-
https://github.com/
|
92
|
+
https://github.com/shakacode/shakapacker#upgrading
|
93
93
|
|
94
|
-
Configure `config/
|
94
|
+
Configure `config/shakapacker.yml` file:
|
95
95
|
|
96
96
|
```yaml
|
97
97
|
development:
|
@@ -101,7 +101,7 @@ development:
|
|
101
101
|
inline: true
|
102
102
|
```
|
103
103
|
|
104
|
-
This basic configuration alone will have HMR working with the default
|
104
|
+
This basic configuration alone will have HMR working with the default Shakapacker setup. However, a code save will trigger a full page refresh each time you save a file.
|
105
105
|
|
106
106
|
Webpack's HMR allows the replacement of modules for React in-place without reloading the browser. To do this, you have two options:
|
107
107
|
|
@@ -6,15 +6,13 @@ React on Rails integrates Rails with (server rendering of) Facebook's [React](ht
|
|
6
6
|
|
7
7
|
# Project Objective
|
8
8
|
|
9
|
-
To provide a high performance framework for integrating Ruby on Rails with React via the [**
|
9
|
+
To provide a high performance framework for integrating Ruby on Rails with React via the [**Shakapacker**](https://github.com/shakacode/shakapacker) gem especially in regards to React Server-Side Rendering for better SEO and improved performance.
|
10
10
|
|
11
11
|
# Features and Why React on Rails?
|
12
12
|
|
13
|
-
Given that [`rails/webpacker`](https://github.com/rails/webpacker/) gem already provides basic React integration, why would you use "React on Rails"?
|
14
|
-
|
15
13
|
1. Easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate request to your API.
|
16
|
-
1. Tight integration with [
|
17
|
-
1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `
|
14
|
+
1. Tight integration with [shakacode/shakapacker](https://github.com/shakacode/shakapacker).
|
15
|
+
1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `shakacode/shakapacker`.
|
18
16
|
1. Support for HMR for a great developer experience.
|
19
17
|
1. Supports latest versions of React with hooks.
|
20
18
|
1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration including server-side-rendering.
|
@@ -23,8 +21,9 @@ Given that [`rails/webpacker`](https://github.com/rails/webpacker/) gem already
|
|
23
21
|
1. [ReScript (Reason ML) Support](https://github.com/shakacode/reason-react-on-rails-example).
|
24
22
|
|
25
23
|
See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
|
24
|
+
|
26
25
|
----
|
27
26
|
|
28
27
|
## Prerequisites
|
29
|
-
|
30
|
-
|
28
|
+
- Ruby on Rails >=5
|
29
|
+
- Shakapacker 6.5.1+.
|
@@ -1,29 +1,29 @@
|
|
1
1
|
# RSpec Configuration
|
2
2
|
_Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/guides/minitest-configuration/)_
|
3
3
|
|
4
|
-
# If your webpack configurations correspond to
|
4
|
+
# If your webpack configurations correspond to Shakapacker's default setup
|
5
5
|
If you're able to configure your webpack configuration to be run by having your webpack configuration
|
6
6
|
returned by the files in `/config/webpack`, then you have 2 options to ensure that your files are
|
7
7
|
compiled by webpack before running tests and during production deployment:
|
8
8
|
|
9
|
-
1. **Use
|
10
|
-
environments. Ensure that your `source_path` is correct, or else `
|
9
|
+
1. **Use Shakapacker's compile option**: Configure your `config/shakapacker.yml` so that `compile: true` is for `test` and `production`
|
10
|
+
environments. Ensure that your `source_path` is correct, or else `Shakapacker` won't correctly
|
11
11
|
detect changes.
|
12
12
|
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
|
13
13
|
|
14
14
|
```yml
|
15
|
-
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/
|
15
|
+
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/shakapacker"
|
16
16
|
```
|
17
17
|
|
18
|
-
Which should you use? If you're already using the `
|
19
|
-
you can keep things simple and use the `
|
18
|
+
Which should you use? If you're already using the `Shakapacker` way to configure webpack, then
|
19
|
+
you can keep things simple and use the `Shakapacker` options.
|
20
20
|
|
21
21
|
# Checking for stale assets using React on Rails
|
22
22
|
|
23
23
|
Because you will probably want to run RSpec tests that rely on compiled webpack assets (typically, your integration/feature specs where `js: true`), you will want to ensure you don't accidentally run tests on missing or stale webpack assets. If you did use stale Webpack assets, you will get invalid test results as your tests do not use the very latest JavaScript code.
|
24
24
|
|
25
|
-
As mentioned above, you can configure `compile: true` in `config/
|
26
|
-
your webpack in the standard `
|
25
|
+
As mentioned above, you can configure `compile: true` in `config/shakapacker.yml` _if_ you've got configuration for
|
26
|
+
your webpack in the standard `Shakapacker` spot of `config/webpack/<NODE_ENV>.js`
|
27
27
|
|
28
28
|
ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/test_helper.rb) for more details. You can customize this to your particular needs by replacing any of the default components used by `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`.
|
29
29
|
|
@@ -44,13 +44,13 @@ If you are using Webpack to build CSS assets, you should do something like this
|
|
44
44
|
```
|
45
45
|
|
46
46
|
Please take note of the following:
|
47
|
-
- If you are using
|
47
|
+
- If you are using Shakapacker, be **SURE** to configure the `source_path` in your `config/shakapacker.yml` unless you are using the defaults for Shakapacker.
|
48
48
|
|
49
|
-
- This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using
|
49
|
+
- This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using Shakapacker, the default value will come from the `config/shakapacker.yml` value for the `public_output_path` and the `source_path`
|
50
50
|
|
51
51
|
- If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
|
52
52
|
|
53
|
-
- By default, the webpack processes look in the webpack generated files folder, configured via the `config/
|
53
|
+
- By default, the webpack processes look in the webpack generated files folder, configured via the `config/shakapacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
|
54
54
|
|
55
55
|
The following `config/react_on_rails.rb` settings **must** match your setup:
|
56
56
|
```ruby
|