react_on_rails 12.0.0.pre.beta.2 → 12.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.travis.yml +8 -4
- data/CHANGELOG.md +28 -19
- data/NEWS.md +5 -0
- data/README.md +79 -80
- data/SUMMARY.md +2 -2
- data/docs/additional-reading/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
- data/docs/additional-reading/react-router.md +1 -1
- data/docs/additional-reading/recommended-project-structure.md +69 -0
- data/docs/additional-reading/server-rendering-tips.md +4 -1
- data/docs/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +2 -2
- data/docs/api/view-helpers-api.md +4 -4
- data/docs/basics/client-vs-server-rendering.md +2 -0
- data/docs/basics/configuration.md +21 -16
- data/docs/basics/deployment.md +2 -3
- data/docs/basics/heroku-deployment.md +24 -0
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +62 -9
- data/docs/basics/i18n.md +3 -3
- data/docs/basics/installation-into-an-existing-rails-app.md +2 -7
- 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 +3 -4
- data/docs/basics/upgrading-react-on-rails.md +37 -18
- data/docs/basics/webpack-configuration.md +12 -22
- data/docs/misc/doctrine.md +1 -2
- data/docs/outdated/code-splitting.md +3 -3
- data/docs/outdated/how-react-on-rails-works.md +8 -4
- data/docs/outdated/manual-installation-overview.md +1 -1
- data/docs/outdated/rails-assets.md +0 -7
- data/docs/tutorial.md +6 -0
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +2 -0
- data/lib/react_on_rails/configuration.rb +45 -6
- data/lib/react_on_rails/helper.rb +8 -8
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
- data/lib/react_on_rails/utils.rb +5 -1
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +10 -4
- data/lib/tasks/assets.rake +38 -14
- data/package.json +1 -1
- data/rakelib/examples.rake +1 -1
- data/rakelib/lint.rake +1 -1
- data/rakelib/release.rake +1 -3
- data/react_on_rails.gemspec +1 -0
- data/yarn.lock +260 -109
- metadata +21 -6
- data/docs/basics/recommended-project-structure.md +0 -94
- data/docs/outdated/heroku-deployment.md +0 -86
data/docs/additional-reading/converting-from-custom-webpack-config-to-rails-webpacker-config.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Converting from Custom Webpack Config to Rails Webpacker Config
|
2
|
+
|
3
|
+
1. Compare your package.json and the dependencies in https://github.com/rails/webpacker/blob/master/package.json#L14-L48
|
4
|
+
and avoid any duplicates. We don't want different versions of the same packages.
|
5
|
+
We want the versions from rails/webpacker unless we specifically want to override them.
|
6
|
+
2. Search the rails/webpacker repo for anything you're not sure about in terms of package names.
|
7
|
+
3. run `bin/webpack` and make sure there are zero errors
|
8
|
+
4. update webpack plugins and loaders to current or close to current
|
9
|
+
5. Make sure that your bin/webpack and bin/webpacker match the latest on
|
10
|
+
https://github.com/rails/webpacker/tree/master/lib/install/bin
|
@@ -39,7 +39,7 @@ For a fleshed out integration of react_on_rails with react-router, check out [Re
|
|
39
39
|
|
40
40
|
# Server Rendering Using React Router V4
|
41
41
|
|
42
|
-
Your
|
42
|
+
Your Render-Function may not return an object with the property `renderedHtml`. Thus, you call
|
43
43
|
renderToString() and return an object with this property.
|
44
44
|
|
45
45
|
This example **only applies to server rendering** and should be only used in the server side bundle.
|
@@ -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.
|
@@ -13,7 +13,7 @@ Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_o
|
|
13
13
|
- You can conditionally avoid running code that references document by either checking if `window`
|
14
14
|
is defined or using the "railsContext"
|
15
15
|
your top level react component. Since the passed in props Hash from the view helper applies to
|
16
|
-
client and server side code, the best way to do this is to use a
|
16
|
+
client and server side code, the best way to do this is to use a Render-Function.
|
17
17
|
- If you're serious about server rendering, it's worth the effort to have different entry points for client and server rendering. It's worth the extra complexity. The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server side.
|
18
18
|
- You can enable Node.js server rendering via [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
19
19
|
|
@@ -22,6 +22,9 @@ Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_o
|
|
22
22
|
1. First be sure your code works with server rendering disabled (`prerender: false`)
|
23
23
|
2. Be sure that `config.trace` is true. You will get the server invocation code that renders your component. If you're not using Webpacker, you will also get the whole file used to setup the JavaScript context.
|
24
24
|
|
25
|
+
## CSS
|
26
|
+
Server bundles must always have CSS Extracted
|
27
|
+
|
25
28
|
## setTimeout, setInterval, and clearTimeout
|
26
29
|
|
27
30
|
These methods are polyfilled for server rendering to be no-ops. We log calls to these when in `trace` mode. In the past, some libraries, namely babel-polyfill, did call setTimout.
|
data/docs/api/javascript-api.md
CHANGED
@@ -25,10 +25,10 @@ The best source of docs is the main [ReactOnRails.js](https://github.com/shakaco
|
|
25
25
|
/**
|
26
26
|
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
|
27
27
|
* find you components for rendering. Components get called with props, or you may use a
|
28
|
-
* "
|
28
|
+
* "Render-Function" to return a React component or an object with the following shape:
|
29
29
|
* { renderedHtml, redirectLocation, error }.
|
30
|
-
* For server rendering, if you wish to return multiple HTML strings from a
|
31
|
-
* you may return an Object from your
|
30
|
+
* For server rendering, if you wish to return multiple HTML strings from a Render-Function,
|
31
|
+
* you may return an Object from your Render-Function with a single top level property of
|
32
32
|
* renderedHtml. Inside this Object, place a key called componentHtml, along with any other
|
33
33
|
* needed keys. This is useful when you using side effects libraries like react helmet.
|
34
34
|
* Your Ruby code with get this Object as a Hash containing keys componentHtml and any other
|
data/docs/api/redux-store-api.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Redux Store
|
2
2
|
|
3
|
-
_This redux API is no longer recommended as it prevents dynamic code splitting for performance. Instead, you should use the standard react_component view helper passing in a "
|
3
|
+
_This redux API is no longer recommended as it prevents dynamic code splitting for performance. Instead, you should use the standard react_component view helper passing in a "Render-Function."_
|
4
4
|
|
5
5
|
You don't need to use the `redux_store` api to use redux. This api was setup to support multiple calls to `react_component` on one page that all talk to the same redux store.
|
6
6
|
|
7
|
-
If you are only rendering one react component on a page, as is typical to do a "Single Page App" in React, then you should _probably_ pass the props to your React component in a "
|
7
|
+
If you are only rendering one react component on a page, as is typical to do a "Single Page App" in React, then you should _probably_ pass the props to your React component in a "Render-Function."
|
8
8
|
|
9
9
|
Consider using the `redux_store` helper for the two following use cases:
|
10
10
|
|
@@ -22,7 +22,7 @@ Uncommonly used options:
|
|
22
22
|
id: nil,
|
23
23
|
```
|
24
24
|
|
25
|
-
- **component_name:** Can be a React component, created using an ES6 class or a
|
25
|
+
- **component_name:** Can be a React component, created using a React Function Component, an ES6 class or a Render-Function that returns a React component (or, only on the server side, an object with shape { redirectLocation, error, renderedHtml }), or a "renderer function" that manually renders a React component to the dom (client side only). Note, a "renderer function" is a special type of "Render-Function." A "renderer function" takes a 3rd param of a DOM ID.
|
26
26
|
All options except `props, id, html_options` will inherit from your `react_on_rails.rb` initializer, as described [here](../basics/configuration.md).
|
27
27
|
- **general options:**
|
28
28
|
- **props:** Ruby Hash which contains the properties to pass to the react object, or a JSON string. If you pass a string, we'll escape it for you.
|
@@ -45,7 +45,7 @@ adding meta-tags to a page. It is exactly like react_component except for the fo
|
|
45
45
|
|
46
46
|
1. `prerender: true` is automatically added to options, as this method doesn't make sense for
|
47
47
|
client only rendering.
|
48
|
-
2. Your JavaScript
|
48
|
+
2. Your JavaScript Render-Function for server rendering must return an Object rather than a React Component.
|
49
49
|
3. Your view code must expect an object and not a string.
|
50
50
|
|
51
51
|
Here is an example of ERB view code:
|
@@ -98,11 +98,11 @@ You can call `rails_context` or `rails_context(server_side: true|false)` from yo
|
|
98
98
|
|
99
99
|
### Renderer Functions (function that will call ReactDOM.render or ReactDOM.hydrate)
|
100
100
|
|
101
|
-
A "renderer function" is a
|
101
|
+
A "renderer function" is a Render-Function that accepts three arguments (rather than 2): `(props, railsContext, domNodeId) => { ... }`. Instead of returning a React component, a renderer is responsible for installing a callback that will call `ReactDOM.render` (in React 16+, `ReactDOM.hydrate`) to render a React component into the DOM. The "renderer function" is called at the same time the document ready event would instantate the React components into the DOM.
|
102
102
|
|
103
103
|
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](docs/outdated/code-splitting.md). In a nutshell, you don't want to load the React component on the DOM node yet. So you want to install some handler that will call `ReactDOM.hydrate` at a later time. In the case of code splitting with server rendering, the server rendered code has any async code loaded and used to server render. Thus, the client code must also fully load any asynch code before server rendering. Otherwise, the client code would first render partially, not matching the server rendering, and then a second later, the full code would render, resulting in an unpleasant flashing on the screen.
|
104
104
|
|
105
|
-
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a
|
105
|
+
Renderer functions are not meant to be used on the server since there's no DOM on the server. Instead, use a Render-Function. Attempting to server render with a renderer function will throw an error.
|
106
106
|
|
107
107
|
------------
|
108
108
|
|
@@ -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.
|
@@ -12,21 +12,27 @@ default: &default
|
|
12
12
|
# public_output_path folder
|
13
13
|
manifest: manifest.json
|
14
14
|
cache_manifest: false
|
15
|
+
|
16
|
+
# Source path is used to check if webpack compilation needs to be run for `compile: true`
|
15
17
|
source_path: client/app
|
16
18
|
|
17
19
|
development:
|
18
20
|
<<: *default
|
19
|
-
#
|
21
|
+
# Generated files for development, in /public/webpack/dev
|
20
22
|
public_output_path: webpack/dev
|
21
23
|
|
22
24
|
test:
|
23
25
|
<<: *default
|
24
|
-
#
|
26
|
+
# Ensure that webpacker invokes webpack to build files for tests if not using the
|
27
|
+
# ReactOnRails rspec helper.
|
28
|
+
compile: true
|
29
|
+
|
30
|
+
# Generated files for tests, in /public/webpack/test
|
25
31
|
public_output_path: webpack/test
|
26
32
|
|
27
33
|
production:
|
28
34
|
<<: *default
|
29
|
-
#
|
35
|
+
# Generated files for production, in /public/webpack/production
|
30
36
|
public_output_path: webpack/production
|
31
37
|
cache_manifest: true
|
32
38
|
```
|
@@ -45,7 +51,7 @@ ReactOnRails.configure do |config|
|
|
45
51
|
# The default is true for development, off otherwise.
|
46
52
|
# With true, you get detailed logs of rendering and stack traces if you call setTimout,
|
47
53
|
# setInterval, clearTimout when server rendering.
|
48
|
-
config.trace = Rails.env.development?
|
54
|
+
config.trace = Rails.env.development? # default
|
49
55
|
|
50
56
|
# Configure if default DOM IDs have a random value or are fixed.
|
51
57
|
# false ==> Sets the dom id to "#{react_component_name}-react-component"
|
@@ -54,18 +60,17 @@ ReactOnRails.configure do |config|
|
|
54
60
|
# it is convenient to set this to true or else you have to either manually set the ids to
|
55
61
|
# avoid collisions. Most newer apps will have only one instance of a component on a page,
|
56
62
|
# so this should be false in most cases.
|
57
|
-
# This value can be
|
58
|
-
config.random_dom_id =
|
63
|
+
# This value can be overridden for a given call to react_component
|
64
|
+
config.random_dom_id = true # default
|
59
65
|
|
60
|
-
# defaults to "" (top level)
|
61
|
-
#
|
62
|
-
|
63
|
-
|
64
|
-
#
|
65
|
-
# if you
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# If that file exists, React on Rails thinks that you'll use the rails/webpacker bin/webpack compiler.
|
66
|
+
# defaults to "" (top level)
|
67
|
+
config.node_modules_location = "client" # If using webpacker you should use "".
|
68
|
+
|
69
|
+
# This configures the script to run to build the production assets by webpack . Set this to nil
|
70
|
+
# if you don't want react_on_rails building this file for you.
|
71
|
+
# Note, if you want to use this command then you should remove the file
|
72
|
+
# config/webpack/production.js
|
73
|
+
# If that file exists, React on Rails thinks that you'll use the rails/webpacker bin/webpack compiler.
|
69
74
|
config.build_production_command = "RAILS_ENV=production bin/webpack"
|
70
75
|
|
71
76
|
################################################################################
|
@@ -210,7 +215,7 @@ Example of a RenderingExtension for custom values in the `rails_context`:
|
|
210
215
|
module RenderingExtension
|
211
216
|
|
212
217
|
# Return a Hash that contains custom values from the view context that will get merged with
|
213
|
-
# the standard rails_context values and passed to all calls to
|
218
|
+
# the standard rails_context values and passed to all calls to Render-Functions used by the
|
214
219
|
# react_component and redux_store view helpers
|
215
220
|
def self.custom_context(view_context)
|
216
221
|
{
|
data/docs/basics/deployment.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# Deployment
|
2
2
|
|
3
|
-
-
|
4
|
-
|
5
|
-
- See the [Heroku Deployment](docs/outdated/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
3
|
+
- `rails/webpacker` puts the necessary precompile steps automatically in the rake precompile step.
|
4
|
+
- See the [Heroku Deployment](docs/basics/heroku-deployment.md) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Heroku Deployment
|
2
|
+
## Heroku buildpacks
|
3
|
+
|
4
|
+
React on Rails requires both a ruby environment (for Rails) and a Node environment (for Webpack), so you will need to have Heroku use multiple buildpacks.
|
5
|
+
|
6
|
+
Assuming you have downloaded and installed the Heroku command-line utility and have initialized the app, you will need to tell Heroku to use both buildpacks via the command-line:
|
7
|
+
|
8
|
+
```
|
9
|
+
heroku buildpacks:set heroku/ruby
|
10
|
+
heroku buildpacks:add --index 1 heroku/nodejs
|
11
|
+
```
|
12
|
+
|
13
|
+
For more information, see [Using Multiple Buildpacks for an App](https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app)
|
14
|
+
|
15
|
+
## assets:precompile
|
16
|
+
|
17
|
+
### rails/webpacker webpack configuration
|
18
|
+
If you're using the standard rails/webpacker configuration of webpack, then rails/webpacker
|
19
|
+
will automatically modify or create an assets:precompile task to build your assets.
|
20
|
+
|
21
|
+
### custom webpack configuration
|
22
|
+
If you're a custom webpack configuration, and you **do not have the default
|
23
|
+
`config/webpack/production.js`** file, then the `config/initializers/react_on_rails.rb`
|
24
|
+
configuration `config.build_production_command` will be used.
|
@@ -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,64 @@ 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 isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;
|
63
|
+
|
64
|
+
//plugins
|
65
|
+
if(isWebpackDevServer) {
|
66
|
+
environment.plugins.append(
|
67
|
+
'ReactRefreshWebpackPlugin',
|
68
|
+
new ReactRefreshWebpackPlugin({
|
69
|
+
overlay: {
|
70
|
+
sockPort: 3035
|
71
|
+
}
|
72
|
+
})
|
73
|
+
);
|
74
|
+
}
|
75
|
+
```
|
76
|
+
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)`.
|
77
|
+
|
78
|
+
4. Add react-refresh plugin in `babel.config.js`
|
79
|
+
```
|
80
|
+
module.export = function(api) {
|
81
|
+
return {
|
82
|
+
plugins: [process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel'].filter(Boolean)
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
That's it :).
|
87
|
+
Now Browser should reflect .js along with .css changes without reloading.
|
88
|
+
|
89
|
+
If by some reason plugin doesn't work you could revert changes and left only devServer hmr/inline to true affecting only css files.
|
90
|
+
|
91
|
+
These plugins are working and tested with
|
92
|
+
- babel 7
|
93
|
+
- webpacker 5
|
94
|
+
- bootstrap 4
|
95
|
+
- jest 26
|
96
|
+
- core-js 3
|
97
|
+
- node 12.10.0
|
98
|
+
- react-refresh-webpack-plugin@0.4.1
|
99
|
+
- react-refresh 0.8.3
|
100
|
+
- react_on_rails 11.1.4
|
101
|
+
|
102
|
+
configuration.
|
data/docs/basics/i18n.md
CHANGED
@@ -25,7 +25,7 @@ Here's a summary of adding the I18n functionality.
|
|
25
25
|
|
26
26
|
3. Javascript locale files must be generated before `yarn build`.
|
27
27
|
|
28
|
-
Once you setup `config.i18n_dir` as in the previous step,
|
28
|
+
Once you setup `config.i18n_dir` as in the previous step, you will need to make sure `rake react_on_rails:locale` runs before webpack.
|
29
29
|
|
30
30
|
For development, you should adjust your startup scripts (Procfiles) so that they run **`bundle exec rake react_on_rails:locale`** before running any webpack watch process (`yarn run build:development`).
|
31
31
|
|
@@ -48,6 +48,7 @@ with `react-intl` supported via js files:
|
|
48
48
|
```
|
49
49
|
|
50
50
|
2. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && yarn install`.
|
51
|
+
Versions should be newer than these:
|
51
52
|
|
52
53
|
```js
|
53
54
|
"dependencies": {
|
@@ -93,7 +94,6 @@ with `react-intl` supported via js files:
|
|
93
94
|
```
|
94
95
|
|
95
96
|
# Notes
|
96
|
-
|
97
97
|
* See why using JSON could be better compare to JS if amount of data is hure [ https://v8.dev/blog/cost-of-javascript-2019#json]( https://v8.dev/blog/cost-of-javascript-2019#json).
|
98
98
|
* See [Support for Rails' i18n pluralization #1000](https://github.com/shakacode/react_on_rails/issues/1000) for a discussion of issues around pluralization.
|
99
|
-
*
|
99
|
+
* *Outdated:* You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340), [commmited](https://github.com/shakacode/react-webpack-rails-tutorial/commit/ef369ed9d922aea5116ca7e50208169fd7831389) for a complete example.
|
@@ -7,8 +7,8 @@
|
|
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 `client/package.json` file.
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem "react_on_rails", "
|
11
|
-
gem "webpacker", "~>
|
10
|
+
gem "react_on_rails", "12.0.0" # Update to the current version
|
11
|
+
gem "webpacker", "~> 5"
|
12
12
|
```
|
13
13
|
|
14
14
|
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).
|
@@ -57,8 +57,3 @@ $ yarn add react-on-rails --exact
|
|
57
57
|
```
|
58
58
|
|
59
59
|
That will install the latest version and update your package.json. **NOTE:** the `--exact` flag will ensure that you do not have a "~" or "^" for your react-on-rails version in your package.json.
|
60
|
-
|
61
|
-
## Webpacker Configuration
|
62
|
-
|
63
|
-
React on Rails users should set configuration value `compile` to false, as React on Rails handles compilation for test and production environments.
|
64
|
-
|
@@ -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/).
|