react_on_rails 12.0.0 → 12.1.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 +12 -2
- data/.eslintrc +2 -1
- data/.github/workflows/lint-js-and-ruby.yml +53 -0
- data/.github/workflows/main.yml +178 -0
- data/.github/workflows/package-js-tests.yml +35 -0
- data/.github/workflows/rspec-package-specs.yml +45 -0
- data/.prettierrc +0 -3
- data/.rubocop.yml +47 -38
- data/.travis.yml +8 -4
- data/CHANGELOG.md +58 -28
- data/CONTRIBUTING.md +4 -3
- data/Gemfile +1 -33
- data/Gemfile.development_dependencies +51 -0
- data/NEWS.md +5 -0
- data/README.md +116 -105
- data/SUMMARY.md +1 -1
- data/docs/{basics → additional-details}/generator-details.md +1 -6
- data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
- data/docs/additional-details/recommended-project-structure.md +69 -0
- data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
- data/docs/api/javascript-api.md +5 -5
- data/docs/api/redux-store-api.md +5 -5
- data/docs/api/view-helpers-api.md +11 -12
- data/docs/basics/client-vs-server-rendering.md +4 -2
- data/docs/basics/configuration.md +43 -26
- data/docs/basics/deployment.md +2 -2
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +64 -9
- data/docs/{outdated → basics}/how-react-on-rails-works.md +8 -9
- data/docs/basics/installation-into-an-existing-rails-app.md +13 -6
- data/docs/basics/react-server-rendering.md +8 -5
- data/docs/basics/render-functions-and-railscontext.md +1 -1
- data/docs/basics/rspec-configuration.md +10 -10
- data/docs/{tutorial.md → basics/tutorial.md} +28 -30
- data/docs/basics/upgrading-react-on-rails.md +52 -15
- data/docs/basics/webpack-configuration.md +14 -22
- data/docs/contributor-info/linters.md +5 -6
- data/docs/contributor-info/pull-requests.md +2 -4
- data/docs/contributor-info/releasing.md +1 -1
- data/docs/home.md +382 -0
- data/docs/{outdated → javascript}/code-splitting.md +8 -8
- data/docs/javascript/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
- data/docs/{additional-reading → javascript}/images.md +5 -6
- data/docs/{additional-reading → javascript}/react-router.md +1 -1
- data/docs/{additional-reading → javascript}/server-rendering-tips.md +4 -1
- data/docs/{articles.md → misc/articles.md} +1 -1
- data/docs/misc/doctrine.md +5 -6
- data/docs/outdated/deferred-rendering.md +39 -0
- data/docs/outdated/rails-assets-relative-paths.md +3 -3
- data/docs/outdated/rails-assets.md +8 -8
- data/docs/outdated/rails3.md +2 -2
- data/docs/{additional-reading → rails}/convert-rails-5-api-only-app.md +1 -1
- data/docs/rails-webpacker-react-integration-options.md +182 -0
- data/docs/testimonials/testimonials.md +6 -6
- data/lib/generators/react_on_rails/base_generator.rb +8 -1
- data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
- data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +8 -2
- data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
- data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
- data/lib/react_on_rails/configuration.rb +2 -0
- data/lib/react_on_rails/git_utils.rb +3 -3
- data/lib/react_on_rails/helper.rb +23 -17
- data/lib/react_on_rails/locales/base.rb +3 -3
- data/lib/react_on_rails/locales/to_js.rb +0 -4
- data/lib/react_on_rails/locales/to_json.rb +0 -4
- data/lib/react_on_rails/prerender_error.rb +1 -1
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +6 -6
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
- data/lib/react_on_rails/test_helper.rb +2 -0
- data/lib/react_on_rails/utils.rb +24 -4
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +9 -5
- data/lib/tasks/assets.rake +35 -14
- data/package.json +24 -29
- data/rakelib/docker.rake +0 -5
- data/rakelib/examples.rake +1 -1
- data/rakelib/lint.rake +4 -10
- data/rakelib/release.rake +28 -16
- data/rakelib/run_rspec.rake +10 -11
- data/rakelib/task_helpers.rb +16 -4
- data/react_on_rails.gemspec +3 -17
- data/yarn.lock +2272 -4960
- metadata +50 -224
- data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
- data/docs/additional-reading/webpack-dev-server.md +0 -15
- data/docs/basics/recommended-project-structure.md +0 -77
- data/ruby-lint.yml +0 -25
- /data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
- /data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
- /data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
- /data/docs/{basics → deployment}/heroku-deployment.md +0 -0
- /data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
- /data/docs/{additional-reading → javascript}/asset-pipeline.md +0 -0
- /data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
- /data/docs/{additional-reading → javascript}/credits.md +0 -0
- /data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
- /data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
- /data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
- /data/docs/{additional-reading → javascript}/react-helmet.md +0 -0
- /data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
- /data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
- /data/docs/{additional-reading → javascript}/webpack.md +0 -0
- /data/docs/{coding-style → misc}/style.md +0 -0
- /data/docs/{additional-reading → misc}/tips.md +0 -0
- /data/docs/{additional-reading → rails}/rails-engine-integration.md +0 -0
- /data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
- /data/docs/{additional-reading → rails}/turbolinks.md +0 -0
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# React Server Rendering
|
|
2
2
|
|
|
3
|
-
See also [Client vs. Server Rendering](
|
|
3
|
+
See also [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/basics/client-vs-server-rendering).
|
|
4
|
+
|
|
5
|
+
## What is the easiest way to setup a webpack configuration for server-side-rendering?
|
|
6
|
+
See the example webpack setup here: [github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh).
|
|
4
7
|
|
|
5
8
|
## What is Server Rendering?
|
|
6
9
|
|
|
@@ -10,13 +13,13 @@ During the Rails rendering of HTML per a browser request, the Rails server will
|
|
|
10
13
|
|
|
11
14
|
The default JavaScript interpretter is [ExecJS](https://github.com/rails/execjs). If you want to maximize the perfomance 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).
|
|
12
15
|
|
|
13
|
-
See [this note](
|
|
14
|
-
|
|
16
|
+
See [this note](https://www.shakacode.com/react-on-rails/docs/basics/client-vs-server-rendering).
|
|
15
17
|
|
|
16
18
|
## How do you do Server Rendering with React on Rails?
|
|
17
19
|
1. The `react_component` view helper method provides the `prerender:` option to switch on or off server rendering.
|
|
18
20
|
1. Configure your Webpack setup to create a different server bundle per your needs. While you may reuse the same bundle as for client rendering, this is not common in larger apps for many reasons, such as as code splitting, handling CSS and images, different code paths for React Router on the server vs. client, etc.
|
|
19
|
-
1. You need to configure `config.server_bundle_js_file = "
|
|
21
|
+
1. You need to configure `config.server_bundle_js_file = "server-bundle.js"` in your `config/initializers/react_on_rails.rb`
|
|
22
|
+
1. You should ***not*** put a hash on the server-bundle so that you can easily use the webpack-dev-server for client bundles and have the server bundle generated by a watch process.
|
|
20
23
|
|
|
21
24
|
## Do you need server rendering?
|
|
22
25
|
|
|
@@ -26,4 +29,4 @@ Server rendering is used for either SEO or performance reasons.
|
|
|
26
29
|
|
|
27
30
|
1. Never access `window`. Animations, globals on window, etc. just don't make sense when you're trying to run some JavaScript code to output a string of HTML.
|
|
28
31
|
2. JavaScript calls to `setTimeout`, `setInterval`, and `clearInterval` similarly don't make sense when server rendering.
|
|
29
|
-
3. Promises don't work when server rendering
|
|
32
|
+
3. Promises and file system access don't work when server rendering with ExecJS. Instead, you can use the Node renderer or [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
|
|
@@ -15,7 +15,7 @@ side rendering, except for the key `serverSide` based on whether or not you are
|
|
|
15
15
|
|
|
16
16
|
While you could manually configure your Rails code to pass the "`railsContext` information" with
|
|
17
17
|
the rest of your "props", the `railsContext` is a convenience because it's passed consistently to
|
|
18
|
-
all invocations of
|
|
18
|
+
all invocations of Render-Functions.
|
|
19
19
|
|
|
20
20
|
For example, suppose you create a "render-function" called MyAppComponent.
|
|
21
21
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# RSpec Configuration
|
|
2
|
-
_Click [here for minitest](
|
|
2
|
+
_Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/basics/minitest-configuration)_
|
|
3
3
|
|
|
4
4
|
# If your webpack configurations correspond to rails/webpacker's default setup
|
|
5
5
|
If you're able to configure your webpack configuration to be run by having your webpack configuration
|
|
@@ -8,12 +8,12 @@ compiled by webpack before running tests and during production deployment:
|
|
|
8
8
|
|
|
9
9
|
1. **Use rails/webpacker's compile option**: Configure your `config/webpacker.yml` so that `compile: true` is for `test` and `production`
|
|
10
10
|
environments. Ensure that your `source_path` is correct, or else `rails/webpacker` won't correctly
|
|
11
|
-
detect changes.
|
|
12
|
-
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](
|
|
11
|
+
detect changes.
|
|
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/basics/configuration).
|
|
13
13
|
|
|
14
14
|
```yml
|
|
15
15
|
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpack"
|
|
16
|
-
```
|
|
16
|
+
```
|
|
17
17
|
|
|
18
18
|
Which should you use? If you're already using the `rails/webpacker` way to configure webpack, then
|
|
19
19
|
you can keep things simple and use the `rails/webpacker` options.
|
|
@@ -25,7 +25,7 @@ Because you will probably want to run RSpec tests that rely on compiled webpack
|
|
|
25
25
|
As mentioned above, you can configure `compile: true` in `config/webpacker.yml` _if_ you've got configuration for
|
|
26
26
|
your webpack in the standard `rails/webpacker` spot of `config/webpack/<NODE_ENV>.js`
|
|
27
27
|
|
|
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](
|
|
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
|
|
|
30
30
|
```ruby
|
|
31
31
|
RSpec.configure do |config|
|
|
@@ -44,22 +44,22 @@ 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 Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
|
|
47
|
+
- If you are using Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
|
|
48
48
|
|
|
49
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 webpacker, the default value will come from the `config/webpacker.yml` value for the `public_output_path` and the `source_path`
|
|
50
50
|
|
|
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.
|
|
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/webpacker.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.
|
|
53
|
+
- By default, the webpack processes look in the webpack generated files folder, configured via the `config/webpacker.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
|
|
57
57
|
# Define the files we need to check for webpack compilation when running tests.
|
|
58
58
|
config.webpack_generated_files = %w( manifest.json )
|
|
59
|
-
|
|
59
|
+
|
|
60
60
|
# OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
|
|
61
61
|
# config.webpack_generated_files = %w( server-bundle.js manifest.json )
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
|
64
64
|
# with rspec then this controls what yarn command is run
|
|
65
65
|
# to automatically refresh your webpack assets on every test run.
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# React on Rails Basic Tutorial
|
|
2
2
|
|
|
3
|
+
-----
|
|
4
|
+
|
|
5
|
+
**November 11, 2020**: See the example repo of [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh) for a new way to setup the creation of your SSR bundle with `rails/webpacker`. This file will be update shortly. Most of it is still relevant.
|
|
6
|
+
|
|
7
|
+
-----
|
|
8
|
+
|
|
3
9
|
*Updated for Ruby 2.7.1, Rails 6.0.3.1, and React on Rails v12.0.0*
|
|
4
10
|
|
|
5
11
|
This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
|
|
@@ -46,18 +52,28 @@ First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If
|
|
|
46
52
|
cd <directory where you want to create your new Rails app>
|
|
47
53
|
|
|
48
54
|
# Any name you like for the rails app
|
|
49
|
-
# Skip javascript so will add that next and get the current version
|
|
55
|
+
# Skip javascript so will add that next and get the current version
|
|
50
56
|
rails new --skip-sprockets -J --skip-turbolinks test-react-on-rails-v12-no-sprockets
|
|
51
57
|
|
|
52
58
|
cd test-react-on-rails
|
|
53
59
|
bundle
|
|
54
60
|
```
|
|
55
61
|
|
|
56
|
-
## Add the webpacker
|
|
62
|
+
## Add the webpacker and react_on_rails gems
|
|
63
|
+
To avoid issues regarding inconsistent gem and npm versions, you should specify the exact versions
|
|
64
|
+
of both the gem and npm package. In other words, don't use the `^` or `~` in the version specifications.
|
|
65
|
+
_Use the latest version for react_on_rails._
|
|
57
66
|
|
|
58
67
|
```
|
|
68
|
+
gem 'react_on_rails', '12.0.4' # prefer exact gem version to match npm version
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Note: The latest released React On Rails version is considered stable. Please use the latest
|
|
72
|
+
version to ensure you get all the security patches and the best support.
|
|
73
|
+
|
|
74
|
+
```bash
|
|
59
75
|
bundle add webpacker
|
|
60
|
-
bundle add react_on_rails
|
|
76
|
+
bundle add react_on_rails --version=12.0.4 --strict
|
|
61
77
|
```
|
|
62
78
|
|
|
63
79
|
## Run the webpacker generator
|
|
@@ -76,29 +92,11 @@ git add -A
|
|
|
76
92
|
git commit -m "Initial commit"
|
|
77
93
|
```
|
|
78
94
|
|
|
79
|
-
##
|
|
80
|
-
|
|
81
|
-
To avoid issues regarding inconsistent gem and npm versions, you should specify the exact versions
|
|
82
|
-
of both the gem and npm package. In other words, don't use the `^` or `~` in the version specifications.
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
gem 'react_on_rails', '12.0.0' # prefer exact gem version to match npm version
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
Note: The latest released React On Rails version is considered stable. Please use the latest
|
|
89
|
-
version to ensure you get all the security patches and the best support.
|
|
90
|
-
|
|
91
|
-
Run `bundle` and commit the changes.
|
|
92
|
-
|
|
93
|
-
```
|
|
94
|
-
bundle
|
|
95
|
-
|
|
96
|
-
git commit -am "Added React on Rails Gem"
|
|
97
|
-
```
|
|
95
|
+
## Run the React on Rails Generator
|
|
98
96
|
|
|
99
97
|
Install React on Rails: `rails generate react_on_rails:install`. You need to first git commit your files before running the generator, or else it will generate an error.
|
|
100
98
|
|
|
101
|
-
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
|
99
|
+
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
|
102
100
|
If you want the redux install: `rails generate react_on_rails:install --redux`
|
|
103
101
|
|
|
104
102
|
```
|
|
@@ -125,7 +123,7 @@ First, check that the `hmr` and the `inline` options are `true` in your `config/
|
|
|
125
123
|
|
|
126
124
|
The basic setup will have HMR working with the default webpacker setup. The basic
|
|
127
125
|
[HMR](https://webpack.js.org/concepts/hot-module-replacement/), without a special
|
|
128
|
-
React setup, will cause a full page refresh each time you save a file.
|
|
126
|
+
React setup, will cause a full page refresh each time you save a file.
|
|
129
127
|
|
|
130
128
|
## Deploying to Heroku
|
|
131
129
|
|
|
@@ -294,8 +292,8 @@ versus with server rendering:
|
|
|
294
292
|
|
|
295
293
|
For more details on server rendering, see:
|
|
296
294
|
|
|
297
|
-
+ [Client vs. Server Rendering](
|
|
298
|
-
+ [React Server Rendering](
|
|
295
|
+
+ [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/basics/client-vs-server-rendering)
|
|
296
|
+
+ [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/basics/react-server-rendering)
|
|
299
297
|
|
|
300
298
|
## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
|
|
301
299
|
|
|
@@ -322,7 +320,7 @@ When you change a JSX file and save, the browser will automatically refresh!
|
|
|
322
320
|
|
|
323
321
|
So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
|
|
324
322
|
|
|
325
|
-
* https://github.com/rails/webpacker/blob/
|
|
323
|
+
* https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md
|
|
326
324
|
* https://webpack.js.org/configuration/dev-server/
|
|
327
325
|
* https://webpack.js.org/concepts/hot-module-replacement/
|
|
328
326
|
|
|
@@ -337,7 +335,7 @@ In case you are running some custom setup with different IP or PORT you should a
|
|
|
337
335
|
web: rails s -p 8080 -b 0.0.0.0
|
|
338
336
|
```
|
|
339
337
|
|
|
340
|
-
Then visit https://your-shared-addr.c9users.io:8080/hello_world
|
|
338
|
+
Then visit https://your-shared-addr.c9users.io:8080/hello_world
|
|
341
339
|
|
|
342
340
|
## RubyMine
|
|
343
341
|
|
|
@@ -351,8 +349,8 @@ It's super important to exclude certain directories from RubyMine or else it wil
|
|
|
351
349
|
|
|
352
350
|
## Conclusion
|
|
353
351
|
|
|
354
|
-
* Browse the docs
|
|
352
|
+
* Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
|
|
355
353
|
|
|
356
|
-
Feedback is greatly appreciated! As are stars on github!
|
|
354
|
+
Feedback is greatly appreciated! As are stars on github!
|
|
357
355
|
|
|
358
356
|
If you want personalized help, don't hesitate to get in touch with us at [contact@shakacode.com](mailto:contact@shakacode.com). We offer [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki) and consulting so you can focus on your app and not on how to make Webpack plus Rails work optimally.
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
# Upgrading React on Rails
|
|
2
2
|
|
|
3
3
|
## Need Help Migrating?
|
|
4
|
-
If you would like help in migrating between React on Rails versions or help with implementing server rendering, please contact [justin@shakacode.com](mailto:justin@shakacode.com) for information about our [
|
|
4
|
+
If you would like help in migrating between React on Rails versions or help with implementing server rendering, please contact [justin@shakacode.com](mailto:justin@shakacode.com) for more information about our [React on Rails Pro Support](https://www.shakacode.com/react-on-rails-pro).
|
|
5
5
|
|
|
6
|
-
We specialize in helping companies to quickly and efficiently
|
|
6
|
+
We specialize in helping companies to quickly and efficiently upgrade. The older versions use the Rails asset pipeline to package client assets. The current and recommended way is to use Webpack 4+ for asset preparation. You may also need help migrating from the `rails/webpacker`'s Webpack configuration to a better setup ready for Server Side Rendering.
|
|
7
7
|
|
|
8
8
|
## Upgrading to v12
|
|
9
9
|
### Recent versions
|
|
10
|
-
Make sure that you are on a relatively more recent version of rails and webpacker.
|
|
11
|
-
v12 is tested on Rails 6. It should work on Rails v5. If you're on
|
|
12
|
-
and v12 doesn't work, please file an issue.
|
|
10
|
+
Make sure that you are on a relatively more recent version of rails and webpacker. Yes, the [rails/webpacker](https://github.com/rails/webpacker) gem is required!
|
|
11
|
+
v12 is tested on Rails 6. It should work on Rails v5. If you're on any older version,
|
|
12
|
+
and v12 doesn't work, please file an issue.
|
|
13
|
+
|
|
14
|
+
### Removed Configuration config.symlink_non_digested_assets_regex
|
|
15
|
+
Remove `config.symlink_non_digested_assets_regex` from your `config/initializers/react_on_rails.rb`.
|
|
16
|
+
If you still need that feature, please file an issue.
|
|
13
17
|
|
|
14
18
|
### i18n default format changed to JSON
|
|
15
19
|
* If you're using the internalization helper, then set `config.i18n_output_format = 'js'`. You can
|
|
@@ -19,25 +23,58 @@ and v12 doesn't work, please file an issue.
|
|
|
19
23
|
### Updated API for `ReactOnRails.register()`
|
|
20
24
|
|
|
21
25
|
In order to solve the issues regarding React Hooks compatibility, the number of parameters
|
|
22
|
-
for functions is used to determine if you have a
|
|
26
|
+
for functions is used to determine if you have a Render-Function that will get invoked to
|
|
23
27
|
return a React component, or you are registering a React component defined by a function.
|
|
28
|
+
Please see [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/basics/render-functions-and-railscontext) for
|
|
29
|
+
more information on what a Render-Function is.
|
|
24
30
|
|
|
25
|
-
#####
|
|
31
|
+
##### Update required for registered functions taking exactly 2 params.
|
|
26
32
|
|
|
27
|
-
Registered Objects are of the following
|
|
28
|
-
1.
|
|
29
|
-
that takes zero or one params and returns a React Element, like JSX.
|
|
33
|
+
Registered Objects are of the following type:
|
|
34
|
+
1. **Function that takes only zero or one params and you return a React Element**, often JSX. If the function takes zero or one params, there is **no migration needed** for that function.
|
|
30
35
|
```js
|
|
31
|
-
export default (props
|
|
36
|
+
export default (props) => <Component {...props} />;
|
|
32
37
|
```
|
|
33
38
|
|
|
34
|
-
2.
|
|
39
|
+
2. **Function that takes only zero or one params and you return an Object (_not a React Element_)**. If the function takes zero or one params, **you need to add one or two unused params so you have exactly 2 params** and then that function will be treated as a render function and it can return an Object rather than a React element. If you don't do this, you'll see this obscure error message:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
[SERVER] message: Objects are not valid as a React child (found: object with keys {renderedHtml}). If you meant to render a collection of children, use an array instead.
|
|
43
|
+
in YourComponentRenderFunction
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
So look in YourComponentRenderFunction and do this change
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
export default (props) => { renderedHTML: getRenderedHTML };
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
To have exactly 2 arguments:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
export default (props, _railsContext) => { renderedHTML: getRenderedHTML };
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
3. Function that takes **2 params** and returns **a React function or class component**. _Migration is needed as the older syntax returned a React Element._
|
|
59
|
+
A function component is a function that takes zero or one params and returns a React Element, like JSX. The correct syntax
|
|
60
|
+
looks like:
|
|
35
61
|
```js
|
|
36
|
-
export default (props) => <Component {...props} />;
|
|
62
|
+
export default (props, railsContext) => () => <Component {{...props, railsContext}} />;
|
|
37
63
|
```
|
|
64
|
+
Note, you cannot return a React Element (JSX). See below for the migration steps. If your function that took **two params returned
|
|
65
|
+
an Object**, then no migration is required.
|
|
66
|
+
4. Function that takes **3 params** and uses the 3rd param, `domNodeId`, to call `ReactDOM.hydrate`. If the function takes 3 params, there is **no migration needed** for that function.
|
|
67
|
+
5. ES6 or ES5 class. There is **no migration needed**.
|
|
68
|
+
|
|
69
|
+
Previously, with case number 2, you could return a React Element.
|
|
70
|
+
|
|
71
|
+
The fix is simple. Here is an example of the change you'll do:
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
38
75
|
##### Broken, as this function takes two params and it returns a React Element from a JSX Literal
|
|
39
76
|
```js
|
|
40
|
-
export default (props,
|
|
77
|
+
export default (props, railsContext) => <Component {{...props, railsContext} />;
|
|
41
78
|
```
|
|
42
79
|
|
|
43
80
|
If you make this mistake, you'll get this warning
|
|
@@ -63,7 +100,7 @@ wrapper such that you're returning a function rather than a React Element, then:
|
|
|
63
100
|
|
|
64
101
|
## Upgrading rails/webpacker from v3 to v4
|
|
65
102
|
### Custom Webpack build file
|
|
66
|
-
The default value for `extract_css` is **false** in `config/webpack.yml`. Custom webpack builds should set this value to true or else no CSS link tags are generated. You have a custom webpack build if you are not using [rails/webpacker](https://github.com/rails/webpacker to setup your Webpack configuration.
|
|
103
|
+
The default value for `extract_css` is **false** in `config/webpack.yml`. Custom webpack builds should set this value to true or else no CSS link tags are generated. You have a custom webpack build if you are not using [rails/webpacker](https://github.com/rails/webpacker) to setup your Webpack configuration.
|
|
67
104
|
|
|
68
105
|
```yml
|
|
69
106
|
default: &default
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
# Webpack Configuration: custom setup for Webpack or rails/webpacker?
|
|
2
2
|
|
|
3
|
-
## Webpack vs. rails/webpacker
|
|
3
|
+
## Webpack vs. rails/webpacker
|
|
4
4
|
|
|
5
|
-
[Webpack](https://webpack.js.org) is the JavaScript npm package that prepares all your client-side assets. The Rails asset pipeline used to be the preferable way to prepare client-side assets.
|
|
5
|
+
[Webpack](https://webpack.js.org) is the JavaScript npm package that prepares all your client-side assets. The Rails asset pipeline used to be the preferable way to prepare client-side assets.
|
|
6
6
|
|
|
7
7
|
[rails/webpacker](https://github.com/rails/webpacker) is the Ruby gem that mainly gives us 2 things:
|
|
8
8
|
|
|
9
|
-
1. View helpers for placing the
|
|
10
|
-
2. A layer of abstraction on top of Webpack customization.
|
|
9
|
+
1. View helpers for placing the webpack bundles on your Rails views. React on Rails depends on these view helpers.
|
|
10
|
+
2. A layer of abstraction on top of Webpack customization. The base setup works great for the client side webpack configuration.
|
|
11
|
+
|
|
12
|
+
To get a deeper understanding of `rails/webpacker`, watch [RailsConf 2020 CE - Webpacker, It-Just-Works, But How? by Justin Gordon](https://youtu.be/sJLoOpc5LD8)
|
|
13
|
+
|
|
14
|
+
Per the example repo [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh),
|
|
15
|
+
you should consider keeping your codebase mostly consistent with the defaults for [rails/webpacker](https://github.com/rails/webpacker).
|
|
11
16
|
|
|
12
17
|
# React on Rails
|
|
13
18
|
|
|
@@ -15,15 +20,15 @@ Version 9 of React on Rails added support for the rails/webpacker view helpers s
|
|
|
15
20
|
|
|
16
21
|
A key decision in your use React on Rails is whether you go with the rails/webpacker default setup or the traditional React on Rails setup of putting all your client side files under the `/client` directory. While there are technically 2 independent choices involved, the directory structure and the mechanism of Webpack configuration, for simplicity sake we'll assume that these choices go together.
|
|
17
22
|
|
|
18
|
-
## Option 1:
|
|
23
|
+
## Option 1: Default Generator Setup: rails/webpacker app/javascript
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
Typical rails/webpacker apps have a standard directory structure as documented [here](https://github.com/rails/webpacker/blob/5-x-stable/docs/recommended-project-structure.md). If you follow the steps in the the [basic tutorial](https://www.shakacode.com/react-on-rails/docs/basics/tutorial), you will see this pattern in action. In order to customize the Webpack configuration, you need to consult with the [rails/webpacker Webpack configuration](https://www.shakacode.com/react-on-rails/docs/javascript/webpack).
|
|
21
26
|
|
|
22
|
-
The
|
|
27
|
+
The *advantage* of using rails/webpacker to configure Webpack is that there is very little code needed to get started and you don't need to understand really anything about Webpack customization.
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
## Option 2: Traditional React on Rails using the /client directory
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
Until version 9, all React on Rails apps used the `/client` directory for configuring React on Rails in terms of the configuration of Webpack and location of your JavaScript and Webpack files, including the node_modules directory. Version 9 changed the default to `/` for the `node_modules` location using this value in `config/initializers/react_on_rails.rb`: `config.node_modules_location`.
|
|
27
32
|
|
|
28
33
|
You can access values in the `config/webpacker.yml`
|
|
29
34
|
|
|
@@ -35,16 +40,3 @@ You will want consider using some of the same values set in these files:
|
|
|
35
40
|
|
|
36
41
|
* https://github.com/rails/webpacker/blob/master/package/environments/base.js
|
|
37
42
|
* https://github.com/rails/webpacker/blob/master/package/environments/development.js
|
|
38
|
-
|
|
39
|
-
**Note**, if your node_modules directory is not at the top level of the Rails project, then you will need to set the
|
|
40
|
-
ENV value of WEBPACKER_CONFIG to the location of the `config/webpacker.yml` file per [rails/webpacker PR 2561](https://github.com/rails/webpacker/pull/2561).
|
|
41
|
-
|
|
42
|
-
## Option 2: Default Generator Setup: rails/webpacker app/javascript
|
|
43
|
-
|
|
44
|
-
Typical rails/webpacker apps have a standard directory structure as documented [here](https://github.com/rails/webpacker/blob/master/docs/folder-structure.md). If you follow the steps in the the [basic tutorial](../../docs/tutorial.md), you will see this pattern in action. In order to customize the Webpack configuration, you need to consult with the [rails/webpacker Webpack configuration](https://github.com/rails/webpacker/blob/master/docs/webpack.md).
|
|
45
|
-
|
|
46
|
-
The *advantage* of using rails/webpacker to configure Webpack is that there is very little code needed to get started and you don't need to understand really anything about Webpack customization. The *big disadvantage* to this is that you will need to learn the ins and outs of the [rails/webpacker way to customize Webpack](https://github.com/rails/webpacker/blob/master/docs/webpack.md) which differs from the plain [Webpack way](https://webpack.js.org/).
|
|
47
|
-
|
|
48
|
-
You can find more details on this topic in [Recommended Project Structure](./recommended-project-structure.md).
|
|
49
|
-
|
|
50
|
-
See [Issue 982: Tutorial Generating Correct Project Structure?](https://github.com/shakacode/react_on_rails/issues/982) to discuss this issue.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Linters
|
|
2
|
-
These linters support the [ShakaCode Style Guidelines](
|
|
2
|
+
These linters support the [ShakaCode Style Guidelines](https://www.shakacode.com/react-on-rails/docs/misc/style)
|
|
3
3
|
|
|
4
4
|
## Autofix!
|
|
5
5
|
|
|
@@ -15,9 +15,9 @@ If you haven't tried the autofix options for `eslint` and `rubocop`, you're seri
|
|
|
15
15
|
```
|
|
16
16
|
eslint --fix .
|
|
17
17
|
```
|
|
18
|
-
|
|
19
|
-
or
|
|
20
|
-
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
21
|
```
|
|
22
22
|
npm run lint -- --fix
|
|
23
23
|
```
|
|
@@ -32,7 +32,7 @@ Rules are configured with a 0, 1 or 2. Setting a rule to 0 is turning it off, se
|
|
|
32
32
|
|
|
33
33
|
Rules can also take a few additional options. In this case, the rule can be set to an array, the first item of which is the 0/1/2 flag and the rest are options.
|
|
34
34
|
|
|
35
|
-
See file [.eslintrc](
|
|
35
|
+
See file [.eslintrc](https://github.com/shakacode/react_on_rails/tree/master/.eslintrc) for examples of configuration
|
|
36
36
|
|
|
37
37
|
### Specify/Override rules in code
|
|
38
38
|
|
|
@@ -66,4 +66,3 @@ You can disable all rules for a line or block, or only specific rules, as shown
|
|
|
66
66
|
* [ESLint quick start](http://untilfalse.com/eslint-quick-start/)
|
|
67
67
|
* [RuboCop](https://github.com/bbatsov/rubocop)
|
|
68
68
|
* [ESLint](http://eslint.org/)
|
|
69
|
-
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Pull Requests
|
|
1
|
+
# Pull Requests
|
|
2
2
|
|
|
3
3
|
## Checklist before Committing
|
|
4
4
|
1. `rake`: runs all linters and specs (you need Docker setup, see below)
|
|
@@ -36,9 +36,7 @@ When making doc changes, we want the change to work on both the gitbook and the
|
|
|
36
36
|
|
|
37
37
|
### Links to other docs:
|
|
38
38
|
* When making references to doc files, use a relative URL path like:
|
|
39
|
-
`[Installation Overview](docs/
|
|
39
|
+
`[Installation Overview](https://www.shakacode.com/react-on-rails/docs/additional-details/manual-installation-overview)`
|
|
40
40
|
|
|
41
41
|
* When making references to source code files, use a full url path like:
|
|
42
42
|
`[spec/dummy/config/initializers/react_on_rails.rb](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/initializers/react_on_rails.rb)`
|
|
43
|
-
|
|
44
|
-
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
We're now releasing this as a combined ruby gem plus npm package. We will keep the version numbers in sync.
|
|
4
4
|
|
|
5
5
|
## Testing the Gem before Release from a Rails App
|
|
6
|
-
See [Contributing](
|
|
6
|
+
See [Contributing](https://github.com/shakacode/react_on_rails/tree/master/CONTRIBUTING.md)
|
|
7
7
|
|
|
8
8
|
## Releasing a new gem version
|
|
9
9
|
Run `rake -D release` to see instructions on how to release via the rake task.
|