react_on_rails 12.0.5.beta.0 → 12.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +11 -1
  3. data/.eslintrc +2 -0
  4. data/.prettierrc +0 -3
  5. data/.rubocop.yml +38 -1
  6. data/CHANGELOG.md +22 -13
  7. data/CONTRIBUTING.md +3 -2
  8. data/Gemfile.development_dependencies +3 -2
  9. data/README.md +68 -65
  10. data/docs/{basics → additional-details}/generator-details.md +1 -6
  11. data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
  12. data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
  13. data/docs/{additional-reading → additional-details}/recommended-project-structure.md +0 -0
  14. data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
  15. data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
  16. data/docs/api/javascript-api.md +2 -2
  17. data/docs/api/redux-store-api.md +3 -3
  18. data/docs/api/view-helpers-api.md +7 -8
  19. data/docs/basics/client-vs-server-rendering.md +3 -3
  20. data/docs/basics/configuration.md +42 -25
  21. data/docs/basics/deployment.md +2 -2
  22. data/docs/{outdated → basics}/how-react-on-rails-works.md +8 -9
  23. data/docs/basics/installation-into-an-existing-rails-app.md +2 -2
  24. data/docs/basics/react-server-rendering.md +3 -3
  25. data/docs/basics/rspec-configuration.md +10 -10
  26. data/docs/{tutorial.md → basics/tutorial.md} +9 -9
  27. data/docs/basics/upgrading-react-on-rails.md +7 -7
  28. data/docs/basics/webpack-configuration.md +4 -6
  29. data/docs/contributor-info/linters.md +5 -6
  30. data/docs/contributor-info/pull-requests.md +2 -4
  31. data/docs/contributor-info/releasing.md +1 -1
  32. data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
  33. data/docs/{basics → deployment}/heroku-deployment.md +0 -0
  34. data/docs/home.md +382 -0
  35. data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
  36. data/docs/{additional-reading → javascript}/asset-pipeline.md +0 -0
  37. data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
  38. data/docs/{outdated → javascript}/code-splitting.md +3 -3
  39. data/docs/{additional-reading → javascript}/converting-from-custom-webpack-config-to-rails-webpacker-config.md +3 -3
  40. data/docs/{additional-reading → javascript}/credits.md +0 -0
  41. data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
  42. data/docs/{additional-reading → javascript}/images.md +5 -6
  43. data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
  44. data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
  45. data/docs/{additional-reading → javascript}/react-helmet.md +0 -0
  46. data/docs/{additional-reading → javascript}/react-router.md +0 -0
  47. data/docs/{additional-reading → javascript}/server-rendering-tips.md +0 -0
  48. data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
  49. data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
  50. data/docs/{additional-reading → javascript}/webpack.md +0 -0
  51. data/docs/{articles.md → misc/articles.md} +1 -1
  52. data/docs/misc/doctrine.md +5 -5
  53. data/docs/{coding-style → misc}/style.md +0 -0
  54. data/docs/{additional-reading → misc}/tips.md +0 -0
  55. data/docs/outdated/deferred-rendering.md +39 -0
  56. data/docs/outdated/rails-assets-relative-paths.md +3 -3
  57. data/docs/outdated/rails-assets.md +8 -8
  58. data/docs/outdated/rails3.md +2 -2
  59. data/docs/rails-webpacker-react-integration-options.md +182 -0
  60. data/docs/{additional-reading → rails}/convert-rails-5-api-only-app.md +1 -1
  61. data/docs/{additional-reading → rails}/rails-engine-integration.md +0 -0
  62. data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
  63. data/docs/{additional-reading → rails}/turbolinks.md +0 -0
  64. data/docs/testimonials/testimonials.md +6 -6
  65. data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
  66. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +4 -1
  67. data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
  68. data/lib/react_on_rails/helper.rb +5 -0
  69. data/lib/react_on_rails/utils.rb +16 -2
  70. data/lib/react_on_rails/version.rb +1 -1
  71. data/lib/react_on_rails/webpacker_utils.rb +5 -1
  72. data/lib/tasks/assets.rake +14 -5
  73. data/package.json +24 -29
  74. data/rakelib/release.rake +19 -3
  75. data/rakelib/task_helpers.rb +15 -2
  76. data/yarn.lock +2272 -4960
  77. metadata +41 -38
  78. data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
