react_on_rails 12.0.0 → 12.1.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.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +12 -2
  3. data/.eslintrc +2 -1
  4. data/.github/workflows/lint-js-and-ruby.yml +53 -0
  5. data/.github/workflows/main.yml +178 -0
  6. data/.github/workflows/package-js-tests.yml +35 -0
  7. data/.github/workflows/rspec-package-specs.yml +45 -0
  8. data/.prettierrc +0 -3
  9. data/.rubocop.yml +47 -38
  10. data/.travis.yml +8 -4
  11. data/CHANGELOG.md +58 -28
  12. data/CONTRIBUTING.md +4 -3
  13. data/Gemfile +1 -33
  14. data/Gemfile.development_dependencies +51 -0
  15. data/NEWS.md +5 -0
  16. data/README.md +116 -105
  17. data/SUMMARY.md +1 -1
  18. data/docs/{basics → additional-details}/generator-details.md +1 -6
  19. data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
  20. data/docs/additional-details/recommended-project-structure.md +69 -0
  21. data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
  22. data/docs/api/javascript-api.md +5 -5
  23. data/docs/api/redux-store-api.md +5 -5
  24. data/docs/api/view-helpers-api.md +11 -12
  25. data/docs/basics/client-vs-server-rendering.md +4 -2
  26. data/docs/basics/configuration.md +43 -26
  27. data/docs/basics/deployment.md +2 -2
  28. data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +64 -9
  29. data/docs/{outdated → basics}/how-react-on-rails-works.md +8 -9
  30. data/docs/basics/installation-into-an-existing-rails-app.md +13 -6
  31. data/docs/basics/react-server-rendering.md +8 -5
  32. data/docs/basics/render-functions-and-railscontext.md +1 -1
  33. data/docs/basics/rspec-configuration.md +10 -10
  34. data/docs/{tutorial.md → basics/tutorial.md} +28 -30
  35. data/docs/basics/upgrading-react-on-rails.md +52 -15
  36. data/docs/basics/webpack-configuration.md +14 -22
  37. data/docs/contributor-info/linters.md +5 -6
  38. data/docs/contributor-info/pull-requests.md +2 -4
  39. data/docs/contributor-info/releasing.md +1 -1
  40. data/docs/home.md +382 -0
  41. data/docs/{outdated → javascript}/code-splitting.md +8 -8
  42. data/docs/javascript/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
  43. data/docs/{additional-reading → javascript}/images.md +5 -6
  44. data/docs/{additional-reading → javascript}/react-router.md +1 -1
  45. data/docs/{additional-reading → javascript}/server-rendering-tips.md +4 -1
  46. data/docs/{articles.md → misc/articles.md} +1 -1
  47. data/docs/misc/doctrine.md +5 -6
  48. data/docs/outdated/deferred-rendering.md +39 -0
  49. data/docs/outdated/rails-assets-relative-paths.md +3 -3
  50. data/docs/outdated/rails-assets.md +8 -8
  51. data/docs/outdated/rails3.md +2 -2
  52. data/docs/{additional-reading → rails}/convert-rails-5-api-only-app.md +1 -1
  53. data/docs/rails-webpacker-react-integration-options.md +182 -0
  54. data/docs/testimonials/testimonials.md +6 -6
  55. data/lib/generators/react_on_rails/base_generator.rb +8 -1
  56. data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
  57. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +8 -2
  58. data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
  59. data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
  60. data/lib/react_on_rails/configuration.rb +2 -0
  61. data/lib/react_on_rails/git_utils.rb +3 -3
  62. data/lib/react_on_rails/helper.rb +23 -17
  63. data/lib/react_on_rails/locales/base.rb +3 -3
  64. data/lib/react_on_rails/locales/to_js.rb +0 -4
  65. data/lib/react_on_rails/locales/to_json.rb +0 -4
  66. data/lib/react_on_rails/prerender_error.rb +1 -1
  67. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +6 -6
  68. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
  69. data/lib/react_on_rails/test_helper.rb +2 -0
  70. data/lib/react_on_rails/utils.rb +24 -4
  71. data/lib/react_on_rails/version.rb +1 -1
  72. data/lib/react_on_rails/webpacker_utils.rb +9 -5
  73. data/lib/tasks/assets.rake +35 -14
  74. data/package.json +24 -29
  75. data/rakelib/docker.rake +0 -5
  76. data/rakelib/examples.rake +1 -1
  77. data/rakelib/lint.rake +4 -10
  78. data/rakelib/release.rake +28 -16
  79. data/rakelib/run_rspec.rake +10 -11
  80. data/rakelib/task_helpers.rb +16 -4
  81. data/react_on_rails.gemspec +3 -17
  82. data/yarn.lock +2272 -4960
  83. metadata +50 -224
  84. data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
  85. data/docs/additional-reading/webpack-dev-server.md +0 -15
  86. data/docs/basics/recommended-project-structure.md +0 -77
  87. data/ruby-lint.yml +0 -25
  88. /data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
  89. /data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
  90. /data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
  91. /data/docs/{basics → deployment}/heroku-deployment.md +0 -0
  92. /data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
  93. /data/docs/{additional-reading → javascript}/asset-pipeline.md +0 -0
  94. /data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
  95. /data/docs/{additional-reading → javascript}/credits.md +0 -0
  96. /data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
  97. /data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
  98. /data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
  99. /data/docs/{additional-reading → javascript}/react-helmet.md +0 -0
  100. /data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
  101. /data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
  102. /data/docs/{additional-reading → javascript}/webpack.md +0 -0
  103. /data/docs/{coding-style → misc}/style.md +0 -0
  104. /data/docs/{additional-reading → misc}/tips.md +0 -0
  105. /data/docs/{additional-reading → rails}/rails-engine-integration.md +0 -0
  106. /data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
  107. /data/docs/{additional-reading → rails}/turbolinks.md +0 -0
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).
@@ -1,6 +1,6 @@
1
1
  # Code Splitting (Outdated)
2
2
 
3
- _Note: This document is outdated._ Please email [justin@shakacode.com](mailto:justin@shakacode.com)
3
+ _Note: This document is outdated._ Please email [justin@shakacode.com](mailto:justin@shakacode.com)
4
4
  if you would be interested in help with code splitting using
5
5
  [loadable-components.com](https://loadable-components.com/docs) with React on Rails.
6
6
 
@@ -21,11 +21,11 @@ Let's say you're requesting a page that needs to fetch a code chunk from the ser
21
21
  > (server) <div data-reactroot="
22
22
  <!--This comment is here because the comment beginning on line 13 messes up Sublime's markdown parsing-->
23
23
 
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.
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.
25
25
 
26
26
  ### The solution
27
27
 
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.
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.
29
29
 
30
30
  Here's an example of how you might use this in practice:
31
31
 
@@ -68,7 +68,7 @@ ReactOnRails.register({
68
68
  RouterApp,
69
69
  });
70
70
  ```
71
- Note that you should not register a renderer on the server, since there won't be a domNodeId when we're server rendering. Note that the `RouterApp` imported by `serverRegistration.js` is from a different file. For an example of how to set up an app for server rendering, see the [react router docs](../additional-reading/react-router.md).
71
+ Note that you should not register a renderer on the server, since there won't be a domNodeId when we're server rendering. Note that the `RouterApp` imported by `serverRegistration.js` is from a different file. For an example of how to set up an app for server rendering, see the [react router docs](https://www.shakacode.com/react-on-rails/docs/javascript/react-router).
72
72
 
73
73
  #### RouterAppRenderer.jsx
74
74
  ```jsx
@@ -117,8 +117,8 @@ There's an implemented example of code splitting in the `spec/dummy` folder of t
117
117
 
118
118
  See:
119
119
 
120
- - [spec/dummy/client/app/startup/clientRegistration.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/clientRegistration.jsx)
121
- - [spec/dummy/client/app/startup/serverRegistration.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/serverRegistration.jsx)
120
+ - [spec/dummy/client/app/startup/client-bundle.js](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/client-bundle.js)
121
+ - [spec/dummy/client/app/packs/server-bundle.js](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/packs/server-bundle.js)
122
122
  - [spec/dummy/client/app/startup/DeferredRenderAppClient](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/DeferredRenderAppClient.jsx)<-- Code splitting implemented here
123
123
  - [spec/dummy/client/app/startup/DeferredRenderAppServer.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/startup/DeferredRenderAppServer.jsx)
124
124
  - [spec/dummy/client/app/components/DeferredRender.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/components/DeferredRender.jsx)
@@ -134,7 +134,7 @@ If you're going to try to do code splitting with server rendered routes, you'll
134
134
 
135
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.
136
136
 
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.
137
+ Also, do not attempt to register a renderer function 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.
138
138
 
139
139
  ## How does Webpack know where to find my code chunks?
140
140
 
@@ -150,7 +150,7 @@ config = {
150
150
 
151
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.
152
152
 
153
- See [rails-assets.md](docs/outdated/rails-assets.md) to learn more about static assets.
153
+ See [our rails assets documentation](https://www.shakacode.com/react-on-rails/docs/outdated/rails-assets) to learn more about static assets.
154
154
 
155
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:
156
156
 
@@ -0,0 +1,10 @@
1
+ # Converting from Custom Webpack Config to Rails Webpacker Config
2
+
3
+ 1. Compare your package.json and the dependencies in https://github.com/rails/webpacker/blob/master/package.json
4
+ and avoid any duplicates. We don't want different versions of the same packages.
5
+ We want the versions from rails/webpacker unless we specifically want to override them.
6
+ 2. Search the rails/webpacker repo for anything you're not sure about in terms of package names.
7
+ 3. run `bin/webpack` and make sure there are zero errors
8
+ 4. update webpack plugins and loaders to current or close to current
9
+ 5. Make sure that your bin/webpack and bin/webpacker match the latest on
10
+ https://github.com/rails/webpacker/tree/master/lib/install/bin
@@ -1,8 +1,8 @@
1
1
  # Images
2
2
 
3
- 1. leading slash necessary on the
3
+ 1. leading slash necessary on the
4
4
  a. Option name for the file-loader and url-loader (todo reference)
5
- b. Option publicPath for the output (todo reference)
5
+ b. Option publicPath for the output (todo reference)
6
6
 
7
7
 
8
8
 
@@ -39,13 +39,13 @@ const assetLoaderRules = [
39
39
 
40
40
 
41
41
 
42
- A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.jsx](../../spec/dummy/client/app/components/ImageExample/ImageExample.jsx)
42
+ A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.jsx](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/client/app/components/ImageExample/ImageExample.jsx)
43
43
 
44
- You are free to use images either in image tags or as background images in SCSS files. You can
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
46
46
  is done with CSS modules.
47
47
 
48
- **images** is a defined alias, so "images/foobar.jpg" would point to the file at
48
+ **images** is a defined alias, so "images/foobar.jpg" would point to the file at
49
49
  `/client/app/assets/images/foobar.jpg.`
50
50
 
51
51
  ```
@@ -55,4 +55,3 @@ is done with CSS modules.
55
55
  },
56
56
  },
57
57
  ```
58
-
@@ -39,7 +39,7 @@ For a fleshed out integration of react_on_rails with react-router, check out [Re
39
39
 
40
40
  # Server Rendering Using React Router V4
41
41
 
42
- Your render function may not return an object with the property `renderedHtml`. Thus, you call
42
+ Your Render-Function may not return an object with the property `renderedHtml`. Thus, you call
43
43
  renderToString() and return an object with this property.
44
44
 
45
45
  This example **only applies to server rendering** and should be only used in the server side bundle.
@@ -13,7 +13,7 @@ Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_o
13
13
  - You can conditionally avoid running code that references document by either checking if `window`
14
14
  is defined or using the "railsContext"
15
15
  your top level react component. Since the passed in props Hash from the view helper applies to
16
- client and server side code, the best way to do this is to use a render function.
16
+ client and server side code, the best way to do this is to use a Render-Function.
17
17
  - If you're serious about server rendering, it's worth the effort to have different entry points for client and server rendering. It's worth the extra complexity. The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server side.
18
18
  - You can enable Node.js server rendering via [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
19
19
 
@@ -22,6 +22,9 @@ Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_o
22
22
  1. First be sure your code works with server rendering disabled (`prerender: false`)
23
23
  2. Be sure that `config.trace` is true. You will get the server invocation code that renders your component. If you're not using Webpacker, you will also get the whole file used to setup the JavaScript context.
24
24
 
25
+ ## CSS
26
+ Server bundles must always have CSS Extracted
27
+
25
28
  ## setTimeout, setInterval, and clearTimeout
26
29
 
27
30
  These methods are polyfilled for server rendering to be no-ops. We log calls to these when in `trace` mode. In the past, some libraries, namely babel-polyfill, did call setTimout.
@@ -6,7 +6,7 @@
6
6
  * [Webpacker Lite: Why Fork Webpacker?](https://blog.shakacode.com/webpacker-lite-why-fork-webpacker-f0a7707fac92)
7
7
  * [React on Rails, 2000+ 🌟 Stars](https://medium.com/shakacode/react-on-rails-2000-stars-32ff5cfacfbf#.6gmfb2gpy)
8
8
  * [The React on Rails Doctrine](https://medium.com/@railsonmaui/the-react-on-rails-doctrine-3c59a778c724)
9
- * [Simple Tutorial](docs/tutorial.md).
9
+ * [Simple Tutorial](https://www.shakacode.com/react-on-rails/docs/basics/tutorial).
10
10
 
11
11
  ## Videos
12
12
 
@@ -4,7 +4,7 @@ By Justin Gordon, January 26, 2016
4
4
 
5
5
  This document is an extension and complement to [The Rails Doctrine](http://rubyonrails.org/doctrine/). If you haven't read that document, I suggest you do so first.
6
6
 
7
- As stated in the [React on Rails README](../../README.md), the project objective is to provide an opinionated and optimal framework for integrating **Ruby on Rails** with modern JavaScript tooling and libraries. When considering what goes into **react_on_rails**, we ask ourselves, is the functionality related to the intersection of using Rails and with modern JavaScript? A good example is view helper integration of React components on a Rails view. If the answer is yes, then the functionality belongs right here. In other cases, we're releasing separate npm packages or Ruby gems. For example, we needed an easy way to integrate [Twitter Bootstrap](http://getbootstrap.com/) with Webpack, and we released the [npm bootstrap-loader](https://github.com/shakacode/bootstrap-loader/).
7
+ As stated in the [React on Rails README](https://www.shakacode.com/react-on-rails/docs/), the project objective is to provide an opinionated and optimal framework for integrating **Ruby on Rails** with modern JavaScript tooling and libraries. When considering what goes into **react_on_rails**, we ask ourselves, is the functionality related to the intersection of using Rails and with modern JavaScript? A good example is view helper integration of React components on a Rails view. If the answer is yes, then the functionality belongs right here. In other cases, we're releasing separate npm packages or Ruby gems. For example, we needed an easy way to integrate [Twitter Bootstrap](http://getbootstrap.com/) with Webpack, and we released the [npm bootstrap-loader](https://github.com/shakacode/bootstrap-loader/).
8
8
 
9
9
  Besides the project objective, let's stick with the "Rails Doctrine" and keep the following in mind.
10
10
 
@@ -19,15 +19,14 @@ 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](docs/basics/heroku-deployment.md).
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 heroku deployment documentation](https://www.shakacode.com/react-on-rails/docs/deployment/heroku-deployment).
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:
26
- * [Style Guide](../coding-style/style.md)
27
- * [linters](../contributor-info/linters.md)
28
- * [Recommended Project Structure](../basics/recommended-project-structure.md)
26
+ * [Style Guide](https://www.shakacode.com/react-on-rails/docs/misc/style)
27
+ * [linters](https://www.shakacode.com/react-on-rails/docs/contributor-info/linters)
29
28
 
30
- We're big believers in this quote from the Rails Doctrine:
29
+ We're big believers in this quote from the Rails Doctrine:
31
30
 
32
31
  > The same goes even when you understand how all the pieces go together. When there’s an obvious next step for every change, we can scoot through the many parts of an application that is the same or very similar to all the other applications that went before it. A place for everything and everything in its place. Constraints liberate even the most able minds.
33
32
 
@@ -0,0 +1,39 @@
1
+ # Deferred Rendering
2
+
3
+ Please see [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/] if you are interested in code splitting using
4
+ [loadable-components.com](https://loadable-components.com/docs) with React on Rails.
5
+
6
+ -----
7
+
8
+ What is code splitting? From the webpack documentation:
9
+
10
+ > For big web apps it’s not efficient to put all code into a single file, especially if some blocks of code are only required under some circumstances. Webpack has a feature to split your codebase into “chunks” which are loaded on demand. Some other bundlers call them “layers”, “rollups”, or “fragments”. This feature is called “code splitting”.
11
+
12
+ ## Server Rendering and Code Splitting
13
+
14
+ Let's say you're requesting a page that needs to fetch a code chunk from the server before it's able to render. If you do all your rendering on the client side, you don't have to do anything special. However, if the page is rendered on the server, you'll find that React will spit out the following error:
15
+
16
+ > Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
17
+
18
+ > (client) <!-- react-empty: 1 -
19
+
20
+ > (server) <div data-reactroot="
21
+ <!--This comment is here because the comment beginning on line 13 messes up Sublime's markdown parsing-->
22
+
23
+ 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 by default render the component as soon as the page loads. However, code splitting requires that components render at a later time when the JavaScript chunks have loaded.
24
+
25
+ ## Solution
26
+
27
+ 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.
28
+
29
+
30
+ ## Server vs. Client Code Caveats
31
+
32
+ If you're going to try to do code splitting with server rendered routes, you'll probably need to use seperate route definitions for client and server to prevent code splitting from happening for the server bundle. The server bundle should be one file containing all the JavaScript code. This will require you to have seperate webpack configurations for client and server.
33
+
34
+ Do not attempt to register a renderer function 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.
35
+
36
+
37
+ ## React on Rails Pro
38
+ [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro/] includes a complete setup using this technique for code splitting using
39
+ [loadable-components.com](https://loadable-components.com/docs) with React on Rails.