react_on_rails 12.0.1 → 12.0.5.beta.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 +1 -1
- data/.eslintrc +0 -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/.rubocop.yml +15 -44
- data/.travis.yml +3 -1
- data/CHANGELOG.md +20 -1
- data/CONTRIBUTING.md +1 -1
- data/Gemfile +1 -33
- data/Gemfile.development_dependencies +50 -0
- data/NEWS.md +5 -0
- data/README.md +26 -20
- data/SUMMARY.md +1 -1
- data/docs/additional-reading/recommended-project-structure.md +69 -0
- data/docs/basics/client-vs-server-rendering.md +2 -0
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +64 -9
- data/docs/basics/installation-into-an-existing-rails-app.md +11 -4
- data/docs/basics/react-server-rendering.md +8 -5
- data/docs/basics/upgrading-react-on-rails.md +37 -14
- data/docs/basics/webpack-configuration.md +12 -18
- data/docs/misc/doctrine.md +0 -1
- data/docs/outdated/code-splitting.md +2 -2
- data/docs/tutorial.md +19 -21
- data/lib/generators/react_on_rails/base_generator.rb +8 -1
- data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +4 -1
- data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
- 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 +10 -9
- 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.rb +2 -0
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
- data/lib/react_on_rails/utils.rb +8 -2
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +4 -4
- data/lib/tasks/assets.rake +6 -6
- data/package.json +1 -1
- data/rakelib/docker.rake +0 -5
- data/rakelib/lint.rake +3 -9
- data/rakelib/release.rake +9 -11
- data/rakelib/run_rspec.rake +10 -11
- data/rakelib/task_helpers.rb +2 -3
- data/react_on_rails.gemspec +3 -17
- metadata +16 -194
- 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/CONTRIBUTING.md
CHANGED
data/Gemfile
CHANGED
@@ -5,36 +5,4 @@ source "https://rubygems.org"
|
|
5
5
|
# Specify your gem"s dependencies in react_on_rails.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
|
9
|
-
# They must be defined here because of the way Travis CI works, in that it will only
|
10
|
-
# bundle install from a single Gemfile. Therefore, all gems that we will need for any dummy/example
|
11
|
-
# app have to be manually added to this file.
|
12
|
-
gem "bootsnap", ">= 1.1.0", require: false
|
13
|
-
gem "bootstrap-sass"
|
14
|
-
gem "jbuilder"
|
15
|
-
gem "jquery-rails"
|
16
|
-
gem "mini_racer"
|
17
|
-
gem "puma"
|
18
|
-
gem "rails_12factor"
|
19
|
-
gem "ruby-lint", require: false
|
20
|
-
gem "sass-rails"
|
21
|
-
gem "scss_lint", require: false
|
22
|
-
gem "sdoc", group: :doc
|
23
|
-
gem "spring"
|
24
|
-
gem "sprockets", "~>3.0"
|
25
|
-
gem "sqlite3", "~> 1.3.6"
|
26
|
-
gem "turbolinks"
|
27
|
-
gem "uglifier"
|
28
|
-
gem "web-console", group: :development
|
29
|
-
|
30
|
-
# below are copied from spec/dummy/Gemfile
|
31
|
-
gem "capybara"
|
32
|
-
gem "capybara-screenshot"
|
33
|
-
gem "launchy"
|
34
|
-
gem "rspec-rails"
|
35
|
-
gem "rspec-retry"
|
36
|
-
gem "selenium-webdriver"
|
37
|
-
gem "webpacker", ">= 4.0.0"
|
38
|
-
|
39
|
-
gem "equivalent-xml", github: "mbklein/equivalent-xml"
|
40
|
-
gem "rainbow"
|
8
|
+
eval_gemfile File.expand_path("./Gemfile.development_dependencies", __dir__)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
gem "webpacker"
|
4
|
+
gem "bootsnap", ">= 1.1.0", require: false
|
5
|
+
gem "rails"
|
6
|
+
gem "sqlite3"
|
7
|
+
gem "sass-rails"
|
8
|
+
gem "uglifier"
|
9
|
+
gem "jquery-rails"
|
10
|
+
gem "puma"
|
11
|
+
|
12
|
+
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
|
13
|
+
gem "turbolinks" if ENV["DISABLE_TURBOLINKS"].nil? || ENV["DISABLE_TURBOLINKS"].strip.empty?
|
14
|
+
|
15
|
+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
|
16
|
+
gem "jbuilder"
|
17
|
+
# bundle exec rake doc:rails generates the API under doc/api.
|
18
|
+
gem "sdoc", group: :doc
|
19
|
+
|
20
|
+
gem "sprockets"
|
21
|
+
|
22
|
+
gem "amazing_print"
|
23
|
+
gem "mini_racer"
|
24
|
+
|
25
|
+
group :development, :test do
|
26
|
+
gem "listen"
|
27
|
+
gem "pry"
|
28
|
+
gem "pry-byebug"
|
29
|
+
gem "pry-doc"
|
30
|
+
gem "pry-rails"
|
31
|
+
gem "pry-rescue"
|
32
|
+
gem "rubocop", require: false
|
33
|
+
gem "rubocop-performance", require: false
|
34
|
+
gem "scss_lint", require: false
|
35
|
+
gem "spring"
|
36
|
+
end
|
37
|
+
|
38
|
+
group :test do
|
39
|
+
gem "capybara"
|
40
|
+
gem "capybara-screenshot"
|
41
|
+
gem "coveralls", require: false
|
42
|
+
gem "equivalent-xml"
|
43
|
+
gem "generator_spec"
|
44
|
+
gem "launchy"
|
45
|
+
gem "rspec_junit_formatter"
|
46
|
+
gem "rspec-rails"
|
47
|
+
gem "rspec-retry"
|
48
|
+
gem "selenium-webdriver"
|
49
|
+
gem "webdrivers", "~> 3.0"
|
50
|
+
end
|
data/NEWS.md
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
|
3
3
|
*We'll keep a history of the news. A few bullets at the top will also show on the [README.md](./README.md).*
|
4
4
|
|
5
|
+
* July 8, 2020: Release **v12**.
|
6
|
+
1. **React Hooks Support** for top level components
|
7
|
+
2. **Typescript bindings**
|
8
|
+
3. **rails/webpacker** "just works" with React on Rails by default.
|
9
|
+
4. i18n support for generating a JSON file rather than a JS file.
|
5
10
|
* 2018-02-27: **Version 10.1.2** Supports the React API for ReactDOM.hydrate.
|
6
11
|
* 2017-09-06: **VERSION 9.0.0 shipped!** This version depends on Webpacker directly. See [Upgrading React on Rails](./docs/basics/upgrading-react-on-rails.md) for more concise instructions on upgrading.
|
7
12
|
* Always see the [CHANGELOG.md](./CHANGELOG.md) for the latest project changes.
|
data/README.md
CHANGED
@@ -14,6 +14,15 @@
|
|
14
14
|
*These are the docs for React on Rails 12. To see the version 11 docs, [click here](https://github.com/shakacode/react_on_rails/tree/11.3.0).*
|
15
15
|
|
16
16
|
#### News
|
17
|
+
|
18
|
+
**October 14, 2020**: [RUBY ROGUES
|
19
|
+
RR 474: React on Rails V12 – Don’t Shave That Yak! with Justin Gordon](https://devchat.tv/ruby-rogues/rr-474-react-on-rails-v12-dont-shave-that-yak-with-justin-gordon/).
|
20
|
+
|
21
|
+
**October 1, 2020**: See the [spec/dummy](./spec/dummy) example repo for a simple configuration of webpack via the rails/webpacker gem
|
22
|
+
that supports SSR.
|
23
|
+
|
24
|
+
**August 2, 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`.
|
25
|
+
|
17
26
|
**July 8, 2020**: Release v12 of React on Rails.
|
18
27
|
|
19
28
|
**Major Improvements**
|
@@ -27,16 +36,14 @@ Be sure to see the [CHANGELOG.md](./CHANGELOG.md) and read the upgrade instructi
|
|
27
36
|
[docs/basics/upgrading-react-on-rails](./docs/basics/upgrading-react-on-rails.md#upgrading-to-v12).
|
28
37
|
|
29
38
|
* See Justin's RailsConf talk: [Webpacker, It-Just-Works, But How?](http://railsconf.com/2020/video/justin-gordon-webpacker-it-just-works-but-how).
|
30
|
-
* Are you interested in support for React on Rails? If so check out [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
39
|
+
* Are you interested in support for React on Rails? Do you want to use Node.js to do your server-side rendering so libraries like Emotion and Loadable Components just work, as compared to rendering via Ruby embedded JS? If so check out [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
31
40
|
* HMR is working with [Loadable Components](https://loadable-components.com) for a both amazing hot-reloading developer experience and great runtime performance. Please [email me](mailto:justin@shakacode.com) if you'd like to use [Loadable Components Code Splitting](https://loadable-components.com/docs/code-splitting/) to speed up your app by reducing your bundle sizes and lazily loading the code that's needed.
|
32
|
-
* `react_on_rails` fully supports `rails/webpacker`. The example test app in `spec/dummy` was recently converted over to use rails/webpacker v4+. It's a good example of how to leverage rails/webpacker's webpack configuration.
|
33
|
-
|
34
41
|
---
|
35
42
|
|
36
43
|
#### About
|
37
44
|
React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
|
38
45
|
|
39
|
-
This project is
|
46
|
+
This project is maintained by the software consulting firm [ShakaCode](https://www.shakacode.com). We focus on Ruby on Rails applications with React front-ends, often using TypeScript or ReasonML. We also build Gatsby sites. See [our recent work](https://www.shakacode.com/recent-work) for examples of what we do.
|
40
47
|
|
41
48
|
Interested in optimizing your webpack setup for React on Rails including code
|
42
49
|
splitting with [react-router](https://github.com/ReactTraining/react-router#readme),
|
@@ -62,7 +69,7 @@ Given that `rails/webpacker` gem already provides basic React integration, why w
|
|
62
69
|
1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `rails/webpacker`.
|
63
70
|
1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration with server-side-rendering.
|
64
71
|
1. [Internationalization (I18n) and (localization)](https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md)
|
65
|
-
1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com/).
|
72
|
+
1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com+depth%3Aall/).
|
66
73
|
1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example).
|
67
74
|
|
68
75
|
See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
|
@@ -87,35 +94,35 @@ Ruby on Rails >=5 and rails/webpacker 4.2+.
|
|
87
94
|
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.
|
88
95
|
|
89
96
|
1. Do the quick [tutorial](docs/tutorial.md).
|
90
|
-
|
91
|
-
|
92
|
-
|
97
|
+
1. Add React on Rails to an existing Rails app per [the instructions](docs/basics/installation-into-an-existing-rails-app.md).
|
98
|
+
1. Look at [spec/dummy](spec/dummy), a simple, no DB example. This has an example of using `rails/webpacker` to configure server-side rendering.
|
99
|
+
1. 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`.
|
100
|
+
1. Look at [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial); it's a full-featured example live at [www.reactrails.com](http://www.reactrails.com).
|
93
101
|
|
94
102
|
## Basic Installation
|
95
103
|
|
96
104
|
*See also [the instructions for installing into an existing Rails app](docs/basics/installation-into-an-existing-rails-app.md).*
|
97
105
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
4. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
106
|
+
1. Add the `rails/webpacker` gem if you don't yet have it.
|
107
|
+
|
108
|
+
1. Add the `react_on_rails` gem to Gemfile. _Use the latest version for react_on_rails._
|
109
|
+
```bash
|
110
|
+
bundle add react_on_rails --version=12.0.4 --strict
|
111
|
+
```
|
105
112
|
|
106
|
-
|
113
|
+
1. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
107
114
|
|
115
|
+
1. Run the generator:
|
108
116
|
```bash
|
109
117
|
rails generate react_on_rails:install
|
110
118
|
```
|
111
119
|
|
112
|
-
|
113
|
-
|
120
|
+
1. Start the app:
|
114
121
|
```bash
|
115
122
|
rails s
|
116
123
|
```
|
117
124
|
|
118
|
-
|
125
|
+
1. Visit http://localhost:3000/hello_world.
|
119
126
|
|
120
127
|
|
121
128
|
### Turning on server rendering
|
@@ -275,7 +282,6 @@ Browse the links in the [Summary Table of Contents](./SUMMARY.md)
|
|
275
282
|
Here are some highly recommended next articles to read:
|
276
283
|
|
277
284
|
1. [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
|
278
|
-
1. [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
|
279
285
|
1. [Webpack Configuration](./docs/basics/webpack-configuration.md)
|
280
286
|
1. [View Helpers API](./docs/api/view-helpers-api.md)
|
281
287
|
1. [Caching and Performance: React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
data/SUMMARY.md
CHANGED
@@ -8,7 +8,6 @@
|
|
8
8
|
+ [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
|
9
9
|
+ [Client vs. Server Rendering](./docs/basics/client-vs-server-rendering.md)
|
10
10
|
+ [React Server Rendering](./docs/basics/react-server-rendering.md)
|
11
|
-
+ [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
|
12
11
|
+ [Render-Functions and the RailsContext](docs/basics/render-functions-and-railscontext.md)
|
13
12
|
+ [Caching and Performance: React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
14
13
|
+ [Deployment](docs/basics/deployment.md).
|
@@ -28,6 +27,7 @@
|
|
28
27
|
+ [Updating Dependencies](./docs/additional-reading/updating-dependencies.md)
|
29
28
|
+ [Manual Installation Overview](docs/outdated/manual-installation-overview.md)
|
30
29
|
+ [Upgrading from rails/webpacker v3 to v4](docs/additional-reading/upgrade-webpacker-v3-to-v4.md)
|
30
|
+
+ [Recommended Project Structure](docs/additional-reading/recommended-project-structure.md)
|
31
31
|
|
32
32
|
## **Rails**
|
33
33
|
+ [Rails Engine Integration](./docs/additional-reading/rails-engine-integration.md)
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Recommended Project structure
|
2
|
+
|
3
|
+
The React on Rails generator uses the standard `rails/webpacker` convention of this structure:
|
4
|
+
|
5
|
+
```yml
|
6
|
+
app/javascript:
|
7
|
+
├── bundles:
|
8
|
+
│ # Logical groups of files that can be used for code splitting
|
9
|
+
│ └── hello-world-bundle.js
|
10
|
+
├── packs:
|
11
|
+
│ # only webpack entry files here
|
12
|
+
│ └── hello-world-bundle.js
|
13
|
+
```
|
14
|
+
|
15
|
+
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),
|
16
|
+
you should consider keeping your codebase mostly consistent with the defaults for [rails/webpacker](https://github.com/rails/webpacker).
|
17
|
+
|
18
|
+
## Steps to convert from the generator defaults to use a `/client` directory for source code
|
19
|
+
|
20
|
+
1. Move the directory:
|
21
|
+
|
22
|
+
```
|
23
|
+
mv app/javascript client
|
24
|
+
```
|
25
|
+
|
26
|
+
2. Edit your `/config/webpacker.yml` file. Change the `default/source_path`:
|
27
|
+
|
28
|
+
```yml
|
29
|
+
source_path: client
|
30
|
+
```
|
31
|
+
|
32
|
+
## Moving node_modules from `/` to `/client` with a custom webpack setup.
|
33
|
+
|
34
|
+
`rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration.
|
35
|
+
|
36
|
+
1. Move the `/package.json` to `/client/package.json`
|
37
|
+
2. Create a `/package.json` that delegates to `/client/package.json`.
|
38
|
+
```
|
39
|
+
"scripts": {
|
40
|
+
"heroku-postbuild": "cd ./client && yarn"
|
41
|
+
},
|
42
|
+
```
|
43
|
+
3. If your node_modules directory is not at the top level of the Rails project, then you will need to set the
|
44
|
+
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).
|
45
|
+
|
46
|
+
## CSS, Sass, Fonts, and Images
|
47
|
+
Should you move your styling assets to Webpack? Or stick with the plain Rails asset pipeline. It depends!
|
48
|
+
|
49
|
+
Here's a good discussion of this option: [Why does Rails 6 include both Webpacker and Sprockets?](https://rossta.net/blog/why-does-rails-install-both-webpacker-and-sprockets.html).
|
50
|
+
|
51
|
+
You have 2 basic choices:
|
52
|
+
|
53
|
+
### Simple Rails Way
|
54
|
+
This isn't really any technique, as you keep handling all your styling assets using Rails standard tools, such as using the [sass-rails gem](https://rubygems.org/gems/sass-rails/versions/5.0.4). Basically, Webpack doesn't get involved with styling. Your Rails layouts just doing the styling the standard Rails way.
|
55
|
+
|
56
|
+
#### Advantages to the Simple Rails Way
|
57
|
+
1. Much simpler! There's no changes really from your current processes.
|
58
|
+
|
59
|
+
### Using Webpack to Manage Styling Assets
|
60
|
+
This technique involves customization of the webpack config files to generate CSS, image, and font assets.
|
61
|
+
|
62
|
+
#### Directory structure
|
63
|
+
1. `/client/app/assets`: Assets for CSS for client app.
|
64
|
+
1. `/client/app/assets/fonts` and `/client/app/assets/styles`: Globally shared assets for styling. Note, most Sass and image assets will be stored next to the JavaScript files.
|
65
|
+
|
66
|
+
#### Advantages to having Webpack Manage Styles
|
67
|
+
1. You can use [CSS modules](https://github.com/css-modules/css-modules), which is super compelling once you seen the benefits.
|
68
|
+
1. You can use CSS in JS.
|
69
|
+
1. You can do hot reloading of your assets. Thus, you do not have to refresh your web page to see asset change, including changing styles.
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# Client-Side Rendering vs. Server-Side Rendering
|
2
2
|
|
3
|
+
*See also [react-server-rendering.md](./react-server-rendering.md).*
|
4
|
+
|
3
5
|
In most cases, you should use the `prerender: false` (default behavior) with the provided 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`.
|
4
6
|
|
5
7
|
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. We recommend using [mini_racer](https://github.com/discourse/mini_racer) as ExecJS's runtime.
|
@@ -8,22 +8,18 @@ The webpack-dev-server provides:
|
|
8
8
|
abruptly lose any tweaks within the Chrome development tools.
|
9
9
|
3. Optional hot-reloading. The older react-hot-loader has been deprecated in
|
10
10
|
favor of [fast-refresh](https://reactnative.dev/docs/fast-refresh).
|
11
|
-
For use with webpack, see [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
|
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
14
|
then you can follow all the regular docs for using the `bin/webpack-dev-server`
|
15
15
|
during development.
|
16
16
|
|
17
|
-
|
18
17
|
# Server Side Rendering with the Default rails/webpacker bin/webpack-dev-server
|
19
18
|
|
20
19
|
If you are using server-side rendering, then you have a couple options. The
|
21
20
|
recommended technique is to have a different webpack configuration for server
|
22
21
|
rendering.
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
23
|
## If you use the same Webpack setup for your server and client bundles
|
28
24
|
If you do use the webpack-dev-server for prerendering, be sure to set the
|
29
25
|
`config/initializers/react_on_rails.rb` setting of
|
@@ -43,7 +39,66 @@ If you don't configure these two to false, you'll see errors like:
|
|
43
39
|
* "ReferenceError: window is not defined" (if hmr is true)
|
44
40
|
* "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
|
45
41
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
# Client Side rendering with HMR using react-refresh-webpack-plugin
|
43
|
+
## Basic installation
|
44
|
+
To enable HMR functionality you have to use `./bin/webpack-dev-server`
|
45
|
+
1. In `config/webpacker.yml` set **hmr** and **inline** `dev_server` properties to true.
|
46
|
+
```
|
47
|
+
dev_server:
|
48
|
+
https: false
|
49
|
+
host: localhost
|
50
|
+
port: 3035
|
51
|
+
public: localhost:3035
|
52
|
+
hmr: true
|
53
|
+
# Inline should be set to true if using HMR
|
54
|
+
inline: true
|
55
|
+
```
|
56
|
+
|
57
|
+
2. Add react refresh packages:
|
58
|
+
` yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
|
59
|
+
|
60
|
+
3. HMR is for use with the webpack-dev-server, so we only add this for the webpack-dev-server.
|
61
|
+
```
|
62
|
+
const { devServer } = require('@rails/webpacker')
|
63
|
+
|
64
|
+
const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER
|
65
|
+
|
66
|
+
//plugins
|
67
|
+
if (isWebpackDevServer) {
|
68
|
+
environment.plugins.append(
|
69
|
+
'ReactRefreshWebpackPlugin',
|
70
|
+
new ReactRefreshWebpackPlugin({
|
71
|
+
overlay: {
|
72
|
+
sockPort: devServer.port
|
73
|
+
}
|
74
|
+
})
|
75
|
+
)
|
76
|
+
}
|
77
|
+
```
|
78
|
+
We added overlay.sockedPort option in `ReactRefreshWebpackPlugin` to match the webpack dev-server port specified in config/webpacker.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
|
+
|
80
|
+
4. Add react-refresh plugin in `babel.config.js`
|
81
|
+
```
|
82
|
+
module.export = function(api) {
|
83
|
+
return {
|
84
|
+
plugins: [process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel'].filter(Boolean)
|
85
|
+
}
|
86
|
+
}
|
87
|
+
```
|
88
|
+
That's it :).
|
89
|
+
Now Browser should reflect .js along with .css changes without reloading.
|
90
|
+
|
91
|
+
If by some reason plugin doesn't work you could revert changes and left only devServer hmr/inline to true affecting only css files.
|
92
|
+
|
93
|
+
These plugins are working and tested with
|
94
|
+
- babel 7
|
95
|
+
- webpacker 5
|
96
|
+
- bootstrap 4
|
97
|
+
- jest 26
|
98
|
+
- core-js 3
|
99
|
+
- node 12.10.0
|
100
|
+
- react-refresh-webpack-plugin@0.4.1
|
101
|
+
- react-refresh 0.8.3
|
102
|
+
- react_on_rails 11.1.4
|
103
|
+
|
104
|
+
configuration.
|
@@ -4,19 +4,26 @@
|
|
4
4
|
|
5
5
|
**If you have rails-5 API only project**, first [convert the rails-5 API only app to rails app](#convert-rails-5-api-only-app-to-rails-app) before [getting started](#getting-started-with-an-existing-rails-app).
|
6
6
|
|
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 `
|
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
10
|
gem "react_on_rails", "12.0.0" # Update to the current version
|
11
11
|
gem "webpacker", "~> 5"
|
12
12
|
```
|
13
13
|
|
14
|
-
|
14
|
+
1. Add the webpacker and react_on_rails gems
|
15
|
+
_Use the latest version for react_on_rails._
|
15
16
|
|
17
|
+
```
|
18
|
+
bundle add webpacker
|
19
|
+
bundle add react_on_rails --version=12.0.4 --strict
|
20
|
+
```
|
21
|
+
|
22
|
+
2. Run the following 2 commands to install Webpacker 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).
|
16
23
|
|
17
24
|
```bash
|
18
|
-
|
19
|
-
|
25
|
+
bundle exec rails webpacker:install
|
26
|
+
bundle exec rails webpacker:install:react
|
20
27
|
```
|
21
28
|
|
22
29
|
3. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
@@ -1,6 +1,9 @@
|
|
1
1
|
# React Server Rendering
|
2
2
|
|
3
|
-
See also [Client vs. Server Rendering](./client-vs-server-rendering.md)
|
3
|
+
See also [Client vs. Server Rendering](./client-vs-server-rendering.md).
|
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](docs/outdated/how-react-on-rails-works.md#client-side-rendering-vs-server-side-rendering)
|
14
|
-
|
16
|
+
See [this note](docs/outdated/how-react-on-rails-works.md#client-side-rendering-vs-server-side-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/).
|