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
@@ -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](
|
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](docs/
|
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
|
@@ -0,0 +1,182 @@
|
|
1
|
+
# Rails/Webpacker React Integration Options
|
2
|
+
|
3
|
+
You only _need_ props hydration if you need SSR. However, there's no good reason to
|
4
|
+
have your app make a second round trip to the Rails server to get initialization props.
|
5
|
+
|
6
|
+
**Server-Side Rendering (SSR)** results in Rails rendering HTML for your React components. The main reasons to use SSR are better SEO and pages display more quickly.
|
7
|
+
|
8
|
+
These gems provide advanced integration of React with [rails/webpacker](https://github.com/rails/webpacker):
|
9
|
+
|
10
|
+
| Gem | Props Hydration | Server-Side-Rendering (SSR) | SSR with HMR | SSR with React-Router | SSR with Code Splitting | Node SSR |
|
11
|
+
| --- | --------------- | --- | --------------------- | ----------------------| ------------------------|----|
|
12
|
+
| [shakacode/react_on_rails](https://github.com/shakacode/react_on_rails) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
13
|
+
| [react-rails](https://github.com/reactjs/react-rails) | ✅ | ✅ | | | | | |
|
14
|
+
| [webpacker-react](https://github.com/renchap/webpacker-react) | ✅ | | | | | | |
|
15
|
+
|
16
|
+
Note, Node SSR for React on Rails requires [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
As mentioned, you don't _need_ to use a gem to integrate Rails with React.
|
21
|
+
|
22
|
+
If you're not concerned with view helpers to pass props or server rendering, can do it yourself:
|
23
|
+
|
24
|
+
```erb
|
25
|
+
<%# views/layouts/application.html.erb %>
|
26
|
+
|
27
|
+
<%= content_tag :div,
|
28
|
+
id: "hello-react",
|
29
|
+
data: {
|
30
|
+
message: 'Hello!',
|
31
|
+
name: 'David'
|
32
|
+
}.to_json do %>
|
33
|
+
<% end %>
|
34
|
+
```
|
35
|
+
|
36
|
+
```js
|
37
|
+
// app/javascript/packs/hello_react.js
|
38
|
+
|
39
|
+
const Hello = props => (
|
40
|
+
<div className='react-app-wrapper'>
|
41
|
+
<img src={clockIcon} alt="clock" />
|
42
|
+
<h5 className='hello-react'>
|
43
|
+
{props.message} {props.name}!
|
44
|
+
</h5>
|
45
|
+
</div>
|
46
|
+
)
|
47
|
+
|
48
|
+
// Render component with data
|
49
|
+
document.addEventListener('DOMContentLoaded', () => {
|
50
|
+
const node = document.getElementById('hello-react')
|
51
|
+
const data = JSON.parse(node.getAttribute('data'))
|
52
|
+
|
53
|
+
ReactDOM.render(<Hello {...data} />, node)
|
54
|
+
})
|
55
|
+
```
|
56
|
+
|
57
|
+
----
|
58
|
+
|
59
|
+
## HMR and React Hot Reloading
|
60
|
+
|
61
|
+
Before turning HMR on, consider upgrading to the latest stable gems and packages:
|
62
|
+
https://github.com/rails/webpacker#upgrading
|
63
|
+
|
64
|
+
Configure `config/webpacker.yml` file:
|
65
|
+
|
66
|
+
```yaml
|
67
|
+
development:
|
68
|
+
extract_css: false
|
69
|
+
dev_server:
|
70
|
+
hmr: true
|
71
|
+
inline: true
|
72
|
+
```
|
73
|
+
|
74
|
+
This basic configuration alone will have HMR working with the default webpacker setup. However, an code saves will trigger a full page refresh each time you save a file.
|
75
|
+
|
76
|
+
Webpack's HMR allows the replacement of modules for React in-place without reloading the browser. To do this, you have two options:
|
77
|
+
|
78
|
+
1. Steps below for the [github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin).
|
79
|
+
1. Deprecated steps below for using the [github.com/gaearon/react-hot-loader](https://github.com/gaearon/react-hot-loader).
|
80
|
+
|
81
|
+
### React Refresh Webpack Plugin
|
82
|
+
[github.com/pmmmwh/react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin)
|
83
|
+
|
84
|
+
You can see an example commit of adding this [here](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/commit/7e53803fce7034f5ecff335db1f400a5743a87e7).
|
85
|
+
|
86
|
+
1. Add react refresh packages:
|
87
|
+
`yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
|
88
|
+
2. Update `babel.config.js` adding
|
89
|
+
```js
|
90
|
+
plugins: [
|
91
|
+
process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel',
|
92
|
+
// other plugins
|
93
|
+
```
|
94
|
+
3. Update `config/webpack/development.js`, only including the plugin if running the WEBPACK_DEV_SERVER
|
95
|
+
```js
|
96
|
+
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
|
97
|
+
const environment = require('./environment')
|
98
|
+
|
99
|
+
const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;
|
100
|
+
|
101
|
+
//plugins
|
102
|
+
if (isWebpackDevServer) {
|
103
|
+
environment.plugins.append(
|
104
|
+
'ReactRefreshWebpackPlugin',
|
105
|
+
new ReactRefreshWebpackPlugin({
|
106
|
+
overlay: {
|
107
|
+
sockPort: 3035
|
108
|
+
}
|
109
|
+
})
|
110
|
+
);
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
### React Hot Loader (Deprecated)
|
115
|
+
|
116
|
+
1. Add the `react-hot-loader` and ` @hot-loader/react-dom` npm packages.
|
117
|
+
```sh
|
118
|
+
yarn add --dev react-hot-loader @hot-loader/react-dom
|
119
|
+
```
|
120
|
+
|
121
|
+
2. Update your babel config, `babel.config.js`. Add the plugin `react-hot-loader/babel`
|
122
|
+
with option `"safetyNet": false`:
|
123
|
+
|
124
|
+
```
|
125
|
+
{
|
126
|
+
"plugins": [
|
127
|
+
[
|
128
|
+
"react-hot-loader/babel",
|
129
|
+
{
|
130
|
+
"safetyNet": false
|
131
|
+
}
|
132
|
+
]
|
133
|
+
]
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
3. Add changes like this to your entry points:
|
138
|
+
|
139
|
+
```diff
|
140
|
+
// app/javascript/app.jsx
|
141
|
+
|
142
|
+
import React from 'react';
|
143
|
+
+ import { hot } from 'react-hot-loader/root';
|
144
|
+
|
145
|
+
const App = () => <SomeComponent(s) />
|
146
|
+
|
147
|
+
- export default App;
|
148
|
+
+ export default hot(App);
|
149
|
+
```
|
150
|
+
|
151
|
+
4. Adjust your webpack configuration for development so that `sourceMapContents` option for the sass
|
152
|
+
loader is `false`:
|
153
|
+
|
154
|
+
```diff
|
155
|
+
// config/webpack/development.js
|
156
|
+
|
157
|
+
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
|
158
|
+
|
159
|
+
const environment = require('./environment')
|
160
|
+
|
161
|
+
// allows for editing sass/scss files directly in browser
|
162
|
+
+ if (!module.hot) {
|
163
|
+
+ environment.loaders.get('sass').use.find(item => item.loader === 'sass-loader').options.sourceMapContents = false
|
164
|
+
+ }
|
165
|
+
+
|
166
|
+
module.exports = environment.toWebpackConfig()
|
167
|
+
```
|
168
|
+
|
169
|
+
5. Adjust your `config/webpack/environment.js` for a
|
170
|
+
|
171
|
+
```diff
|
172
|
+
// config/webpack/environment.js
|
173
|
+
|
174
|
+
// ...
|
175
|
+
|
176
|
+
// Fixes: React-Hot-Loader: react-🔥-dom patch is not detected. React 16.6+ features may not work.
|
177
|
+
// https://github.com/gaearon/react-hot-loader/issues/1227#issuecomment-482139583
|
178
|
+
+ environment.config.merge({ resolve: { alias: { 'react-dom': '@hot-loader/react-dom' } } });
|
179
|
+
|
180
|
+
module.exports = environment;
|
181
|
+
```
|
182
|
+
|
@@ -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](
|
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,13 +13,13 @@ 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](
|
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.
|
20
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.
|
21
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.
|
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.
|
23
23
|
|
24
24
|
## Do you need server rendering?
|
25
25
|
|
File without changes
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# RSpec Configuration
|
2
|
-
_Click [here for minitest](
|
2
|
+
_Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/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
|
@@ -8,12 +8,12 @@ compiled by webpack before running tests and during production deployment:
|
|
8
8
|
|
9
9
|
1. **Use rails/webpacker's compile option**: Configure your `config/webpacker.yml` so that `compile: true` is for `test` and `production`
|
10
10
|
environments. Ensure that your `source_path` is correct, or else `rails/webpacker` won't correctly
|
11
|
-
detect changes.
|
12
|
-
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](
|
11
|
+
detect changes.
|
12
|
+
2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
|
13
13
|
|
14
14
|
```yml
|
15
15
|
config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/webpack"
|
16
|
-
```
|
16
|
+
```
|
17
17
|
|
18
18
|
Which should you use? If you're already using the `rails/webpacker` way to configure webpack, then
|
19
19
|
you can keep things simple and use the `rails/webpacker` options.
|
@@ -25,7 +25,7 @@ Because you will probably want to run RSpec tests that rely on compiled webpack
|
|
25
25
|
As mentioned above, you can configure `compile: true` in `config/webpacker.yml` _if_ you've got configuration for
|
26
26
|
your webpack in the standard `rails/webpacker` spot of `config/webpack/<NODE_ENV>.js`
|
27
27
|
|
28
|
-
ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](
|
28
|
+
ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/test_helper.rb) for more details. You can customize this to your particular needs by replacing any of the default components used by `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`.
|
29
29
|
|
30
30
|
```ruby
|
31
31
|
RSpec.configure do |config|
|
@@ -44,22 +44,22 @@ If you are using Webpack to build CSS assets, you should do something like this
|
|
44
44
|
```
|
45
45
|
|
46
46
|
Please take note of the following:
|
47
|
-
- If you are using Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
|
47
|
+
- If you are using Webpacker, be **SURE** to configure the `source_path` in your `config/webpacker.yml` unless you are using the defaults for webpacker.
|
48
48
|
|
49
49
|
- This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using webpacker, the default value will come from the `config/webpacker.yml` value for the `public_output_path` and the `source_path`
|
50
50
|
|
51
|
-
- If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
|
51
|
+
- If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
|
52
52
|
|
53
|
-
- By default, the webpack processes look in the webpack generated files folder, configured via the `config/webpacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
|
53
|
+
- By default, the webpack processes look in the webpack generated files folder, configured via the `config/webpacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
|
54
54
|
|
55
55
|
The following `config/react_on_rails.rb` settings **must** match your setup:
|
56
56
|
```ruby
|
57
57
|
# Define the files we need to check for webpack compilation when running tests.
|
58
58
|
config.webpack_generated_files = %w( manifest.json )
|
59
|
-
|
59
|
+
|
60
60
|
# OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
|
61
61
|
# config.webpack_generated_files = %w( server-bundle.js manifest.json )
|
62
|
-
|
62
|
+
|
63
63
|
# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
|
64
64
|
# with rspec then this controls what yarn command is run
|
65
65
|
# to automatically refresh your webpack assets on every test run.
|
@@ -1,7 +1,5 @@
|
|
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
|
-----
|
@@ -52,7 +50,7 @@ First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If
|
|
52
50
|
cd <directory where you want to create your new Rails app>
|
53
51
|
|
54
52
|
# Any name you like for the rails app
|
55
|
-
# Skip javascript so will add that next and get the current version
|
53
|
+
# Skip javascript so will add that next and get the current version
|
56
54
|
rails new --skip-sprockets -J --skip-turbolinks test-react-on-rails-v12-no-sprockets
|
57
55
|
|
58
56
|
cd test-react-on-rails
|
@@ -96,7 +94,7 @@ git commit -m "Initial commit"
|
|
96
94
|
|
97
95
|
Install React on Rails: `rails generate react_on_rails:install`. You need to first git commit your files before running the generator, or else it will generate an error.
|
98
96
|
|
99
|
-
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
97
|
+
Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
|
100
98
|
If you want the redux install: `rails generate react_on_rails:install --redux`
|
101
99
|
|
102
100
|
```
|
@@ -123,7 +121,7 @@ First, check that the `hmr` and the `inline` options are `true` in your `config/
|
|
123
121
|
|
124
122
|
The basic setup will have HMR working with the default webpacker setup. The basic
|
125
123
|
[HMR](https://webpack.js.org/concepts/hot-module-replacement/), without a special
|
126
|
-
React setup, will cause a full page refresh each time you save a file.
|
124
|
+
React setup, will cause a full page refresh each time you save a file.
|
127
125
|
|
128
126
|
## Deploying to Heroku
|
129
127
|
|
@@ -292,8 +290,8 @@ versus with server rendering:
|
|
292
290
|
|
293
291
|
For more details on server rendering, see:
|
294
292
|
|
295
|
-
+ [Client vs. Server Rendering](
|
296
|
-
+ [React Server Rendering](
|
293
|
+
+ [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/)
|
294
|
+
+ [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/react-server-rendering/)
|
297
295
|
|
298
296
|
## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
|
299
297
|
|
@@ -320,7 +318,7 @@ When you change a JSX file and save, the browser will automatically refresh!
|
|
320
318
|
|
321
319
|
So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
|
322
320
|
|
323
|
-
* https://github.com/rails/webpacker/blob/
|
321
|
+
* https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md
|
324
322
|
* https://webpack.js.org/configuration/dev-server/
|
325
323
|
* https://webpack.js.org/concepts/hot-module-replacement/
|
326
324
|
|
@@ -335,7 +333,7 @@ In case you are running some custom setup with different IP or PORT you should a
|
|
335
333
|
web: rails s -p 8080 -b 0.0.0.0
|
336
334
|
```
|
337
335
|
|
338
|
-
Then visit https://your-shared-addr.c9users.io:8080/hello_world
|
336
|
+
Then visit https://your-shared-addr.c9users.io:8080/hello_world
|
339
337
|
|
340
338
|
## RubyMine
|
341
339
|
|
@@ -349,8 +347,8 @@ It's super important to exclude certain directories from RubyMine or else it wil
|
|
349
347
|
|
350
348
|
## Conclusion
|
351
349
|
|
352
|
-
* Browse the docs
|
350
|
+
* Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
|
353
351
|
|
354
|
-
Feedback is greatly appreciated! As are stars on github!
|
352
|
+
Feedback is greatly appreciated! As are stars on github!
|
355
353
|
|
356
354
|
If you want personalized help, don't hesitate to get in touch with us at [contact@shakacode.com](mailto:contact@shakacode.com). We offer [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki) and consulting so you can focus on your app and not on how to make Webpack plus Rails work optimally.
|
@@ -9,7 +9,7 @@ We specialize in helping companies to quickly and efficiently upgrade. The older
|
|
9
9
|
### Recent versions
|
10
10
|
Make sure that you are on a relatively more recent version of rails and webpacker. Yes, the [rails/webpacker](https://github.com/rails/webpacker) gem is required!
|
11
11
|
v12 is tested on Rails 6. It should work on Rails v5. If you're on any older version,
|
12
|
-
and v12 doesn't work, please file an issue.
|
12
|
+
and v12 doesn't work, please file an issue.
|
13
13
|
|
14
14
|
### Removed Configuration config.symlink_non_digested_assets_regex
|
15
15
|
Remove `config.symlink_non_digested_assets_regex` from your `config/initializers/react_on_rails.rb`.
|
@@ -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](
|
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.
|
@@ -35,14 +35,14 @@ Registered Objects are of the following type:
|
|
35
35
|
```js
|
36
36
|
export default (props) => <Component {...props} />;
|
37
37
|
```
|
38
|
-
|
38
|
+
|
39
39
|
2. **Function that takes only zero or one params and you return an Object (_not a React Element_)**. If the function takes zero or one params, **you need to add one or two unused params so you have exactly 2 params** and then that function will be treated as a render function and it can return an Object rather than a React element. If you don't do this, you'll see this obscure error message:
|
40
40
|
|
41
41
|
```
|
42
42
|
[SERVER] message: Objects are not valid as a React child (found: object with keys {renderedHtml}). If you meant to render a collection of children, use an array instead.
|
43
43
|
in YourComponentRenderFunction
|
44
44
|
```
|
45
|
-
|
45
|
+
|
46
46
|
So look in YourComponentRenderFunction and do this change
|
47
47
|
|
48
48
|
```js
|
@@ -50,7 +50,7 @@ Registered Objects are of the following type:
|
|
50
50
|
```
|
51
51
|
|
52
52
|
To have exactly 2 arguments:
|
53
|
-
|
53
|
+
|
54
54
|
```js
|
55
55
|
export default (props, _railsContext) => { renderedHTML: getRenderedHTML };
|
56
56
|
```
|
@@ -65,13 +65,13 @@ Registered Objects are of the following type:
|
|
65
65
|
an Object**, then no migration is required.
|
66
66
|
4. Function that takes **3 params** and uses the 3rd param, `domNodeId`, to call `ReactDOM.hydrate`. If the function takes 3 params, there is **no migration needed** for that function.
|
67
67
|
5. ES6 or ES5 class. There is **no migration needed**.
|
68
|
-
|
68
|
+
|
69
69
|
Previously, with case number 2, you could return a React Element.
|
70
70
|
|
71
71
|
The fix is simple. Here is an example of the change you'll do:
|
72
72
|
|
73
73
|
![2020-07-07_09-43-51 (1)](https://user-images.githubusercontent.com/1118459/86927351-eff79e80-c0ce-11ea-9172-d6855c45e2bb.png)
|
74
|
-
|
74
|
+
|
75
75
|
##### Broken, as this function takes two params and it returns a React Element from a JSX Literal
|
76
76
|
```js
|
77
77
|
export default (props, railsContext) => <Component {{...props, railsContext} />;
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# Webpack Configuration: custom setup for Webpack or rails/webpacker?
|
2
2
|
|
3
|
-
## Webpack vs. rails/webpacker
|
3
|
+
## Webpack vs. rails/webpacker
|
4
4
|
|
5
|
-
[Webpack](https://webpack.js.org) is the JavaScript npm package that prepares all your client-side assets. The Rails asset pipeline used to be the preferable way to prepare client-side assets.
|
5
|
+
[Webpack](https://webpack.js.org) is the JavaScript npm package that prepares all your client-side assets. The Rails asset pipeline used to be the preferable way to prepare client-side assets.
|
6
6
|
|
7
7
|
[rails/webpacker](https://github.com/rails/webpacker) is the Ruby gem that mainly gives us 2 things:
|
8
8
|
|
@@ -22,13 +22,13 @@ 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/
|
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
|
|
29
29
|
## Option 2: Traditional React on Rails using the /client directory
|
30
30
|
|
31
|
-
Until version 9, all React on Rails apps used the `/client` directory for configuring React on Rails in terms of the configuration of Webpack and location of your JavaScript and Webpack files, including the node_modules directory. Version 9 changed the default to `/` for the `node_modules` location using this value in `config/initializers/react_on_rails.rb`: `config.node_modules_location`.
|
31
|
+
Until version 9, all React on Rails apps used the `/client` directory for configuring React on Rails in terms of the configuration of Webpack and location of your JavaScript and Webpack files, including the node_modules directory. Version 9 changed the default to `/` for the `node_modules` location using this value in `config/initializers/react_on_rails.rb`: `config.node_modules_location`.
|
32
32
|
|
33
33
|
You can access values in the `config/webpacker.yml`
|
34
34
|
|
@@ -40,5 +40,3 @@ You will want consider using some of the same values set in these files:
|
|
40
40
|
|
41
41
|
* https://github.com/rails/webpacker/blob/master/package/environments/base.js
|
42
42
|
* https://github.com/rails/webpacker/blob/master/package/environments/development.js
|
43
|
-
|
44
|
-
|
data/docs/home.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# React on Rails
|
2
|
+
|
3
|
+
## Details
|
4
|
+
1. [Overview](https://www.shakacode.com/react-on-rails/docs/guides/react-on-rails-overview/)
|
5
|
+
1. [Getting Started](https://www.shakacode.com/react-on-rails/docs/guides/getting-started/)
|
6
|
+
1. [How React on Rails Works](https://www.shakacode.com/react-on-rails/docs/guides/how-react-on-rails-works/)
|
7
|
+
1. [Webpack Configuration](https://www.shakacode.com/react-on-rails/docs/guides/webpack-configuration/)
|
8
|
+
1. [View Helpers API](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api/)
|
9
|
+
1. [Caching and Performance: React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/).
|
10
|
+
1. [Deployment](https://www.shakacode.com/react-on-rails/docs/guides/deployment/).
|
11
|
+
|
12
|
+
## Changes and Upgrades
|
13
|
+
1. [CHANGELOG.md](https://github.com/shakacode/react_on_rails/tree/master/CHANGELOG.md)
|
14
|
+
2. [Upgrading React on Rails](https://www.shakacode.com/react-on-rails/docs/guides/upgrading-react-on-rails/#upgrading-to-v12).
|
15
|
+
|
16
|
+
## Example Apps
|
17
|
+
1. [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy) example repo for a simple configuration of webpack via the rails/webpacker gem
|
18
|
+
that supports SSR.
|
19
|
+
2. 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`.
|
20
|
+
3. Live, [open source](https://github.com/shakacode/react-webpack-rails-tutorial), example of this gem, see [reactrails.com](http://reactrails.com).
|
21
|
+
|
22
|
+
# Other Resources
|
23
|
+
1. RailsConf 2020 talk: [Webpacker, It-Just-Works, But How?](http://railsconf.com/2020/video/justin-gordon-webpacker-it-just-works-but-how)
|
File without changes
|
File without changes
|
File without changes
|