react_on_rails 12.2.0 → 12.5.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 +12 -10
- data/.rubocop.yml +2 -1
- data/CHANGELOG.md +44 -17
- data/Gemfile.development_dependencies +3 -2
- data/NEWS.md +9 -3
- data/REACT-ON-RAILS-PRO-LICENSE +1 -1
- data/README.md +31 -317
- data/Rakefile +1 -1
- data/SUMMARY.md +21 -12
- data/docs/additional-details/generator-details.md +1 -1
- data/docs/additional-details/manual-installation-overview.md +2 -2
- data/docs/api/view-helpers-api.md +4 -4
- data/docs/contributor-info/linters.md +1 -1
- data/docs/contributor-info/pull-requests.md +1 -1
- data/docs/deployment/heroku-deployment.md +16 -5
- data/docs/{basics → guides}/client-vs-server-rendering.md +1 -1
- data/docs/{basics → guides}/configuration.md +11 -9
- data/docs/{basics → guides}/deployment.md +1 -1
- 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/{basics → guides}/how-react-on-rails-works.md +3 -2
- 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/{rails-webpacker-react-integration-options.md → guides/rails-webpacker-react-integration-options.md} +1 -1
- data/docs/guides/react-on-rails-overview.md +30 -0
- data/docs/{basics → guides}/react-server-rendering.md +2 -2
- data/docs/{basics → guides}/render-functions-and-railscontext.md +0 -0
- data/docs/{basics → guides}/rspec-configuration.md +2 -2
- data/docs/{basics → guides}/tutorial.md +20 -18
- data/docs/{basics → guides}/upgrading-react-on-rails.md +1 -1
- data/docs/{basics → guides}/webpack-configuration.md +1 -1
- data/docs/home.md +19 -378
- data/docs/javascript/code-splitting.md +2 -2
- data/docs/misc/articles.md +1 -1
- data/docs/misc/doctrine.md +3 -3
- data/docs/outdated/rails-assets-relative-paths.md +2 -2
- data/docs/outdated/rails-assets.md +1 -1
- data/docs/rails/convert-rails-5-api-only-app.md +1 -1
- data/docs/rails/rails-engine-integration.md +3 -12
- data/docs/rails/turbolinks.md +13 -1
- data/docs/react-on-rails-pro/react-on-rails-pro.md +43 -0
- data/docs/testimonials/testimonials.md +4 -4
- data/lib/generators/react_on_rails/base_generator.rb +47 -4
- data/lib/generators/react_on_rails/generator_helper.rb +4 -0
- data/lib/generators/react_on_rails/templates/.eslintrc +3 -1
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -8
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +9 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css +4 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorldServer.js +5 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js +8 -0
- data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +1 -0
- data/lib/generators/react_on_rails/templates/base/base/babel.config.js.tt +15 -0
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +1 -1
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt +17 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +15 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt +27 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/production.js.tt +11 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt +117 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpackConfig.js.tt +36 -0
- data/lib/generators/react_on_rails/templates/base/base/config/webpacker.yml +62 -0
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
- data/lib/react_on_rails/helper.rb +6 -0
- data/lib/react_on_rails/locales/base.rb +7 -9
- data/lib/react_on_rails/server_rendering_js_code.rb +13 -0
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +9 -0
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/tasks/assets.rake +8 -7
- data/package.json +1 -1
- data/rakelib/example_type.rb +5 -1
- data/rakelib/examples.rake +7 -2
- data/rakelib/release.rake +1 -1
- data/rakelib/task_helpers.rb +7 -0
- data/react_on_rails.gemspec +1 -1
- metadata +35 -19
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +0 -26
@@ -66,24 +66,26 @@ ReactOnRails.configure do |config|
|
|
66
66
|
# defaults to "" (top level)
|
67
67
|
config.node_modules_location = "client" # If using webpacker you should use "".
|
68
68
|
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
69
|
+
# If you're using the standard rails/webpacker configuration of webpack, then rails/webpacker
|
70
|
+
# will automatically modify or create an assets:precompile task to build your assets. If so,
|
71
|
+
# set this value to nil. Alternatively, you can specify `config.build_production_command`
|
72
|
+
# to have react_on_rails invoke a command for you during assets:precompile.
|
73
|
+
# The command is either a script or a module containing a class method `call`
|
74
|
+
# In this example, the module BuildProductionCommand would have a class method `call`.
|
74
75
|
config.build_production_command = "RAILS_ENV=production bin/webpack"
|
75
76
|
|
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
77
|
# See bottom for an example of the BuildProductionCommand module.
|
79
78
|
# config.build_production_command = BuildProductionCommand
|
80
|
-
|
79
|
+
# If you wish to utilize ReactOnRailsPro production bundle caching logic, then use
|
80
|
+
# config.build_production_command = ReactOnRailsPro::AssetsPrecompile
|
81
|
+
# and be sure to check ReactOnRailsPro's configuration documentation!
|
82
|
+
|
81
83
|
################################################################################
|
82
84
|
################################################################################
|
83
85
|
# SERVER RENDERING OPTIONS
|
84
86
|
################################################################################
|
85
87
|
# This is the file used for server rendering of React when using `(prerender: true)`
|
86
|
-
# If you are
|
88
|
+
# If you are not using server rendering, you should set this to `nil`.
|
87
89
|
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
|
88
90
|
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
|
89
91
|
# JavaScript execution instances which should handle any component requested.
|
@@ -1,4 +1,4 @@
|
|
1
1
|
# Deployment
|
2
2
|
|
3
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.
|
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
|
@@ -35,9 +35,10 @@ For example, you might create a [Procfile.dev](https://github.com/shakacode/reac
|
|
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
|
-
1.
|
39
|
-
1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](https://www.shakacode.com/react-on-rails/docs/basics/configuration)
|
38
|
+
1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](https://www.shakacode.com/react-on-rails/docs/guides/configuration/)
|
40
39
|
|
41
40
|
Then React on Rails modifies the `assets:precompile` task to run your `build_production_command`.
|
42
41
|
|
43
42
|
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.
|
43
|
+
|
44
|
+
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
|
@@ -0,0 +1,98 @@
|
|
1
|
+
## How to use different versions of a file for client and server rendering
|
2
|
+
|
3
|
+
There are 3 main ways to use different code for server vs. client rendering.
|
4
|
+
|
5
|
+
## A. Using different Entry Points
|
6
|
+
Many projects will have different entry points for client and server rendering. This only works for a top-level entry point such as the entry point for a react-router app component.
|
7
|
+
|
8
|
+
Your Client Entry can look like this:
|
9
|
+
|
10
|
+
```js
|
11
|
+
import ReactOnRails from 'react-on-rails';
|
12
|
+
import App from './ClientApp';
|
13
|
+
ReactOnRails.register({ App })
|
14
|
+
```
|
15
|
+
|
16
|
+
So your Server Entry can look like:
|
17
|
+
|
18
|
+
```js
|
19
|
+
import ReactOnRails from 'react-on-rails';
|
20
|
+
import App from './ServerApp';
|
21
|
+
ReactOnRails.register({ App })
|
22
|
+
```
|
23
|
+
|
24
|
+
Note that the only difference is on the second line of each of these examples.
|
25
|
+
|
26
|
+
## B. Two Options for Using Webpack Resolve Alias in the Webpack Config
|
27
|
+
Per [Webpack Docs](https://webpack.js.org/configuration/resolve/#resolve-alias).
|
28
|
+
|
29
|
+
### 1. Update `webpack/set-resolve.js` to have a different resolution for the exact file:
|
30
|
+
|
31
|
+
```js
|
32
|
+
function setResolve(builderConfig, webpackConfig) {
|
33
|
+
|
34
|
+
// Use a different resolution for Client and Server file
|
35
|
+
let SomeJsFile;
|
36
|
+
if (builderConfig.serverRendering) {
|
37
|
+
SomeJsFile = path.resolve(__dirname, "../bundles/SomeJsFileServer");
|
38
|
+
} else {
|
39
|
+
SomeJsFile = path.resolve(__dirname, "../bundles/SomeJsFileClient");
|
40
|
+
}
|
41
|
+
|
42
|
+
const resolve = {
|
43
|
+
alias: {
|
44
|
+
... // blah blah
|
45
|
+
SomeJsFile,
|
46
|
+
... // blah blah
|
47
|
+
},
|
48
|
+
```
|
49
|
+
|
50
|
+
Then you have this import:
|
51
|
+
|
52
|
+
```js
|
53
|
+
import SomeJsFile from 'SomeJsFile';
|
54
|
+
```
|
55
|
+
|
56
|
+
### 2. Use a different resolution for the right directory of client or server files:
|
57
|
+
|
58
|
+
#### a. Update `webpack/set-resolve.js` to have something like:
|
59
|
+
```js
|
60
|
+
function setResolve(builderConfig, webpackConfig) {
|
61
|
+
|
62
|
+
// Use a different resolution for Client and Server file
|
63
|
+
let variant;
|
64
|
+
if (builderConfig.serverRendering) {
|
65
|
+
variant = path.resolve(__dirname, "../bundles/variant/ClientOnly");
|
66
|
+
} else {
|
67
|
+
variant = path.resolve(__dirname, "../bundles/variant/serverOnly");
|
68
|
+
}
|
69
|
+
|
70
|
+
const resolve = {
|
71
|
+
alias: {
|
72
|
+
... // blah blah
|
73
|
+
variant
|
74
|
+
... // blah blah
|
75
|
+
},
|
76
|
+
```
|
77
|
+
|
78
|
+
#### b. Add different versions of the file to the `bundles/variant/ClientOnly` and `bundles/variant/ServerOnly` directories
|
79
|
+
|
80
|
+
#### c. Use the `variant` in import in a file that can be used both for client and server rendering:
|
81
|
+
|
82
|
+
```js
|
83
|
+
import SomeJsFile from 'variant/SomeJsFile';
|
84
|
+
import AnotherJsFile from 'variant/AnotherJsFile';
|
85
|
+
```
|
86
|
+
|
87
|
+
## C. Conditional code that can check if `window` is defined.
|
88
|
+
|
89
|
+
This is probably the ugliest and hackiest way to do this, but it's quick! Essentially you wrap code that cannot execute server side in a conditional:
|
90
|
+
|
91
|
+
```js
|
92
|
+
if (window) { // window should be falsy on the server side
|
93
|
+
doSomethingClientOnly();
|
94
|
+
|
95
|
+
// or do an import
|
96
|
+
const foobar = require('foobar').default;
|
97
|
+
}
|
98
|
+
```
|
File without changes
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# Getting Started with an existing Rails app
|
2
2
|
|
3
|
-
**Also consult the instructions for installing on a fresh Rails app**, see the [React on Rails Basic Tutorial](https://www.shakacode.com/react-on-rails/docs/
|
3
|
+
**Also consult the instructions for installing on a fresh Rails app**, see the [React on Rails Basic Tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/).
|
4
4
|
|
5
|
-
**If you have rails-5 API only project**, first [convert the rails-5 API only app to rails app](
|
5
|
+
**If you have rails-5 API only project**, first [convert the rails-5 API only app to rails app](https://www.shakacode.com/react-on-rails/docs/rails/convert-rails-5-api-only-app-to-rails-app/).
|
6
6
|
|
7
7
|
1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `package.json` file.
|
8
8
|
|
@@ -52,7 +52,7 @@ bundle add react_on_rails --version=12.0.4 --strict
|
|
52
52
|
|
53
53
|
## Installation
|
54
54
|
|
55
|
-
See the [Installation Overview](https://www.shakacode.com/react-on-rails/docs/additional-details/manual-installation-overview) for a concise set summary of what's in a React on Rails installation.
|
55
|
+
See the [Installation Overview](https://www.shakacode.com/react-on-rails/docs/additional-details/manual-installation-overview/) for a concise set summary of what's in a React on Rails installation.
|
56
56
|
|
57
57
|
|
58
58
|
## NPM
|
File without changes
|
@@ -13,7 +13,7 @@ These gems provide advanced integration of React with [rails/webpacker](https://
|
|
13
13
|
| [react-rails](https://github.com/reactjs/react-rails) | ✅ | ✅ | | | | | |
|
14
14
|
| [webpacker-react](https://github.com/renchap/webpacker-react) | ✅ | | | | | | |
|
15
15
|
|
16
|
-
Note, Node SSR for React on Rails requires [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
16
|
+
Note, Node SSR for React on Rails requires [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
|
17
17
|
|
18
18
|
---
|
19
19
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# React on Rails
|
2
|
+
|
3
|
+
React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
# Project Objective
|
8
|
+
|
9
|
+
To provide a high performance framework for integrating Ruby on Rails with React via the [**Webpacker**](https://github.com/rails/webpacker) gem especially in regards to React Server-Side Rendering for better SEO and improved performance.
|
10
|
+
|
11
|
+
# Features and Why React on Rails?
|
12
|
+
|
13
|
+
Given that [`rails/webpacker`](https://github.com/rails/webpacker/) gem already provides basic React integration, why would you use "React on Rails"?
|
14
|
+
|
15
|
+
1. Easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate request to your API.
|
16
|
+
1. Tight integration with [rails/webpacker](https://github.com/rails/webpacker).
|
17
|
+
1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `rails/webpacker`.
|
18
|
+
1. Support for HMR for a great developer experience.
|
19
|
+
1. Supports latest versions of React with hooks.
|
20
|
+
1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration including server-side-rendering.
|
21
|
+
1. [Internationalization (I18n) and (localization)](https://www.shakacode.com/react-on-rails/docs/guides/i18n/)
|
22
|
+
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/).
|
23
|
+
1. [ReScript (Reason ML) Support](https://github.com/shakacode/reason-react-on-rails-example).
|
24
|
+
|
25
|
+
See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
|
26
|
+
----
|
27
|
+
|
28
|
+
## Prerequisites
|
29
|
+
|
30
|
+
Ruby on Rails >=5 and rails/webpacker 4.2+.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# React Server Rendering
|
2
2
|
|
3
|
-
See also [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/
|
3
|
+
See also [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/).
|
4
4
|
|
5
5
|
## What is the easiest way to setup a webpack configuration for server-side-rendering?
|
6
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).
|
@@ -13,7 +13,7 @@ During the Rails rendering of HTML per a browser request, the Rails server will
|
|
13
13
|
|
14
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).
|
15
15
|
|
16
|
-
See [this note](https://www.shakacode.com/react-on-rails/docs/
|
16
|
+
See [this note](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/).
|
17
17
|
|
18
18
|
## How do you do Server Rendering with React on Rails?
|
19
19
|
1. The `react_component` view helper method provides the `prerender:` option to switch on or off server rendering.
|
File without changes
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# RSpec Configuration
|
2
|
-
_Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/
|
2
|
+
_Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/guides/minitest-configuration/)_
|
3
3
|
|
4
4
|
# If your webpack configurations correspond to rails/webpacker's default setup
|
5
5
|
If you're able to configure your webpack configuration to be run by having your webpack configuration
|
@@ -9,7 +9,7 @@ compiled by webpack before running tests and during production deployment:
|
|
9
9
|
1. **Use rails/webpacker's compile option**: Configure your `config/webpacker.yml` so that `compile: true` is for `test` and `production`
|
10
10
|
environments. Ensure that your `source_path` is correct, or else `rails/webpacker` won't correctly
|
11
11
|
detect changes.
|
12
|
-
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/
|
12
|
+
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
|
13
13
|
|
14
14
|
```yml
|
15
15
|
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpack"
|
@@ -1,12 +1,10 @@
|
|
1
1
|
# React on Rails Basic Tutorial
|
2
2
|
|
3
|
-
-----
|
4
|
-
|
5
3
|
**November 11, 2020**: See the example repo of [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh) for a new way to setup the creation of your SSR bundle with `rails/webpacker`. This file will be update shortly. Most of it is still relevant.
|
6
4
|
|
7
5
|
-----
|
8
6
|
|
9
|
-
*Updated for Ruby 2.7.1, Rails 6.0.3.1,
|
7
|
+
*Updated for Ruby 2.7.1, Rails 6.0.3.1, React on Rails v12.5.0, and Webpacker v6*
|
10
8
|
|
11
9
|
This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
|
12
10
|
|
@@ -44,7 +42,7 @@ gem install foreman # download and install Foreman
|
|
44
42
|
```
|
45
43
|
|
46
44
|
## Create a new Ruby on Rails App
|
47
|
-
Then we need to create a fresh Rails application
|
45
|
+
Then we need to create a fresh Rails application as following.
|
48
46
|
|
49
47
|
First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If you are using an older version of Rails, you'll need to install webpacker with react per the instructions [here](https://github.com/rails/webpacker).
|
50
48
|
|
@@ -53,10 +51,9 @@ cd <directory where you want to create your new Rails app>
|
|
53
51
|
|
54
52
|
# Any name you like for the rails app
|
55
53
|
# Skip javascript so will add that next and get the current version
|
56
|
-
rails new --skip-sprockets
|
54
|
+
rails new --skip-sprockets --skip-turbolinks --javascript=webpack test-react-on-rails
|
57
55
|
|
58
56
|
cd test-react-on-rails
|
59
|
-
bundle
|
60
57
|
```
|
61
58
|
|
62
59
|
## Add the webpacker and react_on_rails gems
|
@@ -65,22 +62,21 @@ of both the gem and npm package. In other words, don't use the `^` or `~` in the
|
|
65
62
|
_Use the latest version for react_on_rails._
|
66
63
|
|
67
64
|
```
|
68
|
-
gem 'react_on_rails', '12.0
|
65
|
+
gem 'react_on_rails', '12.5.0' # prefer exact gem version to match npm version
|
69
66
|
```
|
70
67
|
|
71
68
|
Note: The latest released React On Rails version is considered stable. Please use the latest
|
72
69
|
version to ensure you get all the security patches and the best support.
|
73
70
|
|
74
71
|
```bash
|
75
|
-
bundle add webpacker
|
76
|
-
bundle add react_on_rails --version=12.0
|
72
|
+
bundle add webpacker --git=https://github.com/rails/webpacker.git
|
73
|
+
bundle add react_on_rails --version=12.5.0 --strict
|
77
74
|
```
|
78
75
|
|
79
76
|
## Run the webpacker generator
|
80
77
|
|
81
|
-
```
|
78
|
+
```terminal
|
82
79
|
bundle exec rails webpacker:install
|
83
|
-
bundle exec rails webpacker:install:react
|
84
80
|
```
|
85
81
|
|
86
82
|
Let's commit everything before installing React on Rails.
|
@@ -99,20 +95,24 @@ Install React on Rails: `rails generate react_on_rails:install`. You need to fir
|
|
99
95
|
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
100
96
|
If you want the redux install: `rails generate react_on_rails:install --redux`
|
101
97
|
|
98
|
+
The generator will add `mini_racer`'s latest version. If you're using linux & encounter issues installing `libv8`, here's [a common solution](https://github.com/rubyjs/mini_racer/issues/218).
|
99
|
+
|
102
100
|
```
|
103
101
|
rails generate react_on_rails:install
|
104
102
|
```
|
105
103
|
|
106
|
-
|
107
|
-
|
104
|
+
Enter `a` to replace all configuration files required by the project.
|
105
|
+
|
106
|
+
Then run the server with one of the following options:
|
108
107
|
|
108
|
+
## Running with HMR
|
109
109
|
```
|
110
110
|
foreman start -f Procfile.dev
|
111
111
|
```
|
112
112
|
|
113
|
-
##
|
113
|
+
## Running without HMR, statically creating the bundles
|
114
114
|
```
|
115
|
-
foreman start -f Procfile.dev-
|
115
|
+
foreman start -f Procfile.dev-static
|
116
116
|
```
|
117
117
|
|
118
118
|
Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
|
@@ -252,6 +252,8 @@ heroku open
|
|
252
252
|
|
253
253
|
and you will see your live app and you can share this URL with your friends. Congrats!
|
254
254
|
|
255
|
+
A deployed version of the project `spec/dummy` which demonstrates several uses of `react_on_rails` is available on heroku [through this link](https://ror-spec-dummy.herokuapp.com/)
|
256
|
+
|
255
257
|
## Turning on Server Rendering
|
256
258
|
|
257
259
|
You can turn on server rendering by simply changing the `prerender` option to `true`:
|
@@ -292,8 +294,8 @@ versus with server rendering:
|
|
292
294
|
|
293
295
|
For more details on server rendering, see:
|
294
296
|
|
295
|
-
+ [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/
|
296
|
-
+ [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/
|
297
|
+
+ [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/)
|
298
|
+
+ [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/react-server-rendering/)
|
297
299
|
|
298
300
|
## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
|
299
301
|
|
@@ -314,7 +316,7 @@ mv app/javascript client
|
|
314
316
|
|
315
317
|
## Using HMR with the rails/webpacker setup
|
316
318
|
|
317
|
-
Start the app using `foreman start -f Procfile.dev
|
319
|
+
Start the app using `foreman start -f Procfile.dev`.
|
318
320
|
|
319
321
|
When you change a JSX file and save, the browser will automatically refresh!
|
320
322
|
|
@@ -25,7 +25,7 @@ If you still need that feature, please file an issue.
|
|
25
25
|
In order to solve the issues regarding React Hooks compatibility, the number of parameters
|
26
26
|
for functions is used to determine if you have a Render-Function that will get invoked to
|
27
27
|
return a React component, or you are registering a React component defined by a function.
|
28
|
-
Please see [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/
|
28
|
+
Please see [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) for
|
29
29
|
more information on what a Render-Function is.
|
30
30
|
|
31
31
|
##### Update required for registered functions taking exactly 2 params.
|
@@ -22,7 +22,7 @@ A key decision in your use React on Rails is whether you go with the rails/webpa
|
|
22
22
|
|
23
23
|
## Option 1: Default Generator Setup: rails/webpacker app/javascript
|
24
24
|
|
25
|
-
Typical rails/webpacker apps have a standard directory structure as documented [here](https://github.com/rails/webpacker/blob/5-x-stable/docs/recommended-project-structure.md). If you follow the steps in the the [basic tutorial](https://www.shakacode.com/react-on-rails/docs/
|
25
|
+
Typical rails/webpacker apps have a standard directory structure as documented [here](https://github.com/rails/webpacker/blob/5-x-stable/docs/recommended-project-structure.md). If you follow the steps in the the [basic tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/), you will see this pattern in action. In order to customize the Webpack configuration, you need to consult with the [rails/webpacker Webpack configuration](https://www.shakacode.com/react-on-rails/docs/javascript/webpack/).
|
26
26
|
|
27
27
|
The *advantage* of using rails/webpacker to configure Webpack is that there is very little code needed to get started and you don't need to understand really anything about Webpack customization.
|
28
28
|
|