react_on_rails 12.0.5.beta.0 → 12.4.0.rc.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +23 -11
- data/.eslintrc +2 -0
- data/.prettierrc +0 -3
- data/.rubocop.yml +40 -2
- data/CHANGELOG.md +56 -26
- data/CONTRIBUTING.md +3 -2
- data/Gemfile.development_dependencies +4 -3
- data/NEWS.md +9 -4
- data/README.md +28 -323
- data/Rakefile +1 -1
- data/SUMMARY.md +12 -12
- data/docs/{basics → additional-details}/generator-details.md +1 -6
- data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
- data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
- data/docs/{additional-reading → additional-details}/recommended-project-structure.md +0 -0
- data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
- data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
- data/docs/api/javascript-api.md +2 -2
- data/docs/api/redux-store-api.md +3 -3
- data/docs/api/view-helpers-api.md +7 -8
- 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/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
- data/docs/{basics → deployment}/heroku-deployment.md +0 -0
- data/docs/{basics → guides}/client-vs-server-rendering.md +3 -3
- data/docs/{basics → guides}/configuration.md +45 -25
- data/docs/guides/deployment.md +4 -0
- data/docs/guides/getting-started.md +183 -0
- data/docs/{basics → guides}/hmr-and-hot-reloading-with-the-webpack-dev-server.md +0 -0
- data/docs/{outdated → guides}/how-react-on-rails-works.md +9 -8
- data/docs/guides/how-to-conditionally-server-render-based-on-device-type.md +39 -0
- data/docs/guides/how-to-use-different-files-for-client-and-server-rendering.md +98 -0
- data/docs/{basics → guides}/i18n.md +0 -0
- data/docs/{basics → guides}/installation-into-an-existing-rails-app.md +3 -3
- data/docs/{basics → guides}/minitest-configuration.md +0 -0
- data/docs/guides/rails-webpacker-react-integration-options.md +182 -0
- data/docs/guides/react-on-rails-overview.md +30 -0
- data/docs/{basics → guides}/react-server-rendering.md +3 -3
- data/docs/{basics → guides}/render-functions-and-railscontext.md +0 -0
- data/docs/{basics → guides}/rspec-configuration.md +10 -10
- data/docs/{tutorial.md → guides/tutorial.md} +9 -11
- data/docs/{basics → guides}/upgrading-react-on-rails.md +7 -7
- data/docs/{basics → guides}/webpack-configuration.md +4 -6
- data/docs/home.md +23 -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/{outdated → javascript}/code-splitting.md +3 -3
- data/docs/{additional-reading → javascript}/converting-from-custom-webpack-config-to-rails-webpacker-config.md +3 -3
- data/docs/{additional-reading → javascript}/credits.md +0 -0
- data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
- data/docs/{additional-reading → javascript}/images.md +5 -6
- 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}/react-router.md +0 -0
- data/docs/{additional-reading → javascript}/server-rendering-tips.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/{articles.md → misc/articles.md} +1 -1
- data/docs/misc/doctrine.md +5 -5
- data/docs/{coding-style → misc}/style.md +0 -0
- data/docs/{additional-reading → misc}/tips.md +0 -0
- 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/{additional-reading → rails}/rails-engine-integration.md +3 -12
- data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
- data/docs/{additional-reading → rails}/turbolinks.md +13 -1
- data/docs/react-on-rails-pro/react-on-rails-pro.md +43 -0
- data/docs/testimonials/testimonials.md +6 -6
- data/lib/generators/react_on_rails/base_generator.rb +1 -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 +4 -1
- data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
- data/lib/react_on_rails/helper.rb +26 -2
- data/lib/react_on_rails/locales/base.rb +7 -9
- data/lib/react_on_rails/react_component/render_options.rb +16 -7
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +11 -0
- data/lib/react_on_rails/utils.rb +16 -2
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/webpacker_utils.rb +5 -1
- data/lib/tasks/assets.rake +17 -6
- data/package.json +24 -29
- data/rakelib/release.rake +22 -6
- data/rakelib/task_helpers.rb +15 -2
- data/yarn.lock +2549 -5169
- metadata +56 -49
- data/.release-it.json +0 -3
- data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
- data/docs/basics/deployment.md +0 -4
@@ -23,7 +23,7 @@ Uncommonly used options:
|
|
23
23
|
```
|
24
24
|
|
25
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
|
-
All options except `props, id, html_options` will inherit from your `react_on_rails.rb` initializer, as described [here](
|
26
|
+
All options except `props, id, html_options` will inherit from your `react_on_rails.rb` initializer, as described [here](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
|
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.
|
29
29
|
- **prerender:** enable server-side rendering of a component. Set to false when debugging!
|
@@ -43,7 +43,7 @@ Uncommonly used options:
|
|
43
43
|
`react_component_hash` is used to return multiple HTML strings for server rendering, such as for
|
44
44
|
adding meta-tags to a page. It is exactly like react_component except for the following:
|
45
45
|
|
46
|
-
1. `prerender: true` is automatically added to options, as this method doesn't make sense for
|
46
|
+
1. `prerender: true` is automatically added to options, as this method doesn't make sense for
|
47
47
|
client only rendering.
|
48
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.
|
@@ -100,7 +100,7 @@ You can call `rails_context` or `rails_context(server_side: true|false)` from yo
|
|
100
100
|
|
101
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
|
-
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](docs/
|
103
|
+
Why would you want to call `ReactDOM.hydrate` yourself? One possible use case is [code splitting](https://www.shakacode.com/react-on-rails/docs/javascript/code-splitting/). 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
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
|
|
@@ -110,9 +110,9 @@ Renderer functions are not meant to be used on the server since there's no DOM o
|
|
110
110
|
|
111
111
|
[React Router](https://github.com/reactjs/react-router) is supported, including server-side rendering! See:
|
112
112
|
|
113
|
-
1. [React on Rails docs for react-router](
|
114
|
-
2. Examples in [spec/dummy/app/views/react_router](
|
115
|
-
3. [Code Splitting docs](docs/
|
113
|
+
1. [React on Rails docs for react-router](https://www.shakacode.com/react-on-rails/docs/javascript/react-router/)
|
114
|
+
2. Examples in [spec/dummy/app/views/react_router](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/app/views/react_router) and follow to the JavaScript code in the [spec/dummy/client/app/startup/ServerRouterApp.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/ServerRouterApp.jsx).
|
115
|
+
3. [Code Splitting docs](https://www.shakacode.com/react-on-rails/docs/javascript/code-splitting/) for information about how to set up code splitting for server rendered routes.
|
116
116
|
|
117
117
|
------------
|
118
118
|
|
@@ -129,5 +129,4 @@ This is a helper method that takes any JavaScript expression and returns the out
|
|
129
129
|
|
130
130
|
# More details
|
131
131
|
|
132
|
-
See the [lib/react_on_rails/helper.rb](
|
133
|
-
|
132
|
+
See the [lib/react_on_rails/helper.rb](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/helper.rb) source.
|
@@ -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.
|
File without changes
|
File without changes
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# Client-Side Rendering vs. Server-Side Rendering
|
2
2
|
|
3
|
-
*
|
3
|
+
*Also, see [our react server-rendering documentation](https://www.shakacode.com/react-on-rails/docs/guides/react-server-rendering/).*
|
4
4
|
|
5
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`.
|
6
6
|
|
7
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
|
-
|
8
|
+
|
9
9
|
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).
|
10
10
|
|
11
11
|
If you open the HTML source of any web page using React on Rails, you'll see the 3 parts of React on Rails rendering:
|
@@ -18,7 +18,7 @@ If you open the HTML source of any web page using React on Rails, you'll see the
|
|
18
18
|
|
19
19
|
# Different Server-Side Rendering Code (and a Server-Specific Bundle)
|
20
20
|
|
21
|
-
You may want different code for your server-rendered components running server side versus client side. For example, if you have an animation that runs when a component is displayed, you might need to turn that off when server rendering. One way to handle this is conditional code like `if (window) { doClientOnlyCode() }`.
|
21
|
+
You may want different code for your server-rendered components running server side versus client side. For example, if you have an animation that runs when a component is displayed, you might need to turn that off when server rendering. One way to handle this is conditional code like `if (window) { doClientOnlyCode() }`.
|
22
22
|
|
23
23
|
Another way is to use a separate webpack configuration file that can use a different server side entry file, like 'serverRegistration.js' as opposed to 'clientRegistration.js.' That would set up different code for server rendering.
|
24
24
|
|
@@ -12,7 +12,7 @@ default: &default
|
|
12
12
|
# public_output_path folder
|
13
13
|
manifest: manifest.json
|
14
14
|
cache_manifest: false
|
15
|
-
|
15
|
+
|
16
16
|
# Source path is used to check if webpack compilation needs to be run for `compile: true`
|
17
17
|
source_path: client/app
|
18
18
|
|
@@ -24,7 +24,7 @@ development:
|
|
24
24
|
test:
|
25
25
|
<<: *default
|
26
26
|
# Ensure that webpacker invokes webpack to build files for tests if not using the
|
27
|
-
# ReactOnRails rspec helper.
|
27
|
+
# ReactOnRails rspec helper.
|
28
28
|
compile: true
|
29
29
|
|
30
30
|
# Generated files for tests, in /public/webpack/test
|
@@ -49,7 +49,7 @@ for all client files, including your sources and node_modules.
|
|
49
49
|
ReactOnRails.configure do |config|
|
50
50
|
# `trace`: General debugging flag.
|
51
51
|
# The default is true for development, off otherwise.
|
52
|
-
# With true, you get detailed logs of rendering and stack traces if you call setTimout,
|
52
|
+
# With true, you get detailed logs of rendering and stack traces if you call setTimout,
|
53
53
|
# setInterval, clearTimout when server rendering.
|
54
54
|
config.trace = Rails.env.development? # default
|
55
55
|
|
@@ -57,22 +57,30 @@ ReactOnRails.configure do |config|
|
|
57
57
|
# false ==> Sets the dom id to "#{react_component_name}-react-component"
|
58
58
|
# true ==> Adds "-#{SecureRandom.uuid}" to that ID
|
59
59
|
# If you might use multiple instances of the same React component on a Rails page, then
|
60
|
-
# it is convenient to set this to true or else you have to either manually set the ids to
|
60
|
+
# it is convenient to set this to true or else you have to either manually set the ids to
|
61
61
|
# avoid collisions. Most newer apps will have only one instance of a component on a page,
|
62
62
|
# so this should be false in most cases.
|
63
63
|
# This value can be overridden for a given call to react_component
|
64
64
|
config.random_dom_id = true # default
|
65
65
|
|
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.
|
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.
|
74
74
|
config.build_production_command = "RAILS_ENV=production bin/webpack"
|
75
75
|
|
76
|
+
# Alternatively, you can also specify a module containing a class method `call`
|
77
|
+
# In this example, the module BuildProductionCommand would have a class method `call`.
|
78
|
+
# See bottom for an example of the BuildProductionCommand module.
|
79
|
+
# config.build_production_command = BuildProductionCommand
|
80
|
+
# If you wish to utilize ReactOnRailsPro production bundle caching logic, then use
|
81
|
+
# config.build_production_command = ReactOnRailsPro::AssetsPrecompile
|
82
|
+
# and be sure to check ReactOnRailsPro's configuration documentation!
|
83
|
+
|
76
84
|
################################################################################
|
77
85
|
################################################################################
|
78
86
|
# SERVER RENDERING OPTIONS
|
@@ -89,9 +97,9 @@ ReactOnRails.configure do |config|
|
|
89
97
|
# you should include a name that matches your bundle name in your webpack config.
|
90
98
|
config.server_bundle_js_file = "server-bundle.js"
|
91
99
|
|
92
|
-
# THE BELOW OPTIONS FOR SERVER-SIDE RENDERING RARELY NEED CHANGING
|
100
|
+
# THE BELOW OPTIONS FOR SERVER-SIDE RENDERING RARELY NEED CHANGING
|
93
101
|
#
|
94
|
-
# This value only affects server-side rendering when using the webpack-dev-server
|
102
|
+
# This value only affects server-side rendering when using the webpack-dev-server
|
95
103
|
# If you are hashing the server bundle and you want to use the same bundle for client and server,
|
96
104
|
# you'd set this to `true` so that React on Rails reads the server bundle from the webpack-dev-server.
|
97
105
|
# Normally, you have different bundles for client and server, thus, the default is false.
|
@@ -101,7 +109,7 @@ ReactOnRails.configure do |config|
|
|
101
109
|
# If true, ensure that in config/webpacker.yml that you have both dev_server.hmr and
|
102
110
|
# dev_server.inline set to false.
|
103
111
|
config.same_bundle_for_client_and_server = false
|
104
|
-
|
112
|
+
|
105
113
|
# If set to true, this forces Rails to reload the server bundle if it is modified
|
106
114
|
# Default value is Rails.env.development?
|
107
115
|
# You probably will never change this.
|
@@ -119,16 +127,16 @@ ReactOnRails.configure do |config|
|
|
119
127
|
|
120
128
|
# Default is true only for development? to raise exception on server if the JS code throws for
|
121
129
|
# server rendering. The reason is that the server logs will show the error and force you to fix
|
122
|
-
# any server rendering issues immediately during development.
|
123
|
-
config.raise_on_prerender_error = Rails.env.development?
|
130
|
+
# any server rendering issues immediately during development.
|
131
|
+
config.raise_on_prerender_error = Rails.env.development?
|
124
132
|
|
125
133
|
################################################################################
|
126
134
|
# Server Renderer Configuration for ExecJS
|
127
135
|
################################################################################
|
128
136
|
# The default server rendering is ExecJS, probably using the mini_racer gem
|
129
|
-
# If you wish to use an alternative Node server rendering for higher performance,
|
137
|
+
# If you wish to use an alternative Node server rendering for higher performance,
|
130
138
|
# contact justin@shakacode.com for details.
|
131
|
-
#
|
139
|
+
#
|
132
140
|
# For ExecJS:
|
133
141
|
# You can configure your pool of JS virtual machines and specify where it should load code:
|
134
142
|
# On MRI, use `mini_racer` for the best performance
|
@@ -153,7 +161,7 @@ ReactOnRails.configure do |config|
|
|
153
161
|
# By default(without this option) all yaml files from Rails.root.join("config", "locales")
|
154
162
|
# and installed gems are loaded
|
155
163
|
config.i18n_yml_dir = Rails.root.join("config", "locales")
|
156
|
-
|
164
|
+
|
157
165
|
# Possible output formats are js and json
|
158
166
|
# The default format is json
|
159
167
|
config.i18n_output_format = 'json'
|
@@ -178,13 +186,13 @@ ReactOnRails.configure do |config|
|
|
178
186
|
# TEST CONFIGURATION OPTIONS
|
179
187
|
# Below options are used with the use of this test helper:
|
180
188
|
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
181
|
-
#
|
189
|
+
#
|
182
190
|
# NOTE:
|
183
191
|
# Instead of using this test helper, you may ensure fresh test files using rails/webpacker via:
|
184
192
|
# 1. Have `config/webpacker/test.js` exporting an array of objects to configure both client and server bundles.
|
185
193
|
# 2. Set the compile option to true in config/webpacker.yml for env test
|
186
194
|
################################################################################
|
187
|
-
|
195
|
+
|
188
196
|
# If you are using this in your spec_helper.rb (or rails_helper.rb):
|
189
197
|
#
|
190
198
|
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
@@ -194,7 +202,7 @@ ReactOnRails.configure do |config|
|
|
194
202
|
#
|
195
203
|
config.build_test_command = "RAILS_ENV=test bin/webpack"
|
196
204
|
|
197
|
-
# CONFIGURE YOUR SOURCE FILES
|
205
|
+
# CONFIGURE YOUR SOURCE FILES
|
198
206
|
# The test helper needs to know where your JavaScript files exist. The value is configured
|
199
207
|
# by your config/webpacker.yml source_path:
|
200
208
|
# source_path: client/app # if using recommended /client directory
|
@@ -202,10 +210,22 @@ ReactOnRails.configure do |config|
|
|
202
210
|
# Define the files we need to check for webpack compilation when running tests.
|
203
211
|
# The default is `%w( manifest.json )` as will be sufficient for most webpacker builds.
|
204
212
|
# However, if you are generated a server bundle that is NOT hashed (present in manifest.json),
|
205
|
-
# then include the file in this list like this:
|
213
|
+
# then include the file in this list like this:
|
206
214
|
config.webpack_generated_files = %w( server-bundle.js manifest.json )
|
207
215
|
# Note, be sure NOT to include your server-bundle.js if it is hashed, or else React on Rails will
|
208
|
-
# think the server-bundle.js is missing every time for test runs.
|
216
|
+
# think the server-bundle.js is missing every time for test runs.
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
220
|
+
Example of a ReactOnRailsConfig module for `production_build_command`:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
module BuildProductionCommand
|
224
|
+
include FileUtils
|
225
|
+
# Method with the name of call will be called during assets:precompile
|
226
|
+
def self.call
|
227
|
+
sh "bin/webpack"
|
228
|
+
end
|
209
229
|
end
|
210
230
|
```
|
211
231
|
|
@@ -0,0 +1,4 @@
|
|
1
|
+
# Deployment
|
2
|
+
|
3
|
+
- `rails/webpacker` puts the necessary precompile steps automatically in the rake precompile step.
|
4
|
+
- See the [Heroku Deployment](https://www.shakacode.com/react-on-rails/docs/deployment/heroku-deployment/) doc for specifics regarding Heroku. The information for Heroku may apply to other deployments.
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# Getting Started
|
2
|
+
|
3
|
+
Note, the best way to understand how to use ReactOnRails is to study a few simple examples. You can do a quick demo setup, either on your existing app or on a new Rails app.
|
4
|
+
|
5
|
+
1. Do the quick [tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/).
|
6
|
+
2. Add React on Rails to an existing Rails app per [the instructions](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).
|
7
|
+
3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy), a simple, no DB example.
|
8
|
+
3. 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://reactrails.com).
|
9
|
+
|
10
|
+
## Basic Installation
|
11
|
+
|
12
|
+
*See also [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).*
|
13
|
+
|
14
|
+
1. Add the `react_on_rails` gem to Gemfile:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
bundle add react_on_rails --strict
|
18
|
+
```
|
19
|
+
|
20
|
+
2. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
21
|
+
|
22
|
+
3. Run the generator:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
rails generate react_on_rails:install
|
26
|
+
```
|
27
|
+
|
28
|
+
4. Start the app:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
rails s
|
32
|
+
```
|
33
|
+
|
34
|
+
5. Visit http://localhost:3000/hello_world.
|
35
|
+
|
36
|
+
|
37
|
+
### Turning on server rendering
|
38
|
+
|
39
|
+
With the code from running the React on Rails generator above:
|
40
|
+
|
41
|
+
1. Edit `app/views/hello_world/index.html.erb` and set the `prerender` option to `true`.
|
42
|
+
2. Refresh the page.
|
43
|
+
|
44
|
+
Below is the line where you turn server rendering on by setting `prerender` to true:
|
45
|
+
|
46
|
+
```erb
|
47
|
+
<%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
|
48
|
+
```
|
49
|
+
|
50
|
+
Note, if you got an error in your console regarding "ReferenceError: window is not defined",
|
51
|
+
then you need to edit `config/webpacker.yml` and set `hmr: false` and `inline: false`.
|
52
|
+
See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
|
53
|
+
issue.
|
54
|
+
|
55
|
+
## Basic Usage
|
56
|
+
|
57
|
+
### Configuration
|
58
|
+
|
59
|
+
* Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](https://www.shakacode.com/react-on-rails/docs/guides/configuration/) for documentation of all configuration options.
|
60
|
+
* Configure `config/webpacker.yml`. If you used the generator and the default webpacker setup, you don't need to touch this file. If you are customizing your setup, then consult the [spec/dummy/config/webpacker.yml](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/webpacker.yml) example or the official default [webpacker.yml](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml).
|
61
|
+
* Most apps should rely on the rails/webpacker setup for Webpack. v6 of rails/webpacker includes support for v5 of webpack.
|
62
|
+
|
63
|
+
## Including your React Component on your Rails Views
|
64
|
+
|
65
|
+
- React component are rendered via your Rails Views. Here's an ERB sample:
|
66
|
+
|
67
|
+
```erb
|
68
|
+
<%= react_component("HelloWorld", props: @some_props) %>
|
69
|
+
```
|
70
|
+
|
71
|
+
- **Server-Side Rendering**: Your react component is first rendered into HTML on the server. Use the **prerender** option:
|
72
|
+
|
73
|
+
```erb
|
74
|
+
<%= react_component("HelloWorld", props: @some_props, prerender: true) %>
|
75
|
+
```
|
76
|
+
|
77
|
+
- The `component_name` parameter is a string matching the name you used to expose your React component globally. So, in the above examples, if you had a React component named "HelloWorld", you would register it with the following lines:
|
78
|
+
|
79
|
+
```js
|
80
|
+
import ReactOnRails from 'react-on-rails';
|
81
|
+
import HelloWorld from './HelloWorld';
|
82
|
+
ReactOnRails.register({ HelloWorld });
|
83
|
+
```
|
84
|
+
|
85
|
+
Exposing your component in this way is how React on Rails is able to reference your component from a Rails view. You can expose as many components as you like, as long as their names do not collide. See below for the details of how you expose your components via the react_on_rails webpack configuration. You may call `ReactOnRails.register` many times.
|
86
|
+
|
87
|
+
- `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Rails View
|
91
|
+
<%= react_component("HelloWorld", props: { name: "Stranger" }) %>
|
92
|
+
```
|
93
|
+
|
94
|
+
- This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://reactjs.org/docs/context.html). See [Render-Functions and the RailsContext](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) for more details on this topic.
|
95
|
+
|
96
|
+
```js
|
97
|
+
import React from 'react';
|
98
|
+
|
99
|
+
export default (props, railsContext) => {
|
100
|
+
// Note wrap in a function to make this a React function component
|
101
|
+
return () => (
|
102
|
+
<div>
|
103
|
+
Your locale is {railsContext.i18nLocale}.<br/>
|
104
|
+
Hello, {props.name}!
|
105
|
+
</div>
|
106
|
+
);
|
107
|
+
};
|
108
|
+
```
|
109
|
+
|
110
|
+
See the [View Helpers API](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api/) for more details on `react_component` and its sibling function `react_component_hash`.
|
111
|
+
|
112
|
+
## Globally Exposing Your React Components
|
113
|
+
|
114
|
+
For the React on Rails view helper `react_component` to use your React components, you will have to **register** them in your JavaScript code.
|
115
|
+
|
116
|
+
Use modules just as you would when using Webpack and React without Rails. The difference is that instead of mounting React components directly to an element using `React.render`, you **register your components to ReactOnRails and then mount them with helpers inside of your Rails views**.
|
117
|
+
|
118
|
+
This is how to expose a component to the `react_component` view helper.
|
119
|
+
|
120
|
+
```javascript
|
121
|
+
// app/javascript/packs/hello-world-bundle.js
|
122
|
+
import HelloWorld from '../components/HelloWorld';
|
123
|
+
import ReactOnRails from 'react-on-rails';
|
124
|
+
ReactOnRails.register({ HelloWorld });
|
125
|
+
```
|
126
|
+
|
127
|
+
#### Different Server-Side Rendering Code (and a Server-Specific Bundle)
|
128
|
+
|
129
|
+
You may want different code for your server-rendered components running server side versus client side. For example, if you have an animation that runs when a component is displayed, you might need to turn that off when server rendering. One way to handle this is conditional code like `if (window) { doClientOnlyCode() }`.
|
130
|
+
|
131
|
+
Another way is to use a separate webpack configuration file that can use a different server side entry file, like 'serverRegistration.js' as opposed to 'clientRegistration.js.' That would set up different code for server rendering.
|
132
|
+
|
133
|
+
For details on techniques to use different code for client and server rendering, see: [How to use different versions of a file for client and server rendering](https://forum.shakacode.com/t/how-to-use-different-versions-of-a-file-for-client-and-server-rendering/1352). (_Requires creating a free account._)
|
134
|
+
|
135
|
+
## Specifying Your React Components: Register directly or use render-functions
|
136
|
+
|
137
|
+
You have two ways to specify your React components. You can either register the React component (either function or class component) directly, or you can create a function that returns a React component, which we using the name of a "render-function". Creating a render-function allows:
|
138
|
+
|
139
|
+
1. You to have access to the `railsContext`. See [documentation for the railsContext](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) in terms of why you might need it. You **need** a Render-Function to access the `railsContext`.
|
140
|
+
2. You can use the passed-in props to initialize a redux store or set up react-router.
|
141
|
+
3. You can return different components depending on what's in the props.
|
142
|
+
|
143
|
+
Note, the return value of a **Render-Function** should be either a React Function or Class Component, or an object representing server rendering results.
|
144
|
+
|
145
|
+
**Do not return a React Element (JSX).**
|
146
|
+
|
147
|
+
ReactOnRails will automatically detect a registered Render-Function by the fact that the function takes
|
148
|
+
more than 1 parameter. In other words, if you want the ability to provide a function that returns the
|
149
|
+
React component, then you need to specify at least a second parameter. This is the `railsContext`.
|
150
|
+
If you're not using this parameter, declare your function with the unused param:
|
151
|
+
|
152
|
+
```js
|
153
|
+
const MyComponentGenerator = (props, _railsContext) => {
|
154
|
+
if (props.print) {
|
155
|
+
// This is a React FunctionComponent because it is wrapped in a function.
|
156
|
+
return () => <H1>{JSON.stringify(props)}</H1>;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
```
|
160
|
+
|
161
|
+
Thus, there is no difference between registering a React Function Component or class Component versus a "Render-Function." Just call `ReactOnRails.register`.
|
162
|
+
|
163
|
+
## react_component_hash for Render-Functions
|
164
|
+
|
165
|
+
Another reason to use a Render-Function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: { renderedHtml, redirectLocation, error }. Make sure you use this function with `react_component_hash`.
|
166
|
+
|
167
|
+
For server rendering, if you wish to return multiple HTML strings from a Render-Function, you may return an Object from your Render-Function with a single top-level property of `renderedHtml`. Inside this Object, place a key called `componentHtml`, along with any other needed keys. An example scenario of this is when you are using side effects libraries like [React Helmet](https://github.com/nfl/react-helmet). Your Ruby code will get this Object as a Hash containing keys componentHtml and any other custom keys that you added:
|
168
|
+
|
169
|
+
```js
|
170
|
+
{ renderedHtml: { componentHtml, customKey1, customKey2} }
|
171
|
+
```
|
172
|
+
|
173
|
+
For details on using react_component_hash with react-helmet, see [our react-helmet documentation](https://www.shakacode.com/react-on-rails/docs/additional-reading/react-helmet/).
|
174
|
+
|
175
|
+
## Error Handling
|
176
|
+
|
177
|
+
* All errors from ReactOnRails will be of type ReactOnRails::Error.
|
178
|
+
* Prerendering (server rendering) errors get context information for HoneyBadger and Sentry for easier debugging.
|
179
|
+
|
180
|
+
## I18n
|
181
|
+
|
182
|
+
React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.json` or `*.js*.
|
183
|
+
See the [How to add I18n](https://www.shakacode.com/react-on-rails/docs/guides/i18n/) for a summary of adding I18n.
|
File without changes
|
@@ -1,21 +1,21 @@
|
|
1
1
|
# How React on Rails Works (with rails/webpacker)
|
2
2
|
|
3
|
-
*Note, older versions of React on Rails pushed the Webpack bundles through the Asset Pipeline. This older method has *many* disadvantages, such as broken sourcemaps, performance issues, etc. If you need help migrating to the current way of bypassing the Asset Pipeline, [email Justin](mailto:justin@shakacode.com).*
|
3
|
+
*Note, older versions of React on Rails pushed the Webpack bundles through the Asset Pipeline. This older method has *many* disadvantages, such as broken sourcemaps, performance issues, etc. If you need help migrating to the current way of bypassing the Asset Pipeline, [email Justin](mailto:justin@shakacode.com).*
|
4
4
|
|
5
|
-
Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [rails/webpacker](https://github.com/rails/webpacker) provides view helpers to access the Webpack generated (and fingerprinted) JS and CSS. These files totally skip the Rails asset pipeline. You are responsible for properly configuring your Webpack output. You will either use the standard Webpack configuration (*recommended*) or the `rails/webpacker` setup for Webpack.
|
5
|
+
Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [rails/webpacker](https://github.com/rails/webpacker) provides view helpers to access the Webpack generated (and fingerprinted) JS and CSS. These files totally skip the Rails asset pipeline. You are responsible for properly configuring your Webpack output. You will either use the standard Webpack configuration (*recommended*) or the `rails/webpacker` setup for Webpack.
|
6
6
|
|
7
7
|
Ensure these generated bundle files are in your `.gitignore`, as you never want to add the large compiled bundles to git.
|
8
8
|
|
9
|
-
Inside your Rails views, you can now use the `react_component` helper method provided by React on Rails. You can pass props directly to the react component helper.
|
9
|
+
Inside your Rails views, you can now use the `react_component` helper method provided by React on Rails. You can pass props directly to the react component helper.
|
10
10
|
|
11
|
-
Optionally, you can also initialize a Redux store with the view or controller helper `redux_store` so that the redux store can be shared amongst multiple React components.
|
11
|
+
Optionally, you can also initialize a Redux store with the view or controller helper `redux_store` so that the redux store can be shared amongst multiple React components.
|
12
12
|
|
13
13
|
## Client-Side Rendering vs. Server-Side Rendering
|
14
14
|
|
15
15
|
In most cases, you should use the `prerender: false` (default behavior) with the provided `react_component` helper method to render the React component from your Rails views. In some cases, such as when SEO is vital, or many users will not have JavaScript enabled, you can enable server-rendering by passing `prerender: true` to your helper, or you can simply change the default in `config/initializers/react_on_rails`.
|
16
16
|
|
17
17
|
Now the server will interpret your JavaScript. The default is to use [ExecJS](https://github.com/rails/execjs) and pass the resulting HTML to the client. If you want to maximize the 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).
|
18
|
-
|
18
|
+
|
19
19
|
## HTML Source Code
|
20
20
|
|
21
21
|
If you open the HTML source of any web page using React on Rails, you'll see the 3 parts of React on Rails rendering:
|
@@ -28,17 +28,18 @@ You can see all this on the source for [reactrails.com](https://www.reactrails.c
|
|
28
28
|
|
29
29
|
## Building the Bundles
|
30
30
|
|
31
|
-
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes. Alternately, the `rails/webpacker` library also can ensure that your bundles are built.
|
31
|
+
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes. Alternately, the `rails/webpacker` library also can ensure that your bundles are built.
|
32
32
|
|
33
|
-
For example, you might create a [Procfile.dev](spec/dummy/Procfile.dev).
|
33
|
+
For example, you might create a [Procfile.dev](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/Procfile.dev).
|
34
34
|
|
35
35
|
On production deployments that use asset precompilation, such as Heroku deployments, `rails/webpacker`, by default, will automatically run webpack to build your JavaScript bundles, running the command `bin/webpack` in your app.
|
36
36
|
|
37
37
|
However, if you want to run a custom command to run webpack to build your bundles, then you will:
|
38
38
|
1. Ensure you do not have a `config/webpack/production.js` file
|
39
|
-
1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](docs/
|
39
|
+
1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](https://www.shakacode.com/react-on-rails/docs/guides/configuration/)
|
40
40
|
|
41
41
|
Then React on Rails modifies the `assets:precompile` task to run your `build_production_command`.
|
42
42
|
|
43
43
|
If you have used the provided generator, these bundles will automatically be added to your `.gitignore` to prevent extraneous noise from re-generated code in your pull requests. You will want to do this manually if you do not use the provided generator.
|
44
44
|
|
45
|
+
You can stop React on Rails from modifying or creating the `assets:precompile` task, by setting a `REACT_ON_RAILS_PRECOMPILE` environment variable to `no`, `false`, `n` or `f`.
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# How to conditionally render server side based on the device type
|
2
|
+
|
3
|
+
In general, we want to use CSS to do mobile responsive layouts.
|
4
|
+
|
5
|
+
However, sometimes we want to have layouts sufficiently different that we can't do this via CSS. If we didn't do server rendering, we can check the device type on the client side. However, if we're doing server rendering, we need to send this data to the client code from the Rails server so that the server rendering can account for this.
|
6
|
+
|
7
|
+
Here's an example:
|
8
|
+
|
9
|
+
## config/initializers/react_on_rails.rb
|
10
|
+
```ruby
|
11
|
+
module RenderingExtension
|
12
|
+
# Return a Hash that contains custom values from the view context that will get passed to
|
13
|
+
# all calls to react_component and redux_store for rendering
|
14
|
+
def self.custom_context(view_context)
|
15
|
+
if view_context.controller.is_a?(ActionMailer::Base)
|
16
|
+
{}
|
17
|
+
else
|
18
|
+
{
|
19
|
+
desktop: !(view_context.browser.device.tablet? || view_context.browser.device.mobile?),
|
20
|
+
tablet: view_context.browser.device.tablet?,
|
21
|
+
mobile: view_context.browser.device.mobile? || false
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Shown below are the defaults for configuration
|
28
|
+
ReactOnRails.configure do |config|
|
29
|
+
# See https://github.com/shakacode/react_on_rails/blob/master/docs/guides/configuration.md for the rest
|
30
|
+
|
31
|
+
# This allows you to add additional values to the Rails Context. Implement one static method
|
32
|
+
# called `custom_context(view_context)` and return a Hash.
|
33
|
+
config.rendering_extension = RenderingExtension
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Note, full details of the React on Rails confguration are [here in docs/basics/configuration.md](https://shakacode.com/react-on-rails/docs/guides/configuration/).
|
38
|
+
|
39
|
+
See the doc file [docs/basics/generator-functions-and-railscontext.md](https://shakacode.com/react-on-rails/docs/guides/generator-functions-and-railscontext/#rails-context) for how your client-side code uses the device information
|