react_on_rails 13.3.0 → 13.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +5 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +1 -1
- data/.github/PULL_REQUEST_TEMPLATE.md +19 -0
- data/.github/workflows/lint-js-and-ruby.yml +2 -1
- data/.github/workflows/main.yml +17 -18
- data/.github/workflows/rspec-package-specs.yml +1 -0
- data/.prettierignore +3 -1
- data/CHANGELOG.md +106 -56
- data/Gemfile.development_dependencies +2 -2
- data/README.md +9 -1
- 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 +29 -19
- data/docs/guides/deployment.md +3 -2
- data/docs/guides/file-system-based-automated-bundle-generation.md +57 -51
- data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +10 -10
- data/docs/guides/how-react-on-rails-works.md +6 -6
- data/docs/guides/how-to-conditionally-server-render-based-on-device-type.md +2 -1
- data/docs/guides/installation-into-an-existing-rails-app.md +10 -8
- data/docs/guides/rails-webpacker-react-integration-options.md +34 -4
- data/docs/guides/react-on-rails-overview.md +6 -7
- data/docs/guides/rspec-configuration.md +11 -11
- data/docs/guides/tutorial.md +89 -83
- 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/bin/dev +2 -5
- data/lib/generators/react_on_rails/bin/dev-static +2 -5
- data/lib/generators/react_on_rails/generator_messages.rb +40 -0
- data/lib/generators/react_on_rails/install_generator.rb +30 -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/configuration.rb +16 -16
- data/lib/react_on_rails/helper.rb +26 -27
- data/lib/react_on_rails/packs_generator.rb +12 -5
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +1 -3
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +1 -1
- data/lib/react_on_rails/test_helper.rb +10 -1
- data/lib/react_on_rails/utils.rb +0 -4
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +8 -3
- data/package.json +1 -1
- data/rakelib/dummy_apps.rake +6 -1
- data/rakelib/run_rspec.rake +1 -15
- metadata +7 -3
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# How React on Rails Works (with
|
|
1
|
+
# How React on Rails Works (with Shakapcker)
|
|
2
2
|
|
|
3
3
|
*Note, older versions of React on Rails pushed the Webpack bundles through the Asset Pipeline. This older method has *many* disadvantages, such as broken sourcemaps, performance issues, etc. If you need help migrating to the current way of bypassing the Asset Pipeline, [email Justin](mailto:justin@shakacode.com).*
|
|
4
4
|
|
|
5
|
-
Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [
|
|
5
|
+
Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [Shakapacker](https://github.com/shakacode/shakapacker) provides view helpers to access the Webpack-generated (and fingerprinted) JS and CSS. These files totally skip the Rails asset pipeline. You are responsible for properly configuring your Webpack output. You will either use the standard Webpack configuration (*recommended*) or the `shakapacker` setup for Webpack.
|
|
6
6
|
|
|
7
7
|
Ensure these generated bundle files are in your `.gitignore`, as you never want to add the large compiled bundles to git.
|
|
8
8
|
|
|
@@ -14,7 +14,7 @@ Optionally, you can also initialize a Redux store with the view or controller he
|
|
|
14
14
|
|
|
15
15
|
In most cases, you should use the `prerender: false` (default behavior) with the provided `react_component` helper method to render the React component from your Rails views. In some cases, such as when SEO is vital, or many users will not have JavaScript enabled, you can enable server-rendering by passing `prerender: true` to your helper, or you can simply change the default in `config/initializers/react_on_rails`.
|
|
16
16
|
|
|
17
|
-
Now the server will interpret your JavaScript. The default is to use [ExecJS](https://github.com/rails/execjs) and pass the resulting HTML to the client. If you want to maximize the
|
|
17
|
+
Now the server will interpret your JavaScript. The default is to use [ExecJS](https://github.com/rails/execjs) and pass the resulting HTML to the client. If you want to maximize the performance of your server rendering, then you want to use React on Rails Pro which uses NodeJS to do the server rendering. See the [docs for React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
|
18
18
|
|
|
19
19
|
## HTML Source Code
|
|
20
20
|
|
|
@@ -22,17 +22,17 @@ If you open the HTML source of any web page using React on Rails, you'll see the
|
|
|
22
22
|
|
|
23
23
|
1. The wrapper div `<div id="HelloWorld-react-component-0">` specifies the div where to place the React rendering. It encloses the server-rendered HTML for the React component. If server rendering is not used (prerender: false), then the major difference is that the HTML rendered for the React component only contains the outer div: `<div id="HelloWorld-react-component-0"/>`. The first specification of the React component is just the same.
|
|
24
24
|
1. A script tag containing the properties of the React component, such as the registered name and any props. A JavaScript function runs after the page loads, using this data to build and initialize your React components.
|
|
25
|
-
1. Additional JavaScript is placed to console-log any messages, such as server rendering errors. Note: these server
|
|
25
|
+
1. Additional JavaScript is placed to console-log any messages, such as server rendering errors. Note: these server-side logs can be configured only to be sent to the server logs.
|
|
26
26
|
|
|
27
27
|
You can see all this on the source for [reactrails.com](https://www.reactrails.com/)
|
|
28
28
|
|
|
29
29
|
## Building the Bundles
|
|
30
30
|
|
|
31
|
-
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes.
|
|
31
|
+
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes. Alternatively, the `shakapacker` library also can ensure that your bundles are built.
|
|
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, `
|
|
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,6 +7,7 @@ However, sometimes we want to have layouts sufficiently different that we can't
|
|
|
7
7
|
Here's an example:
|
|
8
8
|
|
|
9
9
|
## config/initializers/react_on_rails.rb
|
|
10
|
+
|
|
10
11
|
```ruby
|
|
11
12
|
module RenderingExtension
|
|
12
13
|
# Return a Hash that contains custom values from the view context that will get passed to
|
|
@@ -34,6 +35,6 @@ ReactOnRails.configure do |config|
|
|
|
34
35
|
end
|
|
35
36
|
```
|
|
36
37
|
|
|
37
|
-
Note, full details of the React on Rails
|
|
38
|
+
Note, full details of the React on Rails configuration are [here in docs/basics/configuration.md](https://shakacode.com/react-on-rails/docs/guides/configuration/).
|
|
38
39
|
|
|
39
40
|
See the doc file [docs/basics/generator-functions-and-railscontext.md](https://shakacode.com/react-on-rails/docs/guides/generator-functions-and-railscontext/#rails-context) for how your client-side code uses the device information
|
|
@@ -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 "
|
|
11
|
-
gem "
|
|
10
|
+
gem "shakapacker", "7.0.1" # Use the latest and the exact version
|
|
11
|
+
gem "react_on_rails", "13.3.1" # Use the latest and the exact version
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
Or use `bundle add`:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
-
bundle add
|
|
18
|
-
bundle add
|
|
17
|
+
bundle add shakapacker --version=7.0.1 --strict
|
|
18
|
+
bundle add react_on_rails --version=13.3.1 --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`).
|
|
@@ -37,14 +37,16 @@
|
|
|
37
37
|
```bash
|
|
38
38
|
rails generate react_on_rails:install --help
|
|
39
39
|
```
|
|
40
|
-
5. Ensure that you have `overmind` or `foreman` installed
|
|
40
|
+
5. Ensure that you have `overmind` or `foreman` installed.
|
|
41
|
+
|
|
42
|
+
Note: `foreman` should be installed on the system not on your project. [Read more](https://github.com/ddollar/foreman/wiki/Don't-Bundle-Foreman)
|
|
41
43
|
|
|
42
44
|
6. Start your Rails server:
|
|
43
45
|
|
|
44
46
|
```bash
|
|
45
47
|
./bin/dev
|
|
46
48
|
```
|
|
47
|
-
Note: `foreman` defaults to PORT 5000 unless you set the value of PORT in your environment. For example, you can `export PORT=3000` to use the Rails default port of 3000. For the hello_world example this is already set.
|
|
49
|
+
Note: `foreman` defaults to PORT 5000 unless you set the value of PORT in your environment. For example, you can `export PORT=3000` to use the Rails default port of 3000. For the hello_world example, this is already set.
|
|
48
50
|
|
|
49
51
|
7. Visit [localhost:3000/hello_world](http://localhost:3000/hello_world).
|
|
50
52
|
|
|
@@ -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
|
| --- | --------------- | --- | --------------------- | ----------------------| ------------------------|----|
|
|
@@ -56,12 +56,42 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
56
56
|
|
|
57
57
|
----
|
|
58
58
|
|
|
59
|
+
## Suppress warning related to Can't resolve 'react-dom/client' in React < 18
|
|
60
|
+
|
|
61
|
+
You may see a warning like this when building a Webpack bundle using any version of React below 18:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
Module not found: Error: Can't resolve 'react-dom/client' in ....
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
It can be safely [suppressed](https://webpack.js.org/configuration/other-options/#ignorewarnings) in your Webpack configuration. The following is an example of this suppression in `config/webpack/commonWebpackConfig.js`:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
const { webpackConfig: baseClientWebpackConfig, merge } = require('shakapacker');
|
|
71
|
+
|
|
72
|
+
const commonOptions = {
|
|
73
|
+
resolve: {
|
|
74
|
+
extensions: ['.css', '.ts', '.tsx'],
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const ignoreWarningsConfig = {
|
|
79
|
+
ignoreWarnings: [/Module not found: Error: Can't resolve 'react-dom\/client'/],
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const commonWebpackConfig = () => merge({}, baseClientWebpackConfig, commonOptions, ignoreWarningsConfig);
|
|
83
|
+
|
|
84
|
+
module.exports = commonWebpackConfig;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
----
|
|
88
|
+
|
|
59
89
|
## HMR and React Hot Reloading
|
|
60
90
|
|
|
61
91
|
Before turning HMR on, consider upgrading to the latest stable gems and packages:
|
|
62
|
-
https://github.com/
|
|
92
|
+
https://github.com/shakacode/shakapacker#upgrading
|
|
63
93
|
|
|
64
|
-
Configure `config/
|
|
94
|
+
Configure `config/shakapacker.yml` file:
|
|
65
95
|
|
|
66
96
|
```yaml
|
|
67
97
|
development:
|
|
@@ -71,7 +101,7 @@ development:
|
|
|
71
101
|
inline: true
|
|
72
102
|
```
|
|
73
103
|
|
|
74
|
-
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.
|
|
75
105
|
|
|
76
106
|
Webpack's HMR allows the replacement of modules for React in-place without reloading the browser. To do this, you have two options:
|
|
77
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
|
data/docs/guides/tutorial.md
CHANGED
|
@@ -4,7 +4,7 @@ _Also see the example repo of [React on Rails Tutorial With SSR, HMR fast refres
|
|
|
4
4
|
|
|
5
5
|
-----
|
|
6
6
|
|
|
7
|
-
*Updated for Ruby 2.7, Rails 7, React on Rails v13, and Shakapacker
|
|
7
|
+
*Updated for Ruby 2.7, Rails 7, React on Rails v13, and Shakapacker v7*
|
|
8
8
|
|
|
9
9
|
This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ After finishing this tutorial you will get an application that can do the follow
|
|
|
12
12
|
|
|
13
13
|

|
|
14
14
|
|
|
15
|
-
You can find here:
|
|
15
|
+
You can find it here:
|
|
16
16
|
* [Source code for this app in PR, using the --redux option](https://github.com/shakacode/react_on_rails-test-new-redux-generation/pull/17) and [for Heroku](https://github.com/shakacode/react_on_rails-test-new-redux-generation/pull/18).
|
|
17
17
|
* [Live on Heroku](https://react-on-rails-redux-gen-8-0-0.herokuapp.com/)
|
|
18
18
|
|
|
@@ -21,42 +21,56 @@ By the time you read this, the latest may have changed. Be sure to check the ver
|
|
|
21
21
|
* https://rubygems.org/gems/react_on_rails
|
|
22
22
|
* https://www.npmjs.com/package/react-on-rails
|
|
23
23
|
|
|
24
|
+
# Table of Content:
|
|
25
|
+
- [Installation](#installation)
|
|
26
|
+
- [Setting up your environment](#setting-up-your-environment)
|
|
27
|
+
- [Create a new Ruby on Rails App](#create-a-new-ruby-on-rails-app)
|
|
28
|
+
- [Add the Shakapacker and react_on_rails gems](#add-the-shakapacker-and-react_on_rails-gems)
|
|
29
|
+
- [Run the Shakapacker generator](#run-the-shakapacker-generator)
|
|
30
|
+
- [Run the React on Rails Generator](#run-the-react-on-rails-generator)
|
|
31
|
+
- [Setting up your environment variables](#setting-up-your-environment-variables)
|
|
32
|
+
- [Running the app](#running-the-app)
|
|
33
|
+
- [HMR vs. React Hot Reloading](#hmr-vs-react-hot-reloading)
|
|
34
|
+
- [Deploying to Heroku](#deploying-to-heroku)
|
|
35
|
+
- [Create Your Heroku App](#create-your-heroku-app)
|
|
36
|
+
- [Swap out sqlite for postgres](#swap-out-sqlite-for-postgres)
|
|
37
|
+
- [Other features](#other-features)
|
|
38
|
+
- [Turning on Server Rendering](#turning-on-server-rendering)
|
|
39
|
+
- [Moving from the Rails default `/app/javascript` to the recommended `/client` structure](#moving-from-the-rails-default-appjavascript-to-the-recommended-client-structure)
|
|
40
|
+
- [Using HMR with the shakapacker setup](#using-hmr-with-the-shakapacker-setup)
|
|
41
|
+
- [Custom IP & PORT setup (Cloud9 example)](#custom-ip--port-setup-cloud9-example)
|
|
42
|
+
- [RubyMine performance tip](#rubymine-performance-tip)
|
|
43
|
+
- [Conclusion](#conclusion)
|
|
44
|
+
# Installation
|
|
24
45
|
## Setting up your environment
|
|
25
46
|
|
|
26
|
-
Trying out **React on Rails** is super easy, so long as you have the basic prerequisites.
|
|
47
|
+
Trying out **React on Rails** is super easy, so long as you have the basic prerequisites.
|
|
27
48
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
nvm
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
brew install yarn # you can use other installer if desired, such as yvm
|
|
34
|
-
rvm install 2.7 # download and install latest stable Ruby (update to exact version)
|
|
35
|
-
rvm use 2.7 --default # use it and make it default
|
|
36
|
-
rvm list # check
|
|
37
|
-
|
|
38
|
-
gem install rails # download and install latest stable Rails
|
|
39
|
-
gem install foreman # download and install Foreman
|
|
40
|
-
```
|
|
49
|
+
- **Ruby:** We support all active Ruby versions but recommend using the latest stable Ruby version. Solutions like [rvm](https://rvm.io) or [rbenv](https://github.com/rbenv/rbenv) make it easy to have multiple Ruby versions on your machine.
|
|
50
|
+
- **Rails:** We support Rails 6 and later.
|
|
51
|
+
- **Nodejs:** We support all [active Node versions](https://github.com/nodejs/release#release-schedule) but recommend using the latest LTS release of Nodejs for the longest support. Older inactive node versions might still work but is not guaranteed. We also recommend using [nvm](https://github.com/nvm-sh/nvm/) to ease using different node versions in different projects.
|
|
52
|
+
- **yarn:** We use [yarn classic](https://classic.yarnpkg.com/) as our node package manager.
|
|
53
|
+
- You need to have either [Overmind](https://github.com/DarthSim/overmind) or [Foreman](https://rubygems.org/gems/foreman) as a process manager.
|
|
41
54
|
|
|
42
55
|
## Create a new Ruby on Rails App
|
|
43
|
-
Then we need to create a fresh Rails application as
|
|
56
|
+
Then we need to create a fresh Rails application as follows.
|
|
44
57
|
|
|
45
|
-
First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If you are using an older version of Rails, you'll need to install webpacker with react per the instructions [here](https://github.com/rails/webpacker).
|
|
58
|
+
First, be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If you are using an older version of Rails, you'll need to install webpacker with react per the instructions [here](https://github.com/rails/webpacker).
|
|
46
59
|
|
|
47
60
|
```bash
|
|
48
61
|
# For Rails 6.x
|
|
49
62
|
rails new test-react-on-rails --skip-javascript
|
|
50
63
|
|
|
51
64
|
# For Rails 7.x
|
|
52
|
-
rails new test-react-on-rails --skip-
|
|
65
|
+
rails new test-react-on-rails --skip-javascript
|
|
53
66
|
|
|
54
67
|
cd test-react-on-rails
|
|
55
68
|
```
|
|
69
|
+
Note: You can use `--database=postgresql` option to use Postgresql for the database.
|
|
56
70
|
|
|
57
|
-
## Add the
|
|
71
|
+
## Add the Shakapacker and react_on_rails gems
|
|
58
72
|
We recommend using the latest version of these gems. Otherwise, specify the
|
|
59
|
-
exact versions of both the gem and npm
|
|
73
|
+
exact versions of both the gem and npm packages. In other words, don't use
|
|
60
74
|
the `^` or `~` in the version specifications.
|
|
61
75
|
|
|
62
76
|
```bash
|
|
@@ -67,10 +81,10 @@ bundle add shakapacker --strict
|
|
|
67
81
|
Note: The latest released React On Rails version is considered stable. Please use the latest
|
|
68
82
|
version to ensure you get all the security patches and the best support.
|
|
69
83
|
|
|
70
|
-
## Run the
|
|
84
|
+
## Run the Shakapacker generator
|
|
71
85
|
|
|
72
86
|
```bash
|
|
73
|
-
bundle exec rails
|
|
87
|
+
bundle exec rails shakapacker:install
|
|
74
88
|
```
|
|
75
89
|
|
|
76
90
|
Commit all the changes so far to avoid getting errors in the next step.
|
|
@@ -79,7 +93,7 @@ Commit all the changes so far to avoid getting errors in the next step.
|
|
|
79
93
|
git commit -am "Initial commit"
|
|
80
94
|
```
|
|
81
95
|
|
|
82
|
-
Alternatively you can use `--ignore-warnings` in the next step.
|
|
96
|
+
Alternatively, you can use `--ignore-warnings` in the next step.
|
|
83
97
|
|
|
84
98
|
## Run the React on Rails Generator
|
|
85
99
|
|
|
@@ -110,59 +124,64 @@ Then run the server with one of the following options:
|
|
|
110
124
|
|
|
111
125
|
## Running the app
|
|
112
126
|
|
|
113
|
-
### For HMR:
|
|
114
|
-
|
|
115
|
-
```bash
|
|
116
|
-
./bin/dev
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### Without HMR, statically creating the bundles
|
|
120
|
-
|
|
121
127
|
```bash
|
|
122
|
-
./bin/dev
|
|
128
|
+
./bin/dev # For HMR
|
|
129
|
+
# or
|
|
130
|
+
./bin/dev-static # Without HMR, statically creating the bundles
|
|
123
131
|
```
|
|
124
132
|
|
|
125
133
|
Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
|
|
126
134
|
|
|
127
135
|
# HMR vs. React Hot Reloading
|
|
128
136
|
|
|
129
|
-
First, check that the `hmr` and the `inline` options are `true` in your `config/
|
|
137
|
+
First, check that the `hmr` and the `inline` options are `true` in your `config/shakapacker.yml` file.
|
|
130
138
|
|
|
131
|
-
The basic setup will have HMR working with the default
|
|
139
|
+
The basic setup will have HMR working with the default Shakapacker setup. The basic
|
|
132
140
|
[HMR](https://webpack.js.org/concepts/hot-module-replacement/), without a special
|
|
133
141
|
React setup, will cause a full page refresh each time you save a file.
|
|
134
142
|
|
|
135
|
-
|
|
143
|
+
# Deploying to Heroku
|
|
136
144
|
|
|
137
|
-
|
|
138
|
-
*Assuming you can
|
|
145
|
+
## Create Your Heroku App
|
|
146
|
+
*Assuming you can log in to heroku.com and have logged into your shell for Heroku.*
|
|
139
147
|
|
|
140
148
|
1. Visit [https://dashboard.heroku.com/new](https://dashboard.heroku.com/new) and create an app, say named `my-name-react-on-rails`:
|
|
141
149
|
|
|
142
150
|

|
|
143
151
|
|
|
144
|
-
Run this command that looks like this from your new
|
|
152
|
+
Run this command that looks like this from your new Heroku app
|
|
145
153
|
|
|
146
|
-
|
|
154
|
+
```bash
|
|
155
|
+
heroku git:remote -a my-name-react-on-rails
|
|
156
|
+
```
|
|
147
157
|
|
|
148
158
|
Set heroku to use multiple buildpacks:
|
|
149
159
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
160
|
+
```bash
|
|
161
|
+
heroku buildpacks:set heroku/ruby
|
|
162
|
+
heroku buildpacks:add --index 1 heroku/nodejs
|
|
163
|
+
```
|
|
153
164
|
|
|
154
|
-
|
|
165
|
+
## Swap out sqlite for postgres:
|
|
166
|
+
Heroku requires your app to use Postgresql. If you have not setup your app
|
|
167
|
+
with Postgresql, you need to change your app settings to use this database.
|
|
155
168
|
|
|
156
|
-
Run
|
|
169
|
+
Run the following command (in Rails 6+):
|
|
157
170
|
|
|
171
|
+
```bash
|
|
172
|
+
rails db:system:change --to=postgresql
|
|
158
173
|
```
|
|
174
|
+
|
|
175
|
+
If for any reason you want to do this process manually, run these two commands:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
159
178
|
bundle remove sqlite3
|
|
160
179
|
bundle add pg
|
|
161
180
|
```
|
|
162
181
|
|
|
163
182
|

|
|
164
183
|
|
|
165
|
-
|
|
184
|
+
Now replace your `database.yml` file with this (assuming your app name is "ror").
|
|
166
185
|
|
|
167
186
|
```yml
|
|
168
187
|
default: &default
|
|
@@ -189,14 +208,14 @@ production:
|
|
|
189
208
|
|
|
190
209
|
Then you need to setup postgres so you can run locally:
|
|
191
210
|
|
|
192
|
-
```
|
|
211
|
+
```bash
|
|
193
212
|
rake db:setup
|
|
194
213
|
rake db:migrate
|
|
195
214
|
```
|
|
196
215
|
|
|
197
216
|

|
|
198
217
|
|
|
199
|
-
|
|
218
|
+
Optionally you can add this line to your `routes.rb`. That way, your root page will go to the Hello World page for React On Rails.
|
|
200
219
|
|
|
201
220
|
```ruby
|
|
202
221
|
root "hello_world#index"
|
|
@@ -206,9 +225,8 @@ root "hello_world#index"
|
|
|
206
225
|
|
|
207
226
|
Next, configure your app for Puma, per the [instructions on Heroku](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server).
|
|
208
227
|
|
|
209
|
-
Create
|
|
228
|
+
Create `./Procfile` with the following content. This is what Heroku uses to start your app.
|
|
210
229
|
|
|
211
|
-
`Procfile`
|
|
212
230
|
```
|
|
213
231
|
web: bundle exec puma -C config/puma.rb
|
|
214
232
|
```
|
|
@@ -238,14 +256,14 @@ Next, update your `package.json` to specify the version of yarn and node. Add th
|
|
|
238
256
|
|
|
239
257
|
```json
|
|
240
258
|
"engines": {
|
|
241
|
-
"node": "
|
|
259
|
+
"node": "16.19.0",
|
|
242
260
|
"yarn": "1.22.4"
|
|
243
261
|
},
|
|
244
262
|
```
|
|
245
263
|
|
|
246
|
-
Then after all changes are done don't forget to commit them with git and finally you can push your app to Heroku!
|
|
264
|
+
Then after all changes are done don't forget to commit them with git and finally, you can push your app to Heroku!
|
|
247
265
|
|
|
248
|
-
```
|
|
266
|
+
```bash
|
|
249
267
|
git add -A
|
|
250
268
|
git commit -m "Changes for Heroku"
|
|
251
269
|
git push heroku master
|
|
@@ -253,14 +271,13 @@ git push heroku master
|
|
|
253
271
|
|
|
254
272
|
Then run:
|
|
255
273
|
|
|
256
|
-
```
|
|
274
|
+
```bash
|
|
257
275
|
heroku open
|
|
258
276
|
```
|
|
259
277
|
|
|
260
278
|
and you will see your live app and you can share this URL with your friends. Congrats!
|
|
261
279
|
|
|
262
|
-
|
|
263
|
-
|
|
280
|
+
# Other features
|
|
264
281
|
## Turning on Server Rendering
|
|
265
282
|
|
|
266
283
|
You can turn on server rendering by simply changing the `prerender` option to `true`:
|
|
@@ -277,15 +294,7 @@ If you want to test this out with HMR, then you also need to add this line to yo
|
|
|
277
294
|
```
|
|
278
295
|
|
|
279
296
|
More likely, you will create a different build file for server rendering. However, if you want to
|
|
280
|
-
use the same file from the
|
|
281
|
-
|
|
282
|
-
Then push to Heroku:
|
|
283
|
-
|
|
284
|
-
```
|
|
285
|
-
git add -A
|
|
286
|
-
git commit -m "Enable server rendering"
|
|
287
|
-
git push heroku master
|
|
288
|
-
```
|
|
297
|
+
use the same file from the shakapack-dev-server, you'll need to add that line.
|
|
289
298
|
|
|
290
299
|
When you look at the source code for the page (right click, view source in Chrome), you can see the difference between non-server rendering, where your DIV containing your React looks like this:
|
|
291
300
|
|
|
@@ -311,34 +320,34 @@ ShakaCode recommends that you use `/client` for your client side app. This way a
|
|
|
311
320
|
|
|
312
321
|
1. Move the directory:
|
|
313
322
|
|
|
314
|
-
```
|
|
323
|
+
```bash
|
|
315
324
|
mv app/javascript client
|
|
316
325
|
```
|
|
317
326
|
|
|
318
|
-
2. Edit your `/config/
|
|
327
|
+
2. Edit your `/config/shakapacker.yml` file. Change the `default/source_path`:
|
|
319
328
|
|
|
320
329
|
```yml
|
|
321
|
-
|
|
330
|
+
source_path: client
|
|
322
331
|
```
|
|
323
332
|
|
|
324
|
-
## Using HMR with the
|
|
333
|
+
## Using HMR with the shakapacker setup
|
|
325
334
|
|
|
326
|
-
Start the app using `foreman start -f Procfile.dev`.
|
|
335
|
+
Start the app using `overmind start -f Procfile.dev` or `foreman start -f Procfile.dev`.
|
|
327
336
|
|
|
328
|
-
When you change a JSX file
|
|
337
|
+
When you change and save a JSX file, the browser will automatically refresh!
|
|
329
338
|
|
|
330
339
|
So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
|
|
331
340
|
|
|
332
|
-
* https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md
|
|
333
|
-
* https://webpack.js.org/configuration/dev-server/
|
|
334
|
-
* https://webpack.js.org/concepts/hot-module-replacement/
|
|
341
|
+
* [webpack-dev-server](https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md)
|
|
342
|
+
* [DevServer](https://webpack.js.org/configuration/dev-server/)
|
|
343
|
+
* [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/)
|
|
335
344
|
|
|
336
|
-
React on Rails will automatically handle disabling server rendering if there is only one bundle file created by the Webpack development server by
|
|
345
|
+
React on Rails will automatically handle disabling server rendering if there is only one bundle file created by the Webpack development server by `shakapcker`.
|
|
337
346
|
|
|
338
347
|
|
|
339
|
-
|
|
348
|
+
## Custom IP & PORT setup (Cloud9 example)
|
|
340
349
|
|
|
341
|
-
In case you are running some custom setup with different IP or PORT you should also edit Procfile.dev. For example to be able to run on free Cloud9 IDE we are putting IP 0.0.0.0 and PORT 8080. The default generated file `Procfile.dev` uses `-p 3000`.
|
|
350
|
+
In case you are running some custom setup with different IP or PORT you should also edit Procfile.dev. For example, to be able to run on free Cloud9 IDE we are putting IP 0.0.0.0 and PORT 8080. The default generated file `Procfile.dev` uses `-p 3000`.
|
|
342
351
|
|
|
343
352
|
``` Procfile.dev
|
|
344
353
|
web: rails s -p 8080 -b 0.0.0.0
|
|
@@ -346,17 +355,14 @@ web: rails s -p 8080 -b 0.0.0.0
|
|
|
346
355
|
|
|
347
356
|
Then visit https://your-shared-addr.c9users.io:8080/hello_world
|
|
348
357
|
|
|
349
|
-
## RubyMine
|
|
358
|
+
## RubyMine performance tip
|
|
350
359
|
|
|
351
360
|
It's super important to exclude certain directories from RubyMine or else it will slow to a crawl as it tries to parse all the npm files.
|
|
352
361
|
|
|
353
|
-
* Generated files, per the settings in your `config/
|
|
362
|
+
* Generated files, per the settings in your `config/shakapacker.yml`, which default to `public/packs` and `public/packs-test`
|
|
354
363
|
* `node_modules`
|
|
355
364
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
## Conclusion
|
|
365
|
+
# Conclusion
|
|
360
366
|
|
|
361
367
|
* Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
|
|
362
368
|
|
|
@@ -12,13 +12,13 @@ Previously, the gem `webpacker` was a Gem dependency.
|
|
|
12
12
|
|
|
13
13
|
v13 has changed slightly to switch to `shakapacker`.
|
|
14
14
|
|
|
15
|
-
For details, see
|
|
15
|
+
For details, see the Shakapacker guide to upgrading to [version 6](https://github.com/shakacode/shakapacker/blob/master/docs/v6_upgrade.md) and [version 7](https://github.com/shakacode/shakapacker/blob/master/docs/v7_upgrade.md)
|
|
16
16
|
|
|
17
17
|
In summary:
|
|
18
18
|
|
|
19
19
|
1. Change the gem reference from 'webpacker' to 'shakapacker'
|
|
20
20
|
2. Change the npm reference from '@rails/webpacker' to 'shakapacker'
|
|
21
|
-
3. Other updates, depending on what version of rails/webpacker that you had.
|
|
21
|
+
3. Other updates, depending on what version of `rails/webpacker` that you had.
|
|
22
22
|
|
|
23
23
|
## Upgrading to v12
|
|
24
24
|
### Recent versions
|