@@ -52,7 +52,7 @@ First be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If
52
52
  cd <directory where you want to create your new Rails app>
53
53
 
54
54
  # Any name you like for the rails app
55
- # Skip javascript so will add that next and get the current version
55
+ # Skip javascript so will add that next and get the current version
56
56
  rails new --skip-sprockets -J --skip-turbolinks test-react-on-rails-v12-no-sprockets
57
57
 
58
58
  cd test-react-on-rails
@@ -96,7 +96,7 @@ git commit -m "Initial commit"
96
96
 
97
97
  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
98
 
99
- Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
99
+ Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
100
100
  If you want the redux install: `rails generate react_on_rails:install --redux`
101
101
 
102
102
  ```
@@ -123,7 +123,7 @@ First, check that the `hmr` and the `inline` options are `true` in your `config/
123
123
 
124
124
  The basic setup will have HMR working with the default webpacker setup. The basic
125
125
  [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.
126
+ React setup, will cause a full page refresh each time you save a file.
127
127
 
128
128
  ## Deploying to Heroku
129
129
 
@@ -292,8 +292,8 @@ versus with server rendering:
292
292
 
293
293
  For more details on server rendering, see:
294
294
 
295
- + [Client vs. Server Rendering](./basics/client-vs-server-rendering.md)
296
- + [React Server Rendering](./basics/react-server-rendering.md)
295
+ + [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/basics/client-vs-server-rendering)
296
+ + [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/basics/react-server-rendering)
297
297
 
298
298
  ## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
299
299
 
@@ -320,7 +320,7 @@ When you change a JSX file and save, the browser will automatically refresh!
320
320
 
321
321
  So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
322
322
 
323
- * https://github.com/rails/webpacker/blob/master/docs/webpack-dev-server.md
323
+ * https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md
324
324
  * https://webpack.js.org/configuration/dev-server/
325
325
  * https://webpack.js.org/concepts/hot-module-replacement/
326
326
 
@@ -335,7 +335,7 @@ In case you are running some custom setup with different IP or PORT you should a
335
335
  web: rails s -p 8080 -b 0.0.0.0
336
336
  ```
337
337
 
338
- Then visit https://your-shared-addr.c9users.io:8080/hello_world
338
+ Then visit https://your-shared-addr.c9users.io:8080/hello_world
339
339
 
340
340
  ## RubyMine
341
341
 
@@ -349,8 +349,8 @@ It's super important to exclude certain directories from RubyMine or else it wil
349
349
 
350
350
  ## Conclusion
351
351
 
352
- * Browse the docs either on the [gitbook](https://shakacode.gitbooks.io/react-on-rails/content/) or in the [docs directory on github](https://github.com/shakacode/react_on_rails/tree/master/docs)
352
+ * Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
353
353
 
354
- Feedback is greatly appreciated! As are stars on github!
354
+ Feedback is greatly appreciated! As are stars on github!
355
355
 
356
356
  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](./render-functions-and-railscontext.md) for
28
+ Please see [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/basics/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/master/docs/folder-structure.md). If you follow the steps in the the [basic tutorial](../../docs/tutorial.md), 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://github.com/rails/webpacker/blob/master/docs/webpack.md).
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/basics/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
-
@@ -1,5 +1,5 @@
1
1
  # Linters
2
- These linters support the [ShakaCode Style Guidelines](./style.md)
2
+ These linters support the [ShakaCode Style Guidelines](https://www.shakacode.com/react-on-rails/docs/misc/style)
3
3
 
4
4
  ## Autofix!
5
5
 
@@ -15,9 +15,9 @@ If you haven't tried the autofix options for `eslint` and `rubocop`, you're seri
15
15
  ```
16
16
  eslint --fix .
17
17
  ```
18
-
19
- or
20
-
18
+
19
+ or
20
+
21
21
  ```
22
22
  npm run lint -- --fix
23
23
  ```
@@ -32,7 +32,7 @@ Rules are configured with a 0, 1 or 2. Setting a rule to 0 is turning it off, se
32
32
 
33
33
  Rules can also take a few additional options. In this case, the rule can be set to an array, the first item of which is the 0/1/2 flag and the rest are options.
34
34
 
35
- See file [.eslintrc](../../client/.eslintrc) for examples of configuration
35
+ See file [.eslintrc](https://github.com/shakacode/react_on_rails/tree/master/.eslintrc) for examples of configuration
36
36
 
37
37
  ### Specify/Override rules in code
38
38
 
@@ -66,4 +66,3 @@ You can disable all rules for a line or block, or only specific rules, as shown
66
66
  * [ESLint quick start](http://untilfalse.com/eslint-quick-start/)
67
67
  * [RuboCop](https://github.com/bbatsov/rubocop)
68
68
  * [ESLint](http://eslint.org/)
69
-
@@ -1,4 +1,4 @@
1
- # Pull Requests
1
+ # Pull Requests
2
2
 
3
3
  ## Checklist before Committing
4
4
  1. `rake`: runs all linters and specs (you need Docker setup, see below)
@@ -36,9 +36,7 @@ When making doc changes, we want the change to work on both the gitbook and the
36
36
 
37
37
  ### Links to other docs:
38
38
  * When making references to doc files, use a relative URL path like:
39
- `[Installation Overview](docs/basics/installation-overview.md)`
39
+ `[Installation Overview](https://www.shakacode.com/react-on-rails/docs/additional-details/manual-installation-overview)`
40
40
 
41
41
  * When making references to source code files, use a full url path like:
42
42
  `[spec/dummy/config/initializers/react_on_rails.rb](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/initializers/react_on_rails.rb)`
43
-
44
-
@@ -3,7 +3,7 @@
3
3
  We're now releasing this as a combined ruby gem plus npm package. We will keep the version numbers in sync.
4
4
 
5
5
  ## Testing the Gem before Release from a Rails App
6
- See [Contributing](../../CONTRIBUTING.md)
6
+ See [Contributing](https://github.com/shakacode/react_on_rails/tree/master/CONTRIBUTING.md)
7
7
 
8
8
  ## Releasing a new gem version
9
9
  Run `rake -D release` to see instructions on how to release via the rake task.
File without changes
data/docs/home.md ADDED
@@ -0,0 +1,382 @@
1
+ # React on Rails
2
+
3
+ *These are the docs for React on Rails 12. To see the version 11 docs, [click here](https://github.com/shakacode/react_on_rails/tree/11.3.0).*
4
+
5
+ **If you're viewing this page on Github, please go to [our documentation website](https://www.shakacode.com/react-on-rails/docs/) instead!**
6
+
7
+ #### News
8
+ **October 14, 2020**: [RUBY ROGUES
9
+ RR 474: React on Rails V12 – Don’t Shave That Yak! with Justin Gordon](https://devchat.tv/ruby-rogues/rr-474-react-on-rails-v12-dont-shave-that-yak-with-justin-gordon/).
10
+
11
+ **October 1, 2020**: See the [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
12
+ that supports SSR.
13
+
14
+ **August 2, 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`.
15
+
16
+ **July 8, 2020**: Release v12 of React on Rails.
17
+
18
+ **Major Improvements**
19
+
20
+ 1. **React Hooks Support** for top level components
21
+ 2. **Typescript bindings**
22
+ 3. **rails/webpacker** "just works" with React on Rails by default.
23
+ 4. i18n support for generating a JSON file rather than a JS file.
24
+
25
+ Be sure to see the [CHANGELOG.md](https://github.com/shakacode/react_on_rails/tree/master/CHANGELOG.md) and read the upgrade instructions:
26
+ [docs/basics/upgrading-react-on-rails](https://www.shakacode.com/react-on-rails/docs/basics/upgrading-react-on-rails#upgrading-to-v12).
27
+
28
+ * 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
+ * Are you interested in support for React on Rails? Do you want to use Node.js to do your server-side rendering so libraries like Emotion and Loadable Components just work, as compared to rendering via Ruby embedded JS? If so check out [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
30
+ * 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.
31
+ ---
32
+
33
+ #### About
34
+ React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
35
+
36
+ This project is maintained by the software consulting firm [ShakaCode](https://www.shakacode.com). We focus on Ruby on Rails applications with React front-ends, often using TypeScript or ReasonML. We also build Gatsby sites. See [our recent work](https://www.shakacode.com/recent-work) for examples of what we do.
37
+
38
+ Interested in optimizing your webpack setup for React on Rails including code
39
+ splitting with [react-router](https://github.com/ReactTraining/react-router#readme),
40
+ and [loadable-components](https://loadable-components.com/) with server-side rendering?
41
+ 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/).
42
+
43
+ Feel free to contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakacode.com), maintainer of React on Rails, for more information.
44
+
45
+ [Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
46
+
47
+ # Intro
48
+
49
+ ## Project Objective
50
+
51
+ 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.
52
+
53
+ ## Features and Why React on Rails?
54
+
55
+ Given that `rails/webpacker` gem already provides basic React integration, why would you use "React on Rails"?
56
+
57
+ 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.
58
+ 1. Tight integration with [rails/webpacker](https://github.com/rails/webpacker).
59
+ 1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `rails/webpacker`.
60
+ 1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration with server-side-rendering.
61
+ 1. [Internationalization (I18n) and (localization)](https://www.shakacode.com/react-on-rails/docs/basics/i18n)
62
+ 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/).
63
+ 1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example).
64
+
65
+ See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
66
+
67
+ ## ShakaCode Forum Premium Content
68
+ _Requires creating a free account._
69
+
70
+ * [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)
71
+
72
+ ----
73
+
74
+ # Docs
75
+
76
+ **Consider browsing this on our [website](https://www.shakacode.com/react-on-rails/docs/).**
77
+
78
+ ## Prerequisites
79
+
80
+ Ruby on Rails >=5 and rails/webpacker 4.2+.
81
+
82
+ ## Getting Started
83
+
84
+ Note, the best way to understand how to use ReactOnRails is to study a few simple examples. You can do a quick demo setup, either on your existing app or on a new Rails app.
85
+
86
+ 1. Do the quick [tutorial](https://www.shakacode.com/react-on-rails/docs/basics/tutorial).
87
+ 2. Add React on Rails to an existing Rails app per [the instructions](https://www.shakacode.com/react-on-rails/docs/basics/installation-into-an-existing-rails-app).
88
+ 3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy), a simple, no DB example.
89
+ 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).
90
+
91
+ ## Basic Installation
92
+
93
+ *See also [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/basics/installation-into-an-existing-rails-app).*
94
+
95
+ 2. Add the `react_on_rails` gem to Gemfile:
96
+
97
+ ```bash
98
+ bundle add react_on_rails --strict
99
+ ```
100
+
101
+ 4. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
102
+
103
+ 5. Run the generator:
104
+
105
+ ```bash
106
+ rails generate react_on_rails:install
107
+ ```
108
+
109
+ 6. Start the app:
110
+
111
+ ```bash
112
+ rails s
113
+ ```
114
+
115
+ 7. Visit http://localhost:3000/hello_world.
116
+
117
+
118
+ ### Turning on server rendering
119
+
120
+ With the code from running the React on Rails generator above:
121
+
122
+ 1. Edit `app/views/hello_world/index.html.erb` and set `prerender` to `true`.
123
+ 2. Refresh the page.
124
+
125
+ Below is the line where you turn server rendering on by setting `prerender` to true:
126
+
127
+ ```erb
128
+ <%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
129
+ ```
130
+
131
+ Note, if you got an error in your console regarding "ReferenceError: window is not defined",
132
+ then you need to edit `config/webpacker.yml` and set `hmr: false` and `inline: false`.
133
+ See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
134
+ issue.
135
+
136
+ ## Basic Usage
137
+
138
+ ### Configuration
139
+
140
+ * Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](https://www.shakacode.com/react-on-rails/docs/basics/configuration) for documentation of all configuration options.
141
+ * Configure `config/webpacker.yml`. If you used the generator and the default webpacker setup, you don't need to touch this file. If you are customizing your setup, then consult the [spec/dummy/config/webpacker.yml](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/webpacker.yml) example or the official default [webpacker.yml](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml).
142
+ * 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.
143
+ * Your `public_output_path` must match your custom Webpack configuration for `output` of your bundles.
144
+ * Only set `cache_manifest` to `true` in your production env.
145
+
146
+ ## Including your React Component on your Rails Views
147
+
148
+ - React component are rendered via your Rails Views. Here's an ERB sample:
149
+
150
+ ```erb
151
+ <%= react_component("HelloWorld", props: @some_props) %>
152
+ ```
153
+
154
+ - **Server-Side Rendering**: Your react component is first rendered into HTML on the server. Use the **prerender** option:
155
+
156
+ ```erb
157
+ <%= react_component("HelloWorld", props: @some_props, prerender: true) %>
158
+ ```
159
+
160
+ - The `component_name` parameter is a string matching the name you used to expose your React component globally. So, in the above examples, if you had a React component named "HelloWorld", you would register it with the following lines:
161
+
162
+ ```js
163
+ import ReactOnRails from 'react-on-rails';
164
+ import HelloWorld from './HelloWorld';
165
+ ReactOnRails.register({ HelloWorld });
166
+ ```
167
+
168
+ Exposing your component in this way is how React on Rails is able to reference your component from a Rails view. You can expose as many components as you like, as long as their names do not collide. See below for the details of how you expose your components via the react_on_rails webpack configuration. You may call `ReactOnRails.register` many times.
169
+
170
+ - `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component:
171
+
172
+ ```ruby
173
+ # Rails View
174
+ <%= react_component("HelloWorld", props: { name: "Stranger" }) %>
175
+ ```
176
+
177
+ - This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://reactjs.org/docs/context.html). See [Render-Functions and the RailsContext](https://www.shakacode.com/react-on-rails/docs/basics/render-functions-and-railscontext) for more details on this topic.
178
+
179
+ ```js
180
+ import React from 'react';
181
+
182
+ export default (props, railsContext) => {
183
+ // Note wrap in a function to make this a React function component
184
+ return () => (
185
+ <div>
186
+ Your locale is {railsContext.i18nLocale}.<br/>
187
+ Hello, {props.name}!
188
+ </div>
189
+ );
190
+ };
191
+ ```
192
+
193
+ See the [View Helpers API](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api) for more details on `react_component` and its sibling function `react_component_hash`.
194
+
195
+ ## Globally Exposing Your React Components
196
+
197
+ For the React on Rails view helper `react_component` to use your React components, you will have to **register** them in your JavaScript code.
198
+
199
+ Use modules just as you would when using Webpack and React without Rails. The difference is that instead of mounting React components directly to an element using `React.render`, you **register your components to ReactOnRails and then mount them with helpers inside of your Rails views**.
200
+
201
+ This is how to expose a component to the `react_component` view helper.
202
+
203
+ ```javascript
204
+ // app/javascript/packs/hello-world-bundle.js
205
+ import HelloWorld from '../components/HelloWorld';
206
+ import ReactOnRails from 'react-on-rails';
207
+ ReactOnRails.register({ HelloWorld });
208
+ ```
209
+
210
+ #### Different Server-Side Rendering Code (and a Server-Specific Bundle)
211
+
212
+ 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() }`.
213
+
214
+ 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.
215
+
216
+ 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._)
217
+
218
+ ## Specifying Your React Components: Register directly or use render-functions
219
+
220
+ 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:
221
+
222
+ 1. You to have access to the `railsContext`. See [documentation for the railsContext](https://www.shakacode.com/react-on-rails/docs/basics/render-functions-and-railscontext) in terms of why you might need it. You **need** a Render-Function to access the `railsContext`.
223
+ 2. You can use the passed-in props to initialize a redux store or set up react-router.
224
+ 3. You can return different components depending on what's in the props.
225
+
226
+ Note, the return value of a **Render-Function** should be either a React Function or Class Component, or an object representing server rendering results.
227
+
228
+ Do not return a React Element (JSX).
229
+
230
+ ReactOnRails will automatically detect a registered Render-Function by the fact that the function takes
231
+ more than 1 parameter. In other words, if you want the ability to provide a function that returns the
232
+ React component, then you need to specify at least a second parameter. This is the `railsContext`.
233
+ If you're not using this parameter, declare your function with the unused param:
234
+
235
+ ```js
236
+ const MyComponentGenerator = (props, _railsContext) => {
237
+ if (props.print) {
238
+ // This is a React FunctionComponent because it is wrapped in a function.
239
+ return () => <H1>{JSON.stringify(props)}</H1>;
240
+ }
241
+ }
242
+ ```
243
+
244
+ Thus, there is no difference between registering a React Function Component or class Component versus a "Render-Function." Just call `ReactOnRails.register`.
245
+
246
+ ## react_component_hash for Render-Functions
247
+
248
+ 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`.
249
+
250
+ 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:
251
+
252
+ ```js
253
+ { renderedHtml: { componentHtml, customKey1, customKey2} }
254
+ ```
255
+
256
+ For details on using react_component_hash with react-helmet, see [our react-helmet documentation](https://www.shakacode.com/react-on-rails/docs/additional-reading/react-helmet).
257
+
258
+ ## Error Handling
259
+
260
+ * All errors from ReactOnRails will be of type ReactOnRails::Error.
261
+ * Prerendering (server rendering) errors get context information for HoneyBadger and Sentry for easier debugging.
262
+
263
+ ## I18n
264
+
265
+ React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.json` or `*.js*.
266
+ See the [How to add I18n](https://www.shakacode.com/react-on-rails/docs/basics/i18n) for a summary of adding I18n.
267
+
268
+ ## More Details
269
+
270
+ Browse the links in the [Summary Table of Contents](https://github.com/shakacode/react_on_rails/tree/master/SUMMARY.md)
271
+
272
+ Here are some highly recommended next articles to read:
273
+
274
+ 1. [How React on Rails Works](https://www.shakacode.com/react-on-rails/docs/basics/how-react-on-rails-works)
275
+ 1. [Webpack Configuration](https://www.shakacode.com/react-on-rails/docs/basics/webpack-configuration)
276
+ 1. [View Helpers API](https://www.shakacode.com/react-on-rails/docs/api/view-helpers-api)
277
+ 1. [Caching and Performance: React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
278
+ 1. [Deployment](https://www.shakacode.com/react-on-rails/docs/basics/deployment).
279
+
280
+ # Support
281
+
282
+ [Click to join **React + Rails Slack**](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
283
+
284
+ ## Community Resources
285
+
286
+ Please [**click to subscribe**](https://app.mailerlite.com/webforms/landing/l1d9x5) to keep in touch with Justin Gordon and [ShakaCode](http://www.shakacode.com/). I intend to send announcements of new releases of React on Rails and of our latest [blog articles](https://blog.shakacode.com) and tutorials.
287
+
288
+ [![2017-01-31_14-16-56](https://cloud.githubusercontent.com/assets/1118459/22490211/f7a70418-e7bf-11e6-9bef-b3ccd715dbf8.png)](https://app.mailerlite.com/webforms/landing/l1d9x5)
289
+
290
+ - **Slack Room**: [Contact us](mailto:contact@shakacode.com) for an invite to the ShakaCode Slack room! Let us know if you want to contribute.
291
+ - **[forum.shakacode.com](https://forum.shakacode.com)**: Post your questions
292
+ - **[@railsonmaui on Twitter](https://twitter.com/railsonmaui)**
293
+ - For a live, [open source](https://github.com/shakacode/react-webpack-rails-tutorial), example of this gem, see [www.reactrails.com](http://www.reactrails.com).
294
+ - See [Projects](https://github.com/shakacode/react_on_rails/tree/master/PROJECTS.md) using and [KUDOS](https://github.com/shakacode/react_on_rails/tree/master/KUDOS.md) for React on Rails. Please submit yours! Please edit either page or [email us](mailto:contact@shakacode.com) and we'll add your info. We also **love stars** as it helps us attract new users and contributors.
295
+ - *See [NEWS.md](https://github.com/shakacode/react_on_rails/tree/master/NEWS.md) for more notes over time.*
296
+
297
+ ## Contributing
298
+
299
+ Bug reports and pull requests are welcome. See [Contributing](https://github.com/shakacode/react_on_rails/tree/master/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).
300
+
301
+ ## React on Rails Pro
302
+
303
+ 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.
304
+
305
+ [![2018-09-11_10-31-11](https://user-images.githubusercontent.com/1118459/45467845-5bcc7400-b6bd-11e8-91e1-e0cf806d4ea4.png)](https://blog.shakacode.com/hvmns-90-reduction-in-server-response-time-from-react-on-rails-pro-eb08226687db)
306
+
307
+ ## React on Rails Pro Support
308
+
309
+ The [React on Rails Pro Support Plan](https://www.shakacode.com/react-on-rails-pro) can help!
310
+
311
+ * Optimizing your webpack setup to the latest Webpack for React on Rails including code splitting with loadable-components.
312
+ * Upgrading your app to use the current `rails/webpacker` setup that skips the Sprockets asset pipeline.
313
+ * Better performance client and server side.
314
+ * Best practices based on over 6 years of React on Rails experience on many production projects.
315
+ * Using [Reason](https://reasonml.github.io/) with (or without) React on Rails.
316
+
317
+ 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 [HiChee.com](https://hichee.com), we can leverage that code for your app!
318
+
319
+ Please email Justin Gordon [justin@shakacode.com](mailto:justin@shakacode.com), the maintainer of React on Rails, for more information.
320
+
321
+ ### Fragment Caching
322
+
323
+ 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:
324
+
325
+ 1. The `cache_key` takes the same parameters as any Rails `cache` view helper.
326
+ 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`:
327
+
328
+ ```ruby
329
+ <%= cached_react_component("App", cache_key: [@user, @post], prerender: true) do
330
+ some_slow_method_that_returns_props
331
+ end %>
332
+ ```
333
+
334
+ Such fragment caching saves CPU work for your web server and greatly reduces the request time. It completely skips the evaluation costs of:
335
+
336
+ 1. Database calls to compute the props.
337
+ 2. Serialization the props values hash into a JSON string for evaluating JavaScript to server render.
338
+ 3. Costs associated with evaluating JavaScript from your Ruby code.
339
+ 4. Creating the HTML string containing the props and the server-rendered JavaScript code.
340
+
341
+ Note, even without server rendering (without step 3 above), fragment caching is still effective.
342
+
343
+ ## Integration with Node.js for Server Rendering
344
+
345
+ 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).
346
+
347
+ ## Testimonials for ShakaCode
348
+ [HVMN Testimonial, by Paul Benigeri, October 12, 2018](https://www.shakacode.com/react-on-rails/docs/testimonials/hvmn)
349
+ > 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.
350
+
351
+ [ResortPass Testimonial, by Leora Juster, December 10, 2018](https://www.shakacode.com/react-on-rails/docs/testimonials/resortpass)
352
+
353
+ > 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.
354
+
355
+ From Joel Hooks, Co-Founder, Chief Nerd at [egghead.io](https://egghead.io/), January 30, 2017:
356
+
357
+ ![2017-01-30_11-33-59](https://cloud.githubusercontent.com/assets/1118459/22443635/b3549fb4-e6e3-11e6-8ea2-6f589dc93ed3.png)
358
+
359
+ For more testimonials, see [Live Projects](https://github.com/shakacode/react_on_rails/tree/master/PROJECTS.md) and [Kudos](https://github.com/shakacode/react_on_rails/tree/master/KUDOS.md).
360
+
361
+ # Supporters
362
+
363
+ 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.
364
+
365
+ [![2019-09-24_17-48-00](https://user-images.githubusercontent.com/1118459/65567887-96353780-def3-11e9-926d-4a55e2e186ff.png)](https://www.jetbrains.com/ruby/)
366
+ [![Scout](https://user-images.githubusercontent.com/1118459/41828269-106b40f8-77d0-11e8-8d19-9c4b167ef9d8.png)](https://scoutapp.com/)
367
+ [![BrowserStack](https://cloud.githubusercontent.com/assets/1118459/23203304/1261e468-f886-11e6-819e-93b1a3f17da4.png)](https://www.browserstack.com)
368
+ [![CodersRank](https://user-images.githubusercontent.com/1118459/55040254-ad8a7b00-4fcb-11e9-8936-c6765eb30698.png)](https://codersrank.io/?utm_source=github&utm_medium=banner&utm_campaign=shakacode)
369
+
370
+ ## Clubhouse
371
+ 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!
372
+
373
+ *If you'd like to support React on Rails and have your company listed here, [get in touch](mailto:justin@shakacode.com).*
374
+
375
+ Aloha and best wishes from Justin and the ShakaCode team!
376
+
377
+ # Work with Us
378
+ 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!
379
+
380
+ # License
381
+
382
+ The gem is available as open source under the terms of the [MIT License](https://github.com/shakacode/react_on_rails/tree/master/LICENSE.md).