react_on_rails 11.1.4 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +320 -0
- data/.eslintignore +2 -1
- data/.eslintrc +30 -2
- data/.github/FUNDING.yml +1 -0
- data/.gitignore +3 -1
- data/.prettierignore +10 -0
- data/.prettierrc +23 -0
- data/.release-it.json +3 -0
- data/.rubocop.yml +37 -11
- data/.travis.yml +10 -20
- data/CHANGELOG.md +126 -4
- data/CONTRIBUTING.md +61 -80
- data/Gemfile +3 -5
- data/KUDOS.md +4 -1
- data/REACT-ON-RAILS-PRO-LICENSE +95 -0
- data/README.md +148 -89
- data/Rakefile +0 -7
- data/SUMMARY.md +12 -12
- data/book.json +5 -5
- data/docs/additional-reading/asset-pipeline.md +8 -16
- data/docs/additional-reading/images.md +1 -1
- data/docs/additional-reading/rails_view_rendering_from_inline_javascript.md +2 -1
- 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/additional-reading/upgrade-webpacker-v3-to-v4.md +10 -0
- data/docs/api/javascript-api.md +3 -3
- data/docs/api/redux-store-api.md +4 -2
- data/docs/api/view-helpers-api.md +17 -14
- data/docs/basics/client-vs-server-rendering.md +8 -2
- data/docs/basics/configuration.md +87 -73
- data/docs/basics/deployment.md +2 -4
- data/docs/basics/generator-details.md +1 -1
- data/docs/basics/heroku-deployment.md +24 -0
- data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +49 -0
- data/docs/basics/i18n.md +45 -23
- data/docs/basics/installation-into-an-existing-rails-app.md +4 -9
- data/docs/basics/minitest-configuration.md +31 -0
- data/docs/basics/react-server-rendering.md +3 -1
- data/docs/basics/recommended-project-structure.md +21 -15
- data/docs/basics/{generator-functions-and-railscontext.md → render-functions-and-railscontext.md} +59 -21
- data/docs/basics/rspec-configuration.md +29 -18
- data/docs/basics/upgrading-react-on-rails.md +74 -3
- data/docs/basics/webpack-configuration.md +29 -8
- 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 +13 -9
- data/docs/{basics → outdated}/how-react-on-rails-works.md +8 -4
- 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 +2 -12
- data/docs/testimonials/hvmn.md +25 -0
- data/docs/testimonials/resortpass.md +13 -0
- data/docs/testimonials/testimonials.md +28 -0
- data/docs/tutorial.md +182 -44
- data/jest.config.js +4 -0
- data/lib/generators/react_on_rails/base_generator.rb +2 -2
- data/lib/generators/react_on_rails/dev_tests_generator.rb +2 -1
- data/lib/generators/react_on_rails/generator_helper.rb +4 -6
- data/lib/generators/react_on_rails/install_generator.rb +2 -0
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +3 -1
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +26 -0
- data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +20 -40
- data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +4 -1
- 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/configuration.rb +60 -29
- data/lib/react_on_rails/error.rb +2 -0
- data/lib/react_on_rails/git_utils.rb +2 -0
- data/lib/react_on_rails/helper.rb +110 -159
- data/lib/react_on_rails/json_output.rb +1 -1
- data/lib/react_on_rails/json_parse_error.rb +2 -0
- data/lib/react_on_rails/locales/base.rb +150 -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/react_component/render_options.rb +4 -0
- data/lib/react_on_rails/server_rendering_js_code.rb +42 -0
- data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +85 -60
- data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +7 -8
- data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
- data/lib/react_on_rails/utils.rb +19 -20
- data/lib/react_on_rails/version.rb +1 -1
- data/lib/react_on_rails/version_checker.rb +5 -1
- data/lib/react_on_rails/webpacker_utils.rb +27 -2
- data/lib/react_on_rails.rb +4 -1
- data/lib/tasks/assets.rake +33 -46
- data/lib/tasks/locale.rake +4 -2
- data/package-scripts.yml +49 -0
- data/package.json +41 -31
- 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/release.rake +3 -8
- data/rakelib/run_rspec.rake +5 -18
- data/react_on_rails.gemspec +4 -5
- data/tsconfig.json +14 -0
- data/webpackConfigLoader.js +5 -4
- data/yarn.lock +7177 -2311
- metadata +54 -55
- data/Gemfile.rails32 +0 -74
- data/docs/additional-reading/babel.md +0 -5
- data/docs/additional-reading/heroku-deployment.md +0 -92
- data/docs/additional-reading/hot-reloading-rails-development.md +0 -57
- data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
- data/docs/testimonials.md +0 -11
- data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-server +0 -12
- data/lib/react_on_rails/assets_precompile.rb +0 -150
- data/lib/react_on_rails/locales_to_js.rb +0 -134
- /data/docs/{misc → outdated}/rails3.md +0 -0
data/README.md
CHANGED
|
@@ -1,69 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://shakacode.com/"><img src="https://user-images.githubusercontent.com/10421828/79436256-517d0500-7fd9-11ea-9300-dfbc7c293f26.png"></a>
|
|
5
|
+
<a href="https://forum.shakacode.com/"><img src="https://user-images.githubusercontent.com/10421828/79436266-53df5f00-7fd9-11ea-94b3-b985e1b05bdc.png"></a>
|
|
6
|
+
<a href="https://forum.shakacode.com/t/react-on-rails-pro-2020-shave-the-yak/1842"><img src="https://user-images.githubusercontent.com/10421828/79436265-53df5f00-7fd9-11ea-8220-fc474f6a856c.png"></a>
|
|
7
|
+
<a href="https://github.com/sponsors/shakacode"><img src="https://user-images.githubusercontent.com/10421828/79466109-cdd90d80-8004-11ea-88e5-25f9a9ddcf44.png"></a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
[](./LICENSE.md) [](https://travis-ci.org/shakacode/react_on_rails) [](https://badge.fury.io/rb/react_on_rails) [](https://badge.fury.io/js/react-on-rails) [](https://codeclimate.com/github/shakacode/react_on_rails) [](https://coveralls.io/github/shakacode/react_on_rails?branch=master) [](https://rubygems.org/gems/react_on_rails)
|
|
13
|
+
|
|
14
|
+
*These are the docs for React on Rails 12, coming soon. To see the version 11 docs, [click here](https://github.com/shakacode/react_on_rails/tree/11.3.0).*
|
|
15
|
+
|
|
16
|
+
#### News
|
|
17
|
+
**June 24, 2020**:
|
|
18
|
+
1. Gem version: 12.0.0.pre.beta.3 and npm version 12.0.0-beta.3 released! Please try this out!
|
|
19
|
+
Major enhancements:
|
|
20
|
+
1. Proper support for React Hooks
|
|
21
|
+
2. Typescript bindings
|
|
22
|
+
3. i18n support for generating a JSON file rather than a JS file.
|
|
23
|
+
4. Tighter rails/webpacker integration
|
|
24
|
+
|
|
25
|
+
Be sure to see the [CHANGELOG.md](./CHANGELOG.md) and read the upgrade instructions:
|
|
26
|
+
[docs/basics/upgrading-react-on-rails](./docs/basics/upgrading-react-on-rails.md#upgrading-to-v12).
|
|
27
|
+
|
|
28
|
+
2. See Justin's RailsConf talk: [Webpacker, It-Just-Works, But How?](http://railsconf.com/2020/video/justin-gordon-webpacker-it-just-works-but-how).
|
|
29
|
+
|
|
30
|
+
**April 2, 2020**:
|
|
31
|
+
* Are you interested in support for React on Rails? If so check out [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
|
32
|
+
* HMR is working with [Loadable Components](https://loadable-components.com) for a both amazing hot-reloading developer experience and great runtime performance. Please [email me](mailto:justin@shakacode.com) if you'd like to use [Loadable Components Code Splitting](https://loadable-components.com/docs/code-splitting/) to speed up your app by reducing your bundle sizes and lazily loading the code that's needed.
|
|
33
|
+
* `react_on_rails` fully supports `rails/webpacker`. The example test app in `spec/dummy` was recently converted over to use rails/webpacker v4. It's a good example of how to leverage rails/webpacker's webpack configuration.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
#### About
|
|
38
|
+
React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
|
|
2
39
|
|
|
3
|
-
|
|
40
|
+
This project is sponsored by the software consulting firm [ShakaCode](https://www.shakacode.com). We focus on React front-ends, often with Ruby on Rails or Gatsby. The best way to see what we do is to see the details of [our recent work](https://www.shakacode.com/recent-work).
|
|
4
41
|
|
|
5
|
-
React on Rails
|
|
42
|
+
Interested in optimizing your webpack setup for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable with server side rendering? We just did this for Popmenu, [lowering Heroku costs 20-25% while getting a 73% decrease in average response times](https://www.shakacode.com/recent-work/popmenu/).
|
|
6
43
|
|
|
7
|
-
|
|
44
|
+
Feel free to contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakacode.com), maintainer of React on Rails, for more information.
|
|
8
45
|
|
|
9
|
-
|
|
46
|
+
[Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
10
47
|
|
|
11
48
|
# Intro
|
|
12
49
|
|
|
13
50
|
## Project Objective
|
|
14
51
|
|
|
15
|
-
To provide
|
|
52
|
+
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 Rendering.
|
|
16
53
|
|
|
17
54
|
## Features and Why React on Rails?
|
|
18
55
|
|
|
19
|
-
Given that rails/webpacker gem already provides basic React integration, why would you use "React on Rails"?
|
|
56
|
+
Given that rails/webpacker gem already provides basic React integration, why would you use "React on Rails"?
|
|
20
57
|
|
|
21
|
-
1. The 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
|
|
22
|
-
1.
|
|
58
|
+
1. The 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.
|
|
59
|
+
1. Tight integration with [rails/webpacker](https://github.com/rails/webpacker).
|
|
60
|
+
1. Server rendering, often used for SEO crawler indexing and UX performance, is not offered by rails/webpacker.
|
|
61
|
+
1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/reactjs/react-router) integration with server-side-rendering.
|
|
23
62
|
1. [Internationalization (I18n) and (localization)](https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md)
|
|
24
|
-
1. [RSpec Test Helpers Configuration](docs/basics/rspec-configuration.md) to ensure your Webpack bundles are ready for tests.
|
|
25
63
|
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/).
|
|
26
|
-
1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example)
|
|
64
|
+
1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example).
|
|
27
65
|
|
|
28
66
|
See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
|
|
29
67
|
|
|
30
|
-
##
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-

|
|
35
|
-
|
|
36
|
-
* [HVMN’s 90% Reduction in Server Response Time from React on Rails Pro](https://medium.com/@railsonmaui/hvmns-90-reduction-in-server-response-time-from-react-on-rails-pro-eb08226687db)
|
|
37
|
-
* [Egghead React on Rails Pro Deployment Highlights](https://github.com/shakacode/react_on_rails/wiki/Egghead-React-on-Rails-Pro-Deployment-Highlights)
|
|
38
|
-
|
|
39
|
-
For more information, see the [React on Rails Pro Docs](https://github.com/shakacode/react_on_rails/wiki).
|
|
40
|
-
|
|
41
|
-
The [ShakaCode Pro Support Plan](http://www.shakacode.com/work/shakacode-pro-support.pdf) can help you with:
|
|
42
|
-
|
|
43
|
-
* Optimizing your webpack setup to Webpack v4 for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable.
|
|
44
|
-
* Upgrading your app to use the current Webpack setup that skips the Sprockets asset pipeline.
|
|
45
|
-
* Better performance client and server side.
|
|
46
|
-
* Efficiently migrating from [Angular to React](https://www.shakacode.com/services/angular-to-react/).
|
|
47
|
-
* Best practices based on over four years of React on Rails experience.
|
|
48
|
-
* Using [Reason](https://reasonml.github.io/) with (or without) React on Rails.
|
|
49
|
-
|
|
50
|
-
ShakaCode can also help you with your custom software development needs. We specialize in marketplace and e-commerce applications that utilize both Rails and React. Because we own [HawaiiChee.com](https://www.hawaiichee.com), we can leverage that code for your app!
|
|
51
|
-
|
|
52
|
-
The article [Why Hire ShakaCode?](https://blog.shakacode.com/can-shakacode-help-you-4a5b1e5a8a63#.jex6tg9w9) provides additional details about our projects.
|
|
53
|
-
|
|
54
|
-
Please [email me (Justin Gordon), the creator of React on Rails](mailto:justin@shakacode.com), to see if I can help you or if you want an invite to our private Slack room for ShakaCode.
|
|
55
|
-
|
|
56
|
-
## Testimonials for Hiring ShakaCode and our "Pro Support"
|
|
57
|
-
|
|
58
|
-
From Kyle Maune of Cooper Aerial, May 4, 2018
|
|
59
|
-
|
|
60
|
-

|
|
61
|
-
|
|
62
|
-
From Joel Hooks, Co-Founder, Chief Nerd at [egghead.io](https://egghead.io/), January 30, 2017:
|
|
63
|
-
|
|
64
|
-

|
|
65
|
-
|
|
66
|
-
For more testimonials, see [Live Projects](PROJECTS.md) and [Kudos](./KUDOS.md).
|
|
68
|
+
## ShakaCode Forum Premium Content
|
|
69
|
+
_Requires creating a free account._
|
|
70
|
+
|
|
71
|
+
* [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)
|
|
67
72
|
|
|
68
73
|
----
|
|
69
74
|
|
|
@@ -73,7 +78,7 @@ For more testimonials, see [Live Projects](PROJECTS.md) and [Kudos](./KUDOS.md).
|
|
|
73
78
|
|
|
74
79
|
## Prerequisites
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
Ruby on Rails >=5 and rails/webpacker 4.2+.
|
|
77
82
|
|
|
78
83
|
## Getting Started
|
|
79
84
|
|
|
@@ -82,29 +87,16 @@ Note, the best way to understand how to use ReactOnRails is to study a few simpl
|
|
|
82
87
|
1. Do the quick [tutorial](docs/tutorial.md).
|
|
83
88
|
2. Add React on Rails to an existing Rails app per [the instructions](docs/basics/installation-into-an-existing-rails-app.md).
|
|
84
89
|
3. Look at [spec/dummy](spec/dummy), a simple, no DB example.
|
|
85
|
-
3. Look at [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial); it's a full
|
|
90
|
+
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://www.reactrails.com).
|
|
86
91
|
|
|
87
92
|
## Basic Installation
|
|
88
93
|
|
|
89
94
|
*See also [the instructions for installing into an existing Rails app](docs/basics/installation-into-an-existing-rails-app.md).*
|
|
90
95
|
|
|
91
|
-
1. Create a new Rails app:
|
|
92
|
-
|
|
93
|
-
``````bash
|
|
94
|
-
$ rails new my-app --webpack=react
|
|
95
|
-
$ cd my-app
|
|
96
|
-
``````
|
|
97
|
-
|
|
98
96
|
2. Add the `react_on_rails` gem to Gemfile:
|
|
99
97
|
|
|
100
|
-
```ruby
|
|
101
|
-
gem 'react_on_rails', '11.1.4' # Use the exact gem version to match npm version
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
3. Install the `react_on_rails` gem:
|
|
105
|
-
|
|
106
98
|
```bash
|
|
107
|
-
|
|
99
|
+
bundle add react_on_rails --strict
|
|
108
100
|
```
|
|
109
101
|
|
|
110
102
|
4. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
|
|
@@ -112,13 +104,13 @@ Note, the best way to understand how to use ReactOnRails is to study a few simpl
|
|
|
112
104
|
5. Run the generator:
|
|
113
105
|
|
|
114
106
|
```bash
|
|
115
|
-
|
|
107
|
+
rails generate react_on_rails:install
|
|
116
108
|
```
|
|
117
109
|
|
|
118
110
|
6. Start the app:
|
|
119
111
|
|
|
120
112
|
```bash
|
|
121
|
-
|
|
113
|
+
rails s
|
|
122
114
|
```
|
|
123
115
|
|
|
124
116
|
7. Visit http://localhost:3000/hello_world.
|
|
@@ -137,13 +129,18 @@ Below is the line where you turn server rendering on by setting `prerender` to t
|
|
|
137
129
|
<%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
|
|
138
130
|
```
|
|
139
131
|
|
|
132
|
+
Note, if you got an error in your console regarding "ReferenceError: window is not defined",
|
|
133
|
+
then you need to edit `config/webpacker.yml` and set `hmr: false` and `inline: false`.
|
|
134
|
+
See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
|
|
135
|
+
issue.
|
|
136
|
+
|
|
140
137
|
## Basic Usage
|
|
141
138
|
|
|
142
139
|
### Configuration
|
|
143
140
|
|
|
144
141
|
* Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](docs/basics/configuration.md) for documentation of all configuration options.
|
|
145
|
-
* 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](./spec/dummy/config/webpacker.yml) example
|
|
146
|
-
*
|
|
142
|
+
* 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](./spec/dummy/config/webpacker.yml) example or the official default [webpacker.yml](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml).
|
|
143
|
+
* Tip: set `compile: false` for development if you know that you'll always be compiling with a watch process. Otherwise, every request will check if compilation is needed.
|
|
147
144
|
* Your `public_output_path` must match your Webpack configuration for `output` of your bundles.
|
|
148
145
|
* Only set `cache_manifest` to `true` in your production env.
|
|
149
146
|
|
|
@@ -178,13 +175,14 @@ Below is the line where you turn server rendering on by setting `prerender` to t
|
|
|
178
175
|
<%= react_component("HelloWorld", props: { name: "Stranger" }) %>
|
|
179
176
|
```
|
|
180
177
|
|
|
181
|
-
- 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 [
|
|
178
|
+
- 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](docs/basics/render-functions-and-railscontext.md) for more details on this topic.
|
|
182
179
|
|
|
183
180
|
```js
|
|
184
181
|
import React from 'react';
|
|
185
182
|
|
|
186
183
|
export default (props, railsContext) => {
|
|
187
|
-
|
|
184
|
+
// Note wrap in a function to make this a React function component
|
|
185
|
+
return () => (
|
|
188
186
|
<div>
|
|
189
187
|
Your locale is {railsContext.i18nLocale}.<br/>
|
|
190
188
|
Hello, {props.name}!
|
|
@@ -197,7 +195,7 @@ See the [View Helpers API](./docs/api/view-helpers-api.md) for more details on `
|
|
|
197
195
|
|
|
198
196
|
## Fragment Caching
|
|
199
197
|
|
|
200
|
-
Fragment caching is a [React on Rails Pro](https://
|
|
198
|
+
Fragment caching is a [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro) feature. Fragment caching is a **HUGE** performance booster for your apps. Use the `cached_react_component` and `cached_react_component_hash`. The API is the same as `react_component` and `react_component_hash`, but for 2 differences:
|
|
201
199
|
|
|
202
200
|
1. The `cache_key` takes the same parameters as any Rails `cache` view helper.
|
|
203
201
|
1. The **props** are passed via a block so that evaluation of the props is not done unless the cache is broken. Suppose you put your props calculation into some method called `some_slow_method_that_returns_props`:
|
|
@@ -219,7 +217,7 @@ Note, even without server rendering (without step 3 above), fragment caching is
|
|
|
219
217
|
|
|
220
218
|
## Integration with Node.js for Server Rendering
|
|
221
219
|
|
|
222
|
-
Default server rendering is done by ExecJS. If you want to use a Node.js server for better performing server rendering, [email justin@shakacode.com](mailto:justin@shakacode.com). ShakaCode has built a premium Node rendering server that is part of [React on Rails Pro](https://
|
|
220
|
+
Default server rendering is done by ExecJS. If you want to use a Node.js server for better performing server rendering, [email justin@shakacode.com](mailto:justin@shakacode.com). ShakaCode has built a premium Node rendering server that is part of [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
|
223
221
|
|
|
224
222
|
## Globally Exposing Your React Components
|
|
225
223
|
|
|
@@ -238,25 +236,45 @@ This is how to expose a component to the `react_component` view helper.
|
|
|
238
236
|
|
|
239
237
|
#### Different Server-Side Rendering Code (and a Server-Specific Bundle)
|
|
240
238
|
|
|
241
|
-
You may want different
|
|
239
|
+
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() }`.
|
|
240
|
+
|
|
241
|
+
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.
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
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._)
|
|
244
244
|
|
|
245
|
-
## Specifying Your React Components:
|
|
245
|
+
## Specifying Your React Components: Register directly or use render-functions
|
|
246
246
|
|
|
247
|
-
You have two ways to specify your React components. You can either register the React component directly, or you can create a function that returns a React component
|
|
247
|
+
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:
|
|
248
248
|
|
|
249
|
-
1. You have access to the `railsContext`. See documentation for the railsContext in terms of why you might need it. You **need** a
|
|
249
|
+
1. You have access to the `railsContext`. See documentation for the railsContext in terms of why you might need it. You **need** a render function to access the `railsContext`.
|
|
250
250
|
2. You can use the passed-in props to initialize a redux store or set up react-router.
|
|
251
251
|
3. You can return different components depending on what's in the props.
|
|
252
252
|
|
|
253
|
-
|
|
253
|
+
Note, the return value of a **render function** should be either a React Function or Class Component, or an object representing server rendering results.
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
Do not return a React Element (JSX).
|
|
256
256
|
|
|
257
|
-
|
|
257
|
+
ReactOnRails will automatically detect a registered render function by the fact that the function takes
|
|
258
|
+
more than 1 parameter. In other words, if you want the ability to provide a function that returns the
|
|
259
|
+
React component, then you need to specify at least a second parameter. This is the `railsContext`.
|
|
260
|
+
If you're not using this parameter, declare your function with the unused param:
|
|
258
261
|
|
|
259
|
-
|
|
262
|
+
```js
|
|
263
|
+
const MyComponentGenerator = (props, _railsContext) => {
|
|
264
|
+
if (props.print) {
|
|
265
|
+
// This is a React FunctionComponent because it is wrapped in a function.
|
|
266
|
+
return () => <H1>{JSON.stringify(props)}</H1>;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Thus, there is no difference between registering a React function or class Component versus a "render function."
|
|
272
|
+
|
|
273
|
+
## react_component_hash for render functions
|
|
274
|
+
|
|
275
|
+
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`.
|
|
276
|
+
|
|
277
|
+
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:
|
|
260
278
|
|
|
261
279
|
```js
|
|
262
280
|
{ renderedHtml: { componentHtml, customKey1, customKey2} }
|
|
@@ -271,7 +289,8 @@ For details on using react_component_hash with react-helmet, see the docs below
|
|
|
271
289
|
|
|
272
290
|
## I18n
|
|
273
291
|
|
|
274
|
-
|
|
292
|
+
React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.json` or `*.js*.
|
|
293
|
+
See the [How to add I18n](docs/basics/i18n.md) for a summary of adding I18n.
|
|
275
294
|
|
|
276
295
|
## More Details
|
|
277
296
|
|
|
@@ -279,18 +298,16 @@ Browse the links in the [Summary Table of Contents](./SUMMARY.md)
|
|
|
279
298
|
|
|
280
299
|
Here are some highly recommended next articles to read:
|
|
281
300
|
|
|
282
|
-
1. [How React on Rails Works](
|
|
301
|
+
1. [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
|
|
283
302
|
1. [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
|
|
284
303
|
1. [Webpack Configuration](./docs/basics/webpack-configuration.md)
|
|
285
|
-
1. [View Helpers API](./docs/view-helpers-api.md)
|
|
286
|
-
1. [Caching and Performance: React on Rails Pro](https://
|
|
304
|
+
1. [View Helpers API](./docs/api/view-helpers-api.md)
|
|
305
|
+
1. [Caching and Performance: React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
|
|
287
306
|
1. [Deployment](docs/basics/deployment.md).
|
|
288
307
|
|
|
289
308
|
# Support
|
|
290
309
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
Aloha, I'm Justin Gordon the creator and maintainer of React on Rails. I'm supporting my continued dedication to this project by project by offering a React on Rails **Pro Support Plan**. Please [email me](mailto:justin@shakacode.com) to see if I can help you.
|
|
310
|
+
[Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
294
311
|
|
|
295
312
|
## Community Resources
|
|
296
313
|
|
|
@@ -309,17 +326,59 @@ Please [**click to subscribe**](https://app.mailerlite.com/webforms/landing/l1d9
|
|
|
309
326
|
|
|
310
327
|
Bug reports and pull requests are welcome. See [Contributing](CONTRIBUTING.md) to get started, and the [list of help wanted issues](https://github.com/shakacode/react_on_rails/labels/contributions%3A%20up%20for%20grabs%21).
|
|
311
328
|
|
|
329
|
+
## React on Rails Pro
|
|
330
|
+
|
|
331
|
+
React on Rails Pro provides Node server rendering, fragment caching, code-splitting, and other performance enhancements for React on Rails. For a case study, see the article [HVMN’s 90% Reduction in Server Response Time from React on Rails Pro](https://blog.shakacode.com/hvmns-90-reduction-in-server-response-time-from-react-on-rails-pro-eb08226687db). The [Wiki](https://github.com/shakacode/react_on_rails/wiki) contains more details.
|
|
332
|
+
|
|
333
|
+
[](https://blog.shakacode.com/hvmns-90-reduction-in-server-response-time-from-react-on-rails-pro-eb08226687db)
|
|
334
|
+
|
|
335
|
+
## React on Rails Pro Support
|
|
336
|
+
|
|
337
|
+
The [React on Rails Pro Support Plan](https://www.shakacode.com/react-on-rails-pro) can help!
|
|
338
|
+
|
|
339
|
+
* Optimizing your webpack setup to Webpack v4 for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable.
|
|
340
|
+
* Upgrading your app to use the current Webpack setup that skips the Sprockets asset pipeline.
|
|
341
|
+
* Better performance client and server side.
|
|
342
|
+
* Best practices based on over four years of React on Rails experience.
|
|
343
|
+
* Using [Reason](https://reasonml.github.io/) with (or without) React on Rails.
|
|
344
|
+
|
|
345
|
+
ShakaCode can also help you with your custom software development needs. We specialize in marketplace and e-commerce applications that utilize both Rails and React. Because we own [HawaiiChee.com](https://www.hawaiichee.com), we can leverage that code for your app!
|
|
346
|
+
|
|
347
|
+
Please email Justin Gordon [justin@shakacode.com](mailto:justin@shakacode.com), the maintainer of React on Rails, for more information.
|
|
348
|
+
|
|
349
|
+
## Testimonials for ShakaCode
|
|
350
|
+
[HVMN Testimonial, by Paul Benigeri, October 12, 2018](./docs/testimonials/hvmn.md)
|
|
351
|
+
> The price we paid for the consultation + the React on Rails pro license has already been made back a couple of times from hosting fees alone. The entire process was super hands off, and our core team was able to focus on shipping new feature during that sprint.
|
|
352
|
+
|
|
353
|
+
[ResortPass Testimonial, by Leora Juster, December 10, 2018](./docs/testimonials/resortpass.md)
|
|
354
|
+
|
|
355
|
+
> Justin and his team were instrumental in assisting us in setting design foundations and standards for our transition to a react on rails application. Just three months of work with the team at Shaka code and we have a main page of our application server-side rendering at exponentially improved speeds.
|
|
356
|
+
|
|
357
|
+
From Joel Hooks, Co-Founder, Chief Nerd at [egghead.io](https://egghead.io/), January 30, 2017:
|
|
358
|
+
|
|
359
|
+

|
|
360
|
+
|
|
361
|
+
For more testimonials, see [Live Projects](PROJECTS.md) and [Kudos](./KUDOS.md).
|
|
362
|
+
|
|
312
363
|
# Supporters
|
|
313
364
|
|
|
314
|
-
The following companies support this open source project, and ShakaCode uses their products! Justin writes React on Rails on [RubyMine](https://www.jetbrains.com/ruby/). We use [Scout](https://scoutapp.com/) to monitor the live performance of [HawaiiChee.com](https://www.hawaiichee.com),
|
|
365
|
+
The following companies support this open source project, and ShakaCode uses their products! Justin writes React on Rails on [RubyMine](https://www.jetbrains.com/ruby/). We use [Scout](https://scoutapp.com/) to monitor the live performance of [HawaiiChee.com](https://www.hawaiichee.com), [BrowserStack](https://www.browserstack.com) to solve problems with oddball browsers, and [CodersRank](https://codersrank.io/) to find candidates for our team.
|
|
315
366
|
|
|
367
|
+
[](https://www.jetbrains.com/ruby/)
|
|
316
368
|
[](https://scoutapp.com/)
|
|
317
369
|
[](https://www.browserstack.com)
|
|
370
|
+
[](https://codersrank.io/?utm_source=github&utm_medium=banner&utm_campaign=shakacode)
|
|
371
|
+
|
|
372
|
+
## Clubhouse
|
|
373
|
+
I've just moved ShakaCode's development to [ClubHouse](https://clubhouse.io/) from Trello. We're going to be doing this with all our projects. If you want to **try ClubHouse and get 2 months free beyond the 14-day trial period**, click [here to use ShakaCode's referral code](http://r.clbh.se/mvfoNeH). We're participating in their awesome triple-sided referral program, which you can read about [here](https://clubhouse.io/blog/clubhouse-referral-program-5f614bb437c3). By using our [referral code](http://r.clbh.se/mvfoNeH) you'll be supporting ShakaCode and, thus, React on Rails!
|
|
318
374
|
|
|
319
375
|
*If you'd like to support React on Rails and have your company listed here, [get in touch](mailto:justin@shakacode.com).*
|
|
320
376
|
|
|
321
377
|
Aloha and best wishes from Justin and the ShakaCode team!
|
|
322
378
|
|
|
379
|
+
# Work with Us
|
|
380
|
+
ShakaCode is **[currently looking to hire](http://www.shakacode.com/about/#work-with-us)** like-minded, remote-first, developers that wish to work on our projects, including [Hawaii Chee](https://www.hawaiichee.com). Your main coding interview will be pairing with us on our open source! We're also using [ReasonML](https://reasonml.github.io/) extensively!
|
|
381
|
+
|
|
323
382
|
# License
|
|
324
383
|
|
|
325
384
|
The gem is available as open source under the terms of the [MIT License](LICENSE.md).
|
data/Rakefile
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
# Rake will automatically load any *.rake files inside of the "rakelib" folder
|
|
4
4
|
# See rakelib/
|
|
5
5
|
|
|
6
|
-
require_relative "./spec/react_on_rails/support/rails32_helper"
|
|
7
|
-
|
|
8
6
|
tasks = %w[run_rspec lint]
|
|
9
7
|
prepare_for_ci = %w[node_package dummy_apps]
|
|
10
8
|
|
|
@@ -14,11 +12,6 @@ if ENV["USE_COVERALLS"] == "TRUE"
|
|
|
14
12
|
tasks << "coveralls:push"
|
|
15
13
|
end
|
|
16
14
|
|
|
17
|
-
if using_rails32?
|
|
18
|
-
tasks = %w[run_rspec:gem_rails32 run_rspec:dummy_no_webpacker]
|
|
19
|
-
prepare_for_ci = %w[node_package dummy_apps:dummy_no_webpacker]
|
|
20
|
-
end
|
|
21
|
-
|
|
22
15
|
desc "Run all tests and linting"
|
|
23
16
|
task default: tasks
|
|
24
17
|
|
data/SUMMARY.md
CHANGED
|
@@ -5,13 +5,16 @@
|
|
|
5
5
|
## **Basics**
|
|
6
6
|
+ [React on Rails Basic Installation Tutorial](./docs/tutorial.md)
|
|
7
7
|
+ [Webpack Configuration](./docs/basics/webpack-configuration.md)
|
|
8
|
-
+ [How React on Rails Works](
|
|
8
|
+
+ [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
|
|
9
|
+
+ [Client vs. Server Rendering](./docs/basics/client-vs-server-rendering.md)
|
|
10
|
+
+ [React Server Rendering](./docs/basics/react-server-rendering.md)
|
|
9
11
|
+ [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
|
|
10
|
-
+ [
|
|
12
|
+
+ [Render-Functions and the RailsContext](docs/basics/render-functions-and-railscontext.md)
|
|
11
13
|
+ [Caching and Performance: React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
|
|
12
14
|
+ [Deployment](docs/basics/deployment.md).
|
|
13
15
|
+ [React on Rails Internationalization (I18n, localization)](docs/basics/i18n.md)
|
|
14
16
|
+ [RSpec Test Helpers Configuration](docs/basics/rspec-configuration.md)
|
|
17
|
+
+ [Minitest Configuration](docs/basics/minitest-configuration.md)
|
|
15
18
|
+ [Upgrading React on Rails](docs/basics/upgrading-react-on-rails.md)
|
|
16
19
|
|
|
17
20
|
## **API**
|
|
@@ -23,34 +26,31 @@
|
|
|
23
26
|
+ [Migration from react-rails](./docs/basics/migrating-from-react-rails.md)
|
|
24
27
|
+ [Generator Details](docs/basics/generator-details.md)
|
|
25
28
|
+ [Updating Dependencies](./docs/additional-reading/updating-dependencies.md)
|
|
26
|
-
+ [Manual Installation Overview](docs/
|
|
29
|
+
+ [Manual Installation Overview](docs/outdated/manual-installation-overview.md)
|
|
30
|
+
+ [Upgrading from rails/webpacker v3 to v4](docs/additional-reading/upgrade-webpacker-v3-to-v4.md)
|
|
27
31
|
|
|
28
32
|
## **Rails**
|
|
29
33
|
+ [Rails Engine Integration](./docs/additional-reading/rails-engine-integration.md)
|
|
30
34
|
+ [Rails View Rendering from Inline JavaScript](./docs/additional-reading/rails_view_rendering_from_inline_javascript.md)
|
|
31
35
|
+ [Turbolinks](./docs/additional-reading/turbolinks.md)
|
|
32
|
-
+ [Rails Assets](docs/misc-pending/rails-assets.md)
|
|
33
36
|
+ [Converting a Rails 5 API only app to a Rails app](./docs/additional-reading/convert-rails-5-api-only-app.md)
|
|
34
37
|
|
|
35
38
|
## **Javascript**
|
|
36
39
|
+ [Node Dependencies, NPM, and Yarn](./docs/additional-reading/node-dependencies-and-npm.md)
|
|
37
|
-
+ [Babel](./docs/additional-reading/babel.md)
|
|
38
40
|
+ [React Router](./docs/additional-reading/react-router.md)
|
|
39
41
|
+ [React & Redux](./docs/additional-reading/react-and-redux.md)
|
|
40
|
-
+ [Webpack Tips](./docs/additional-reading/webpack
|
|
42
|
+
+ [Webpack Tips](./docs/additional-reading/webpack.md)
|
|
41
43
|
+ [Server Rendering Tips](./docs/additional-reading/server-rendering-tips.md)
|
|
42
|
-
+ [Code Splitting](docs/
|
|
44
|
+
+ [Code Splitting](docs/outdated/code-splitting.md)
|
|
43
45
|
+ [AngularJS Integration and Migration to React on Rails](./docs/additional-reading/angular-js-integration-migration.md)
|
|
44
46
|
|
|
45
47
|
## **Deployment**
|
|
46
|
-
+ [Heroku Deployment](
|
|
48
|
+
+ [Heroku Deployment](docs/basics/heroku-deployment.md)
|
|
47
49
|
+ [Elastic Beanstalk Deployment](./docs/additional-reading/elastic-beanstalk.md)
|
|
48
50
|
|
|
49
|
-
##
|
|
50
|
-
+ [Setting up Hot Reloading during Rails Development, API docs](./docs/api/ruby-api-hot-reload-view-helpers.md)
|
|
51
|
+
## Outdated Non-Webpack Docs
|
|
51
52
|
+ [Developing with the Webpack Dev Server](./docs/additional-reading/webpack-dev-server.md)
|
|
52
|
-
+ [Webpack, the Asset Pipeline, and Using Assets w/ React](./docs/
|
|
53
|
-
+ [Hot Reloading of Assets For Rails Development](./docs/additional-reading/hot-reloading-rails-development.md)
|
|
53
|
+
+ [Webpack, the Asset Pipeline, and Using Assets w/ React](./docs/outdated/rails-assets-relative-paths.md)
|
|
54
54
|
|
|
55
55
|
## **[CONTRIBUTING](CONTRIBUTING.md)**
|
|
56
56
|
+ [Generator Testing](./docs/contributor-info/generator-testing.md)
|
data/book.json
CHANGED
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
"url": "https://github.com/shakacode/react_on_rails/"
|
|
8
8
|
},
|
|
9
9
|
"sharing": {
|
|
10
|
-
"facebook":
|
|
11
|
-
"twitter":
|
|
12
|
-
"google":
|
|
13
|
-
"weibo":
|
|
10
|
+
"facebook": true,
|
|
11
|
+
"twitter": true,
|
|
12
|
+
"google": true,
|
|
13
|
+
"weibo": true,
|
|
14
14
|
"instapaper": true,
|
|
15
|
-
"vk":
|
|
15
|
+
"vk": true
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
# Asset Pipeline
|
|
1
|
+
# Asset Pipeline with React on Rails
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
In general, you should not be mixing the asset pipeline with rails/webpacker and React on Rails.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
If you're using React, then all of your CSS and images should be under either `/client` or
|
|
6
|
+
`/app/javascript` or wherever you want your client side application.
|
|
6
7
|
|
|
8
|
+
If you are incrementally migrating a large application, your main concern will be how to minimize
|
|
9
|
+
duplication of styles and images between your old application and the new one.
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
This option still works for your `/config/initializers/react_on_rails.rb` if you are still using the
|
|
11
|
-
asset pipeline.
|
|
12
|
-
```
|
|
13
|
-
################################################################################
|
|
14
|
-
# MISCELLANEOUS OPTIONS
|
|
15
|
-
################################################################################
|
|
16
|
-
# If you want to use webpack for CSS and images, and still use the asset pipeline,
|
|
17
|
-
# see https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/rails-assets.md
|
|
18
|
-
# And you will use a setting like this.
|
|
19
|
-
config.symlink_non_digested_assets_regex = /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/
|
|
20
|
-
```
|
|
11
|
+
Please email [justin@shakacode.com](mailto:justin@shakacode.com) if you would be interested in help
|
|
12
|
+
to migrate a larger application.
|
|
@@ -39,7 +39,7 @@ const assetLoaderRules = [
|
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.
|
|
42
|
+
A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.jsx](../../spec/dummy/client/app/components/ImageExample/ImageExample.jsx)
|
|
43
43
|
|
|
44
44
|
You are free to use images either in image tags or as background images in SCSS files. You can
|
|
45
45
|
use a "global" location of /client/app/assets/images or a relative path to your JS or SCSS file, as
|
|
@@ -12,9 +12,10 @@ You can easily render React components in your JavaScript with `render` method t
|
|
|
12
12
|
* @param name Name of your registered component
|
|
13
13
|
* @param props Props to pass to your component
|
|
14
14
|
* @param domNodeId
|
|
15
|
+
* @param hydrate [optional] Pass truthy to update server rendered html. Default is falsy
|
|
15
16
|
* @returns {virtualDomElement} Reference to your component's backing instance
|
|
16
17
|
*/
|
|
17
|
-
ReactOnRails.render(componentName, props,
|
|
18
|
+
ReactOnRails.render(componentName, props, domNodeId)
|
|
18
19
|
```
|
|
19
20
|
|
|
20
21
|
## Why do we need this?
|