react_on_rails 11.3.0 → 12.0.0.pre.beta.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 +320 -0
- data/.eslintignore +2 -1
- data/.eslintrc +23 -1
- data/.github/FUNDING.yml +1 -0
- data/.gitignore +3 -1
- data/.prettierignore +10 -1
- data/.prettierrc +3 -0
- data/.rubocop.yml +37 -11
- data/.travis.yml +9 -22
- data/CHANGELOG.md +46 -4
- data/CONTRIBUTING.md +60 -71
- data/Gemfile +3 -4
- data/{COMM-LICENSE → REACT-ON-RAILS-PRO-LICENSE} +6 -9
- data/README.md +102 -69
- data/Rakefile +0 -7
- data/SUMMARY.md +7 -11
- data/book.json +5 -5
- data/docs/additional-reading/asset-pipeline.md +8 -16
- data/docs/additional-reading/react-helmet.md +30 -10
- data/docs/additional-reading/react-router.md +52 -75
- data/docs/additional-reading/server-rendering-tips.md +12 -7
- data/docs/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +4 -2
- data/docs/api/view-helpers-api.md +6 -7
- data/docs/basics/configuration.md +60 -57
- data/docs/basics/deployment.md +1 -2
- data/docs/basics/i18n.md +44 -22
- data/docs/basics/installation-into-an-existing-rails-app.md +2 -2
- data/docs/basics/react-server-rendering.md +1 -1
- data/docs/basics/{generator-functions-and-railscontext.md → render-functions-and-railscontext.md} +59 -21
- data/docs/basics/upgrading-react-on-rails.md +50 -2
- data/docs/basics/webpack-configuration.md +15 -1
- data/docs/contributor-info/errors-with-hooks.md +45 -0
- data/docs/contributor-info/pull-requests.md +44 -0
- data/docs/misc/doctrine.md +1 -1
- data/docs/{misc-pending → outdated}/code-splitting.md +12 -8
- data/docs/{additional-reading → outdated}/heroku-deployment.md +0 -6
- data/docs/{basics → outdated}/how-react-on-rails-works.md +2 -2
- data/docs/{misc-pending → outdated}/manual-installation-overview.md +5 -5
- data/docs/{additional-reading → outdated}/rails-assets-relative-paths.md +3 -3
- data/docs/{misc-pending → outdated}/rails-assets.md +4 -7
- data/docs/{misc → outdated}/rails3.md +0 -0
- data/docs/tutorial.md +54 -34
- data/jest.config.js +4 -0
- data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
- data/lib/generators/react_on_rails/generator_helper.rb +4 -6
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +9 -8
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +4 -8
- data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/store/helloWorldStore.js +1 -3
- data/lib/react_on_rails.rb +3 -1
- data/lib/react_on_rails/configuration.rb +13 -22
- data/lib/react_on_rails/error.rb +2 -0
- data/lib/react_on_rails/helper.rb +41 -91
- data/lib/react_on_rails/json_parse_error.rb +2 -0
- data/lib/react_on_rails/locales/base.rb +142 -0
- data/lib/react_on_rails/locales/to_js.rb +37 -0
- data/lib/react_on_rails/locales/to_json.rb +27 -0
- data/lib/react_on_rails/prerender_error.rb +11 -15
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +41 -46
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +7 -8
- data/lib/react_on_rails/utils.rb +14 -19
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/version_checker.rb +1 -0
- data/lib/react_on_rails/webpacker_utils.rb +13 -2
- data/lib/tasks/assets.rake +5 -45
- data/lib/tasks/locale.rake +8 -2
- data/package-scripts.yml +11 -8
- data/package.json +29 -28
- data/rakelib/dummy_apps.rake +1 -9
- data/rakelib/example_type.rb +3 -1
- data/rakelib/examples.rake +3 -0
- data/rakelib/lint.rake +2 -7
- data/rakelib/node_package.rake +2 -2
- data/rakelib/run_rspec.rake +5 -18
- data/react_on_rails.gemspec +3 -5
- data/tsconfig.json +14 -0
- data/webpackConfigLoader.js +3 -2
- data/yarn.lock +4170 -2197
- metadata +30 -56
- data/Gemfile.rails32 +0 -73
- data/docs/additional-reading/babel.md +0 -5
- data/docs/additional-reading/hot-reloading-rails-development-asset-pipeline.md +0 -47
- data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
- data/lib/react_on_rails/assets_precompile.rb +0 -153
- data/lib/react_on_rails/locales_to_js.rb +0 -138
@@ -23,7 +23,21 @@ The [ShakaCode Team](http://www.shakacode.com) _recommends_ this approach for pr
|
|
23
23
|
|
24
24
|
The two best examples of this pattern are the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) and the integration test example in [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy).
|
25
25
|
|
26
|
-
In this case, you don't need to understand the nuances of customization of your
|
26
|
+
In this case, you don't need to understand the nuances of customization of your Webpack config via the [Webpacker mechanism](./docs/additional-reading/webpack-tips.md).
|
27
|
+
|
28
|
+
You can access values in the `config/webpacker.yml`
|
29
|
+
|
30
|
+
```js
|
31
|
+
const { config, devServer } = require('@rails/webpacker');
|
32
|
+
```
|
33
|
+
|
34
|
+
You will want consider using some of the same values set in these files:
|
35
|
+
|
36
|
+
* https://github.com/rails/webpacker/blob/master/package/environments/base.js
|
37
|
+
* https://github.com/rails/webpacker/blob/master/package/environments/development.js
|
38
|
+
|
39
|
+
**Note**, if your node_modules directory is not at the top level of the Rails project, then you will need to set the
|
40
|
+
ENV value of WEBPACKER_CONFIG to the location of the `config/webpacker.yml` file per [rails/webpacker PR 2561](https://github.com/rails/webpacker/pull/2561).
|
27
41
|
|
28
42
|
## Option 2: Default Generator Setup: rails/webpacker app/javascript
|
29
43
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Invalid hook call error
|
2
|
+
|
3
|
+
```
|
4
|
+
react.development.js:1465 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
|
5
|
+
1. You might have mismatching versions of React and the renderer (such as React DOM)
|
6
|
+
2. You might be breaking the Rules of Hooks
|
7
|
+
3. You might have more than one copy of React in the same app
|
8
|
+
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
|
9
|
+
```
|
10
|
+
|
11
|
+
The main reason to get this is due to multiple versions of React installed.
|
12
|
+
|
13
|
+
```
|
14
|
+
cd <top level>
|
15
|
+
npm ls react
|
16
|
+
|
17
|
+
cd spec/dummy
|
18
|
+
npm ls react
|
19
|
+
```
|
20
|
+
|
21
|
+
For the second one, you might get:
|
22
|
+
|
23
|
+
```
|
24
|
+
react_on_rails@ /Users/justin/shakacode/react-on-rails/react_on_rails/spec/dummy
|
25
|
+
├── react@16.13.1
|
26
|
+
└─┬ react-on-rails@12.0.0 -> /Users/justin/shakacode/react-on-rails/react_on_rails invalid
|
27
|
+
└── react@16.13.1 extraneous
|
28
|
+
|
29
|
+
npm ERR! invalid: react-on-rails@12.0.0 /Users/justin/shakacode/react-on-rails/react_on_rails/spec/dummy/node_modules/react-on-rails
|
30
|
+
npm ERR! extraneous: react@16.13.1 /Users/justin/shakacode/react-on-rails/react_on_rails/spec/dummy/node_modules/react-on-rails/node_modules/react
|
31
|
+
```
|
32
|
+
|
33
|
+
Make sure there is only one version of React installed!
|
34
|
+
|
35
|
+
If you used yarn link, then you'll have two versions of React installed.
|
36
|
+
|
37
|
+
Instead use [Yalc](https://github.com/whitecolor/yalc).
|
38
|
+
|
39
|
+
```
|
40
|
+
cd <top level>
|
41
|
+
yalc publish
|
42
|
+
|
43
|
+
cd spec/dummy
|
44
|
+
yalc link react-on-rails
|
45
|
+
```
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Pull Requests
|
2
|
+
|
3
|
+
## Checklist before Committing
|
4
|
+
1. `rake`: runs all linters and specs (you need Docker setup, see below)
|
5
|
+
2. Did you need any more tests for your change?
|
6
|
+
3. Did you document your change? Update the README.md?
|
7
|
+
4. Did you add a CHANGELOG.md entry?
|
8
|
+
|
9
|
+
|
10
|
+
For non-doc fixes:
|
11
|
+
|
12
|
+
* Provide changelog entry in the [unreleased section of the CHANGELOG.md](https://github.com/shakacode/react_on_rails/blob/master/CHANGELOG.md#unreleased).
|
13
|
+
* Ensure CI passes and that you added a test that passes with the fix and fails without the fix.
|
14
|
+
* Squash all commits down to one with a nice commit message *ONLY* once final review is given. Make sure this single commit is rebased on top of master.
|
15
|
+
* Please address all code review comments.
|
16
|
+
* Ensure that docs are updated accordingly if a feature is added.
|
17
|
+
|
18
|
+
## Commit Messages
|
19
|
+
|
20
|
+
From [How to Write a Git Commit Message](http://chris.beams.io/posts/git-commit/)
|
21
|
+
|
22
|
+
#### The seven rules of a great git commit message
|
23
|
+
> Keep in mind: This has all been said before.
|
24
|
+
|
25
|
+
1. Separate subject from body with a blank line
|
26
|
+
1. Limit the subject line to 50 characters
|
27
|
+
1. Capitalize the subject line
|
28
|
+
1. Do not end the subject line with a period
|
29
|
+
1. Use the imperative mood in the subject line
|
30
|
+
1. Wrap the body at 72 characters
|
31
|
+
1. Use the body to explain what and why vs. how
|
32
|
+
|
33
|
+
## Doc Changes
|
34
|
+
|
35
|
+
When making doc changes, we want the change to work on both the gitbook and the regular github site. The issue is that non-doc files will not go to the gitbook site, so doc references to non doc files must use the github URL.
|
36
|
+
|
37
|
+
### Links to other docs:
|
38
|
+
* When making references to doc files, use a relative URL path like:
|
39
|
+
`[Installation Overview](docs/basics/installation-overview.md)`
|
40
|
+
|
41
|
+
* When making references to source code files, use a full url path like:
|
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
|
+
|
data/docs/misc/doctrine.md
CHANGED
@@ -19,7 +19,7 @@ The React on Rails setup provides several key components related to front-end de
|
|
19
19
|
6. Happiness for us is actively participating in open source, so we want to be where the action is, which is with the npm libraries on github.com.
|
20
20
|
7. You can get set up on React on Rails **FAST** using our application generator.
|
21
21
|
8. By placing all client-side development inside of the `/client` directory, pure JavaScript developers can productively do development separate from Rails. Instead of Rails APIs, stub APIs on an express server can provide a simple backend, allowing for rapid iteration of UI prototypes.
|
22
|
-
9. Just because we're not relying on the Rails asset pipeline for ES6 conversion does not mean that we're deploying Rails apps in any different way. We still use the asset pipeline to include our Webpack compiled JavaScript. This only requires a few small modifications, as explained in our doc [Heroku Deployment](
|
22
|
+
9. Just because we're not relying on the Rails asset pipeline for ES6 conversion does not mean that we're deploying Rails apps in any different way. We still use the asset pipeline to include our Webpack compiled JavaScript. This only requires a few small modifications, as explained in our doc [Heroku Deployment](docs/outdated/heroku-deployment.md).
|
23
23
|
|
24
24
|
## Convention over Configuration
|
25
25
|
* React on Rails has taken the hard work out of figuring out the JavaScript tooling that works best with Rails. Not only could you spend lots of time researching different tooling, but then you'd have to figure out how to splice it all together. This is where a lot of "JavaScript fatigue" comes from. The following keep the code clean and consistent:
|
@@ -1,6 +1,10 @@
|
|
1
|
-
# Code Splitting
|
1
|
+
# Code Splitting (Outdated)
|
2
2
|
|
3
|
-
|
3
|
+
_Note: This document is outdated._ Please email [justin@shakacode.com](mailto:justin@shakacode.com)
|
4
|
+
if you would be interested in help with code splitting using
|
5
|
+
[loadable-components.com](https://loadable-components.com/docs) with React on Rails.
|
6
|
+
|
7
|
+
-----
|
4
8
|
|
5
9
|
What is code splitting? From the webpack documentation:
|
6
10
|
|
@@ -17,11 +21,11 @@ Let's say you're requesting a page that needs to fetch a code chunk from the ser
|
|
17
21
|
> (server) <div data-reactroot="
|
18
22
|
<!--This comment is here because the comment beginning on line 13 messes up Sublime's markdown parsing-->
|
19
23
|
|
20
|
-
Different markup is generated on the client than on the server. Why does this happen? When you register a component or
|
24
|
+
Different markup is generated on the client than on the server. Why does this happen? When you register a component or render function with `ReactOnRails.register`, react on rails will render the component as soon as the page loads. However, react-router renders a comment while waiting for the code chunk to be fetched from the server. This means that react will tear all of the server rendered code out of the DOM, and then rerender it a moment later once the code chunk arrives from the server, defeating most of the purpose of server rendering.
|
21
25
|
|
22
26
|
### The solution
|
23
27
|
|
24
|
-
To prevent this, you have to wait until the code chunk is fetched before doing the initial render on the client side. To accomplish this, react on rails allows you to register a renderer. This works just like registering a
|
28
|
+
To prevent this, you have to wait until the code chunk is fetched before doing the initial render on the client side. To accomplish this, react on rails allows you to register a renderer. This works just like registering a render function, except that the function you pass takes three arguments: `renderer(props, railsContext, domNodeId)`, and is responsible for calling `ReactDOM.render` or `ReactDOM.hydrate` to render the component to the DOM. React on rails will automatically detect when a render function takes three arguments, and will **not** call `ReactDOM.render` or `ReactDOM.hydrate`, instead allowing you to control the initial render yourself. Note, you have to be careful to call `ReactDOM.hydrate` rather than `ReactDOM.render` if you are are server rendering.
|
25
29
|
|
26
30
|
Here's an example of how you might use this in practice:
|
27
31
|
|
@@ -34,7 +38,7 @@ Here's an example of how you might use this in practice:
|
|
34
38
|
|
35
39
|
#### clientRegistration.js
|
36
40
|
```js
|
37
|
-
import ReactOnRails from '
|
41
|
+
import ReactOnRails from 'node_package/lib/ReactOnRails';
|
38
42
|
import NavigationApp from './NavigationApp';
|
39
43
|
|
40
44
|
// Note that we're importing a different RouterApp than in serverRegistration.js
|
@@ -78,7 +82,7 @@ import { Provider } from 'react-redux';
|
|
78
82
|
|
79
83
|
import routes from '../routes/routes';
|
80
84
|
|
81
|
-
|
85
|
+
// NOTE how this function takes 3 params, and is thus responsible for calling ReactDOM.render
|
82
86
|
const RouterAppRenderer = (props, railsContext, domNodeId) => {
|
83
87
|
const store = ReactOnRails.getStore('applicationStore');
|
84
88
|
const history = browserHistory;
|
@@ -130,7 +134,7 @@ If you're going to try to do code splitting with server rendered routes, you'll
|
|
130
134
|
|
131
135
|
The reason is we do server rendering with ExecJS, which is not capable of doing anything asynchronous. It would be impossible to asyncronously fetch a code chunk while server rendering. See [this issue](https://github.com/shakacode/react_on_rails/issues/477) for a discussion.
|
132
136
|
|
133
|
-
Also, do not attempt to register a renderer on the server. Instead, register either a
|
137
|
+
Also, do not attempt to register a renderer on the server. Instead, register either a render function or a component. If you register a renderer in the server bundle, you'll get an error when react on rails tries to server render the component.
|
134
138
|
|
135
139
|
## How does Webpack know where to find my code chunks?
|
136
140
|
|
@@ -146,7 +150,7 @@ config = {
|
|
146
150
|
|
147
151
|
This causes Webpack to prepend the code chunk filename with `/assets/` in the request url. The react on rails sets up the webpack config to put webpack bundles in `app/assets/javascripts/webpack`, and modifies `config/initializers/assets.rb` so that rails detects the bundles. This means that when we prepend the request url with `/assets/`, rails will know what webpack is asking for.
|
148
152
|
|
149
|
-
See [rails-assets.md](rails-assets.md) to learn more about static assets.
|
153
|
+
See [rails-assets.md](docs/outdated/rails-assets.md) to learn more about static assets.
|
150
154
|
|
151
155
|
If you forget to set the public path, webpack will request the code chunk at `/{filename}`. This will cause the request to be handled by the Rails router, which will send back a 404 response, assuming that you don't have a catch-all route. In your javascript console, you'll get the following error:
|
152
156
|
|
@@ -2,10 +2,8 @@
|
|
2
2
|
The generator has created the necessary files and gems for deployment to Heroku. If you have installed manually, you will need to provide these files yourself:
|
3
3
|
|
4
4
|
+ `Procfile`: used by Heroku and Foreman to start the Puma server
|
5
|
-
+ `12factor` gem: required by Heroku if using a version before Rails 5 (see their [README](https://github.com/heroku/rails_12factor#rails-5) for more information if upgrading from a lower version)
|
6
5
|
+ `'puma'` gem: recommended Heroku webserver
|
7
6
|
+ `config/puma.rb`: Puma webserver config file
|
8
|
-
+ `/package.json`: Top level package.json which must contain `"scripts": { "postinstall": "cd client && npm install" }`
|
9
7
|
|
10
8
|
If you want to see an updated example deployed to Heroku, please visit the [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial).
|
11
9
|
|
@@ -17,10 +15,6 @@ This is how the `assets:precompile` rake task gets modified by `react_on_rails`.
|
|
17
15
|
Rake::Task["assets:precompile"]
|
18
16
|
.clear_prerequisites
|
19
17
|
.enhance([:environment, "react_on_rails:assets:compile_environment"])
|
20
|
-
.enhance do
|
21
|
-
Rake::Task["react_on_rails:assets:symlink_non_digested_assets"].invoke
|
22
|
-
Rake::Task["react_on_rails:assets:delete_broken_symlinks"].invoke
|
23
|
-
end
|
24
18
|
```
|
25
19
|
|
26
20
|
For an example of how to do this, see the [dummy app](https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/lib/tasks/assets.rake).
|
@@ -32,9 +32,9 @@ Each time you change your client code, you will need to re-generate the bundles
|
|
32
32
|
|
33
33
|
Simply run `foreman start -f Procfile.dev`. [Example](spec/dummy/Procfile.static).
|
34
34
|
|
35
|
-
On production deployments that use asset precompilation, such as Heroku deployments, React on Rails, by default, will automatically run webpack to build your JavaScript bundles. You configure the command used as `config.build_production_command` in your [config/initializers/react_on_rails.rb](
|
35
|
+
On production deployments that use asset precompilation, such as Heroku deployments, React on Rails, by default, will automatically run webpack to build your JavaScript bundles. You configure the command used as `config.build_production_command` in your [config/initializers/react_on_rails.rb](docs/basics/configuration.md).
|
36
36
|
|
37
|
-
You can see the source code for what gets added to your precompilation [here](https://github.com/shakacode/react_on_rails/tree/master/lib/tasks/assets.rake). For more information on this topic, see [the doc on Heroku deployment](docs/
|
37
|
+
You can see the source code for what gets added to your precompilation [here](https://github.com/shakacode/react_on_rails/tree/master/lib/tasks/assets.rake). For more information on this topic, see [the doc on Heroku deployment](docs/outdated/heroku-deployment.md#more-details-on-precompilation-using-webpack-to-create-javascript-assets).
|
38
38
|
|
39
39
|
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.
|
40
40
|
|
@@ -14,17 +14,17 @@ The only requirements within this directory for basic React on Rails integration
|
|
14
14
|
1. Your webpack configuration files:
|
15
15
|
1. Create outputs in a directory like `/public/webpack`, which is customizable in your `config/initializers/react_on_rails.rb`.
|
16
16
|
1. Provide server rendering if you wish to use that feature.
|
17
|
-
1. Your JavaScript code "registers" any components and stores per the ReactOnRails APIs of ReactOnRails.register(components) and ReactOnRails.registerStore(stores). See API docs in [README.md](
|
17
|
+
1. Your JavaScript code "registers" any components and stores per the ReactOnRails APIs of ReactOnRails.register(components) and ReactOnRails.registerStore(stores). See API docs in [README.md](README.md) and the [ReactOnRails.js source](../../node_package/src/ReactOnRails.js).
|
18
18
|
1. Set your registration file as an "entry" point in your Webpack configs.
|
19
19
|
1. Add the [Manifest plugin](https://github.com/danethurber/webpack-manifest-plugin) to your config. For examples see [dummy config](../../spec/dummy/client/webpack.client.base.config.js).
|
20
20
|
The default path: `public/webpack` can be loaded with webpackConfigLoader as shown in the dummy example.
|
21
21
|
1. You create scripts in `client/package.json` per the example apps. These are used for building your Webpack assets. Also do this for your top level `package.json`.
|
22
22
|
|
23
23
|
## Rails Steps (outside of /client)
|
24
|
-
1. Add `gem "webpacker"` to the Gemfile, run bundle. The gem provides the `stylesheet_pack_tag` and `javascript_pack_tag` helpers which is used to load the bundled assets to your layouts.[Dummy Example](
|
25
|
-
1. Configure the `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [spec/dummy/config/initializers/react_on_rails.rb](
|
24
|
+
1. Add `gem "webpacker"` to the Gemfile, run bundle. The gem provides the `stylesheet_pack_tag` and `javascript_pack_tag` helpers which is used to load the bundled assets to your layouts.[Dummy Example](spec/dummy/app/views/layouts/application.html.erb)
|
25
|
+
1. Configure the `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [spec/dummy/config/initializers/react_on_rails.rb](spec/dummy/config/initializers/react_on_rails.rb) for a detailed example of configuration, including comments on the different values to configure.
|
26
26
|
1. Configure your Procfiles per the example apps. These are at the root of your Rails installation.
|
27
|
-
1. Configure your top level JavaScript files for inclusion in your layout. You'll want a version that you use for static assets, and you want a file for any files in your setup that are not part of your webpack build. The reason for this is for use with hot-reloading. If you are not using hot reloading, then you only need to configure your `application.js` file to include your Webpack generated files.
|
28
|
-
1. If you are deploying to Heroku, see [heroku-deployment.md](/docs/
|
27
|
+
1. Configure your top level JavaScript files for inclusion in your layout. You'll want a version that you use for static assets, and you want a file for any files in your setup that are not part of your webpack build. The reason for this is for use with hot-reloading. If you are not using hot reloading, then you only need to configure your `application.js` file to include your Webpack generated files.
|
28
|
+
1. If you are deploying to Heroku, see [heroku-deployment.md](/docs/outdated/heroku-deployment.md)
|
29
29
|
|
30
30
|
If I missed anything, please submit a PR or file an issue.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
*Note: this doc
|
1
|
+
*Note: this doc reflects using Sprockets for assets and has not been updated for rails/webpacker*
|
2
2
|
|
3
3
|
# Using Webpack bundled assets with the Rails Asset Pipeline
|
4
4
|
|
@@ -77,7 +77,7 @@ Note: _You can output these files in the asset pipeline wherever you see fit. My
|
|
77
77
|
|
78
78
|
Lastly, we will set the publicPath to our file(s). This will be the endpoint on our rails web server that you can visit to reach the asset (if you don't know how this works, read the [intro](#using-webpack-bundled-assets-with-the-rails-asset-pipeline)). If you've been following the previous steps, you know that we set our outputPath for our assets to be absolute at `app/assets/webpack/webpack-assets/`, which your rails app should end up hosting at `/assets/webpack-assets/file-name+hash.ext` when the server is run.
|
79
79
|
|
80
|
-
Note: _If you're having a hard time figuring out what an asset's path will be on your rails server, simply run `rake assets:precompile` and `cd public/`. The path from there to your file will then be the path/url on your web server to that asset. On top of this, it is also a good idea to check out [this doc](
|
80
|
+
Note: _If you're having a hard time figuring out what an asset's path will be on your rails server, simply run `rake assets:precompile` and `cd public/`. The path from there to your file will then be the path/url on your web server to that asset. On top of this, it is also a good idea to check out [this doc](docs/outdated/rails-assets.md) to understand how `react_on_rails` allows us to access these files after precompilation, when Rails applies another hash onto the asset._
|
81
81
|
|
82
82
|
Our publicPath setting will match the path to our outputted assets on our rails web server. Given our assets in this example will be outputted to `/app/assets/webpack/webpack-assets/` and hosted at `/assets/webpack-assets/`, our publicPath would be:
|
83
83
|
|
@@ -192,4 +192,4 @@ module.exports = {
|
|
192
192
|
};
|
193
193
|
```
|
194
194
|
|
195
|
-
If you'd like to understand how react_on_rails handles these bundled assets after asset precompilation and in production mode, check out: [Rails Assets](
|
195
|
+
If you'd like to understand how react_on_rails handles these bundled assets after asset precompilation and in production mode, check out: [Rails Assets](docs/outdated/rails-assets.md).
|
@@ -33,21 +33,18 @@ fail to load.
|
|
33
33
|
_If you are interested in learning how to use assets in your React components, read this doc: [Webpack, the Asset Pipeline, and Using Assets w/ React](../additional-reading/rails-assets-relative-paths.md)._
|
34
34
|
|
35
35
|
## The Solution: Symlink Original File Names to New File Names
|
36
|
+
_Note, this solution was removed in v14. If you're intersted in this symlink solution, please create
|
37
|
+
a github issue._
|
38
|
+
|
36
39
|
React on Rails creates symlinks of non-digested versions (original webpack digested file names)
|
37
40
|
to the Rails deployed digested versions when doing a Rails assets compile. The solution is
|
38
41
|
implemented using `assets:precompile` after-hook in
|
39
|
-
file [lib/tasks/assets.rake](
|
42
|
+
file [lib/tasks/assets.rake](lib/tasks/assets.rake)
|
40
43
|
The assets for symlinking are defined by `config.symlink_non_digested_assets_regex` in
|
41
44
|
`config/initializers/react_on_rails.rb`.
|
42
45
|
|
43
|
-
## Disabling the Symlinking
|
44
|
-
To disable symlinks set this parameter to `nil`.
|
45
|
-
|
46
|
-
|
47
46
|
## Example from /spec/dummy
|
48
47
|
|
49
|
-
If you run
|
50
|
-
|
51
48
|
```
|
52
49
|
cd spec/dummy
|
53
50
|
RAILS_ENV=production bundle exec rake assets:precompile
|
File without changes
|
data/docs/tutorial.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# React on Rails Basic Tutorial
|
2
2
|
|
3
|
-
|
3
|
+
*Updated for Ruby 2.7.1, Rails 6.0.3.1, and React on Rails v12.0.0*
|
4
|
+
|
5
|
+
This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
|
4
6
|
|
5
7
|
After finishing this tutorial you will get an application that can do the following (live on Heroku):
|
6
8
|
|
@@ -17,9 +19,9 @@ By the time you read this, the latest may have changed. Be sure to check the ver
|
|
17
19
|
|
18
20
|
_Note: some of the screen images below show the "npm" command. react_on_rails 6.6.0 and greater uses `yarn`._
|
19
21
|
|
20
|
-
## Setting up
|
22
|
+
## Setting up your environment
|
21
23
|
|
22
|
-
Trying out **React on Rails** is super easy, so long as you have the basic prerequisites. This includes the basics for Rails
|
24
|
+
Trying out **React on Rails** is super easy, so long as you have the basic prerequisites. This includes the basics for Rails 6.x and node version 13+. I recommend `rvm` and `nvm` to install Ruby and Node, and [brew](https://brew.sh/) to install [yarn](https://yarnpkg.com/en/docs/install#mac-tab). Rails can be installed as an ordinary gem.
|
23
25
|
|
24
26
|
```
|
25
27
|
nvm install node # download and install latest stable Node
|
@@ -27,9 +29,8 @@ nvm alias default node # make it default version
|
|
27
29
|
nvm list # check
|
28
30
|
|
29
31
|
brew install yarn # you can use other installer if desired
|
30
|
-
|
31
|
-
rvm
|
32
|
-
rvm use 2.5.0 --default # use it and make it default
|
32
|
+
rvm install 2.7 # download and install latest stable Ruby (update to exact version)
|
33
|
+
rvm use 2.7 --default # use it and make it default
|
33
34
|
rvm list # check
|
34
35
|
|
35
36
|
gem install rails # download and install latest stable Rails
|
@@ -44,44 +45,48 @@ First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If
|
|
44
45
|
cd <directory where you want to create your new Rails app>
|
45
46
|
|
46
47
|
# any name you like for the rails app
|
47
|
-
rails new test-react-on-rails --webpack=react
|
48
|
+
rails new test-react-on-rails --webpack=react --skip-sprockets
|
48
49
|
|
49
50
|
cd test-react-on-rails
|
50
51
|
bundle
|
51
52
|
```
|
52
53
|
|
53
|
-
Note: if you are
|
54
|
+
Note: if you are adding React On Rails to an existing app you will instead to run these two commands as well:
|
54
55
|
|
55
56
|
```
|
56
57
|
bundle exec rails webpacker:install
|
57
58
|
bundle exec rails webpacker:install:react
|
58
59
|
```
|
59
60
|
|
61
|
+
Let's commit everything before installing React on Rails.
|
62
|
+
|
63
|
+
```
|
64
|
+
# Here are git commands to make a new git repo and commit everything.
|
65
|
+
# Newer versions of Rails create the git repo by default.
|
66
|
+
git add -A
|
67
|
+
git commit -m "Initial commit"
|
68
|
+
```
|
69
|
+
|
60
70
|
Add the **React On Rails** gem to your `Gemfile`:
|
61
71
|
|
62
72
|
```
|
63
|
-
gem 'react_on_rails', '
|
73
|
+
gem 'react_on_rails', '12.0.0' # prefer exact gem version to match npm version
|
64
74
|
```
|
65
75
|
|
66
76
|
Note: Latest released React On Rails version is considered stable. Please use the latest version to ensure you get all the security patches and the best support.
|
67
77
|
|
68
|
-
Run `bundle` and commit the
|
69
|
-
|
78
|
+
Run `bundle` and commit the changes.
|
70
79
|
|
71
80
|
```
|
72
81
|
bundle
|
73
82
|
|
74
|
-
|
75
|
-
# Newer versions of Rails create the git repo by default.
|
76
|
-
git add -A
|
77
|
-
git commit -m "Initial commit"
|
83
|
+
git commit -am "Added React on Rails Gem"
|
78
84
|
```
|
79
85
|
|
80
86
|
Install React on Rails: `rails generate react_on_rails:install` or `rails generate react_on_rails:install --redux`. You need to first git commit your files before running the generator, or else it will generate an error.
|
81
87
|
|
82
88
|
```
|
83
89
|
rails generate react_on_rails:install
|
84
|
-
bundle && yarn
|
85
90
|
```
|
86
91
|
|
87
92
|
Then run server with static client side files:
|
@@ -96,27 +101,34 @@ foreman start -f Procfile.dev-server
|
|
96
101
|
```
|
97
102
|
|
98
103
|
Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
|
99
|
-
Note, foreman defaults to PORT 5000 unless you set the value of PORT in your environment or in the Procfile.
|
100
104
|
|
101
|
-
|
102
|
-
|
105
|
+
*Note, foreman may default to PORT 5000 unless you set the value of PORT in your environment or in the Procfile.*
|
106
|
+
|
107
|
+
# HMR vs. React Hot Reloading
|
108
|
+
|
109
|
+
First, check that the `hmr` option is `true` in your `config/webpacker.yml` file.
|
110
|
+
|
111
|
+
The basic setup will have HMR working with the default webpacker setup. However, the basic will cause a full page refresh each time you save a file.
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
103
128
|
|
104
|
-
**Don't run `rails new` with the `--webpack=react` option**. Instead, add the webpacker gem to the Gemfile such that it points to master, like this if `11.2.1` is the version you want.
|
105
129
|
|
106
|
-
```ruby
|
107
|
-
gem 'webpacker', github: "rails/webpacker"
|
108
|
-
gem 'react_on_rails', '11.2.1' # always use exact version
|
109
|
-
```
|
110
130
|
|
111
|
-
Then run these commands:
|
112
131
|
|
113
|
-
```sh
|
114
|
-
bundle exec rails webpacker:install
|
115
|
-
yarn add "rails/webpacker" # because the installer has a bug that puts in an invalid version in your package.json.
|
116
|
-
bundle exec rails webpacker:install:react
|
117
|
-
yarn add --dev webpack-dev-server
|
118
|
-
run rails generate react_on_rails:install && bundle && yarn
|
119
|
-
```
|
120
132
|
|
121
133
|
### Custom IP & PORT setup (Cloud9 example)
|
122
134
|
|
@@ -265,6 +277,16 @@ You can turn on server rendering by simply changing the `prerender` option to `t
|
|
265
277
|
<%= react_component("HelloWorld", props: @hello_world_props, prerender: true) %>
|
266
278
|
```
|
267
279
|
|
280
|
+
If you want to test this out with HMR, then you also need to add this line to your
|
281
|
+
`config/intializers/react_on_rails.rb`
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
config.same_bundle_for_client_and_server = true
|
285
|
+
```
|
286
|
+
|
287
|
+
More likely, you will create a different build file for server rendering. However, if you want to
|
288
|
+
use the same file from the webpack-dev-server, you'll need to add that line.
|
289
|
+
|
268
290
|
Then push to Heroku:
|
269
291
|
|
270
292
|
```
|
@@ -318,8 +340,6 @@ So you get some basics from HMR with no code changes. If you want to go further,
|
|
318
340
|
* https://github.com/rails/webpacker/blob/master/docs/webpack-dev-server.md
|
319
341
|
* https://webpack.js.org/configuration/dev-server/
|
320
342
|
* https://webpack.js.org/concepts/hot-module-replacement/
|
321
|
-
* https://gaearon.github.io/react-hot-loader/getstarted/
|
322
|
-
* https://github.com/gaearon/react-hot-loader
|
323
343
|
|
324
344
|
React on Rails will automatically handle disabling server rendering if there is only one bundle file created by the Webpack development server by rails/webpacker.
|
325
345
|
|