react_on_rails 12.0.0 → 12.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc +0 -1
  3. data/.github/workflows/lint-js-and-ruby.yml +53 -0
  4. data/.github/workflows/main.yml +178 -0
  5. data/.github/workflows/package-js-tests.yml +35 -0
  6. data/.github/workflows/rspec-package-specs.yml +45 -0
  7. data/.rubocop.yml +18 -40
  8. data/.travis.yml +8 -4
  9. data/CHANGELOG.md +41 -20
  10. data/CONTRIBUTING.md +1 -1
  11. data/Gemfile +1 -33
  12. data/Gemfile.development_dependencies +54 -0
  13. data/NEWS.md +5 -0
  14. data/README.md +73 -65
  15. data/SUMMARY.md +1 -1
  16. data/docs/additional-reading/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
  17. data/docs/additional-reading/react-router.md +1 -1
  18. data/docs/additional-reading/recommended-project-structure.md +69 -0
  19. data/docs/additional-reading/server-rendering-tips.md +4 -1
  20. data/docs/api/javascript-api.md +3 -3
  21. data/docs/api/redux-store-api.md +2 -2
  22. data/docs/api/view-helpers-api.md +4 -4
  23. data/docs/basics/client-vs-server-rendering.md +2 -0
  24. data/docs/basics/configuration.md +1 -1
  25. data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +64 -9
  26. data/docs/basics/react-server-rendering.md +8 -5
  27. data/docs/basics/render-functions-and-railscontext.md +1 -1
  28. data/docs/basics/upgrading-react-on-rails.md +51 -14
  29. data/docs/basics/webpack-configuration.md +12 -18
  30. data/docs/misc/doctrine.md +0 -1
  31. data/docs/outdated/code-splitting.md +5 -5
  32. data/docs/tutorial.md +6 -0
  33. data/lib/generators/react_on_rails/base_generator.rb +8 -1
  34. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +4 -1
  35. data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
  36. data/lib/react_on_rails/configuration.rb +2 -0
  37. data/lib/react_on_rails/git_utils.rb +3 -3
  38. data/lib/react_on_rails/helper.rb +15 -14
  39. data/lib/react_on_rails/locales/to_js.rb +0 -4
  40. data/lib/react_on_rails/locales/to_json.rb +0 -4
  41. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +6 -6
  42. data/lib/react_on_rails/test_helper.rb +2 -0
  43. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
  44. data/lib/react_on_rails/utils.rb +8 -2
  45. data/lib/react_on_rails/version.rb +1 -1
  46. data/lib/react_on_rails/webpacker_utils.rb +4 -4
  47. data/lib/tasks/assets.rake +21 -9
  48. data/package.json +1 -1
  49. data/rakelib/docker.rake +0 -5
  50. data/rakelib/examples.rake +1 -1
  51. data/rakelib/lint.rake +4 -10
  52. data/rakelib/release.rake +9 -13
  53. data/rakelib/run_rspec.rake +9 -10
  54. data/rakelib/task_helpers.rb +2 -3
  55. data/react_on_rails.gemspec +3 -17
  56. metadata +15 -192
  57. data/docs/additional-reading/webpack-dev-server.md +0 -15
  58. data/docs/basics/recommended-project-structure.md +0 -77
  59. data/ruby-lint.yml +0 -25
@@ -62,7 +62,7 @@ cd react_on_rails/
62
62
  yarn
63
63
 
64
64
  # Update the lib directory with babel compiled files
65
- yarn run build:watch
65
+ yarn run build-watch
66
66
  ```
67
67
 
68
68
  You need to do this once:
data/Gemfile CHANGED
@@ -5,36 +5,4 @@ source "https://rubygems.org"
5
5
  # Specify your gem"s dependencies in react_on_rails.gemspec
6
6
  gemspec
7
7
 
8
- # The following gems are dependencies of the gem's dummy/example apps, not the gem itself.
9
- # They must be defined here because of the way Travis CI works, in that it will only
10
- # bundle install from a single Gemfile. Therefore, all gems that we will need for any dummy/example
11
- # app have to be manually added to this file.
12
- gem "bootsnap", ">= 1.1.0", require: false
13
- gem "bootstrap-sass"
14
- gem "jbuilder"
15
- gem "jquery-rails"
16
- gem "mini_racer"
17
- gem "puma"
18
- gem "rails_12factor"
19
- gem "ruby-lint", require: false
20
- gem "sass-rails"
21
- gem "scss_lint", require: false
22
- gem "sdoc", group: :doc
23
- gem "spring"
24
- gem "sprockets", "~>3.0"
25
- gem "sqlite3", "~> 1.3.6"
26
- gem "turbolinks"
27
- gem "uglifier"
28
- gem "web-console", group: :development
29
-
30
- # below are copied from spec/dummy/Gemfile
31
- gem "capybara"
32
- gem "capybara-screenshot"
33
- gem "launchy"
34
- gem "rspec-rails"
35
- gem "rspec-retry"
36
- gem "selenium-webdriver"
37
- gem "webpacker", ">= 4.0.0"
38
-
39
- gem "equivalent-xml", github: "mbklein/equivalent-xml"
40
- gem "rainbow"
8
+ eval_gemfile File.expand_path("./Gemfile.development_dependencies", __dir__)
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ # We'll revert this before the final merge.
5
+ # gem "webpacker", git: 'https://github.com/rails/webpacker.git'
6
+ gem "webpacker"
7
+ gem "bootsnap", ">= 1.1.0", require: false
8
+ gem "rails"
9
+ gem "sqlite3"
10
+ gem "sass-rails"
11
+ gem "uglifier"
12
+ gem "jquery-rails"
13
+ gem "puma"
14
+
15
+ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
16
+ gem "turbolinks" if ENV["DISABLE_TURBOLINKS"].nil? || ENV["DISABLE_TURBOLINKS"].strip.empty?
17
+
18
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
19
+ gem "jbuilder"
20
+ # bundle exec rake doc:rails generates the API under doc/api.
21
+ gem "sdoc", group: :doc
22
+
23
+ gem "sprockets"
24
+
25
+ # Use Capistrano for deployment
26
+ # gem 'capistrano-rails', group: :development
27
+ gem "awesome_print"
28
+ gem "mini_racer"
29
+
30
+ group :development, :test do
31
+ gem "listen"
32
+ gem "pry"
33
+ gem "pry-byebug"
34
+ gem "pry-doc"
35
+ gem "pry-rails"
36
+ gem "pry-rescue"
37
+ gem "rubocop", "1.2.0", require: false
38
+ gem "scss_lint", require: false
39
+ gem "spring"
40
+ end
41
+
42
+ group :test do
43
+ gem "capybara"
44
+ gem "capybara-screenshot"
45
+ gem "coveralls", require: false
46
+ gem "equivalent-xml"
47
+ gem "generator_spec"
48
+ gem "launchy"
49
+ gem "rspec_junit_formatter"
50
+ gem "rspec-rails"
51
+ gem "rspec-retry"
52
+ gem "selenium-webdriver"
53
+ gem "webdrivers", "~> 3.0"
54
+ end
data/NEWS.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  *We'll keep a history of the news. A few bullets at the top will also show on the [README.md](./README.md).*
4
4
 
5
+ * July 8, 2020: Release **v12**.
6
+ 1. **React Hooks Support** for top level components
7
+ 2. **Typescript bindings**
8
+ 3. **rails/webpacker** "just works" with React on Rails by default.
9
+ 4. i18n support for generating a JSON file rather than a JS file.
5
10
  * 2018-02-27: **Version 10.1.2** Supports the React API for ReactDOM.hydrate.
6
11
  * 2017-09-06: **VERSION 9.0.0 shipped!** This version depends on Webpacker directly. See [Upgrading React on Rails](./docs/basics/upgrading-react-on-rails.md) for more concise instructions on upgrading.
7
12
  * Always see the [CHANGELOG.md](./CHANGELOG.md) for the latest project changes.
data/README.md CHANGED
@@ -11,35 +11,44 @@
11
11
 
12
12
  [![License](https://img.shields.io/badge/license-mit-green.svg)](./LICENSE.md) [![Build Status](https://travis-ci.org/shakacode/react_on_rails.svg?branch=master)](https://travis-ci.org/shakacode/react_on_rails) [![Gem Version](https://badge.fury.io/rb/react_on_rails.svg)](https://badge.fury.io/rb/react_on_rails) [![npm version](https://badge.fury.io/js/react-on-rails.svg)](https://badge.fury.io/js/react-on-rails) [![Code Climate](https://codeclimate.com/github/shakacode/react_on_rails/badges/gpa.svg)](https://codeclimate.com/github/shakacode/react_on_rails) [![Coverage Status](https://coveralls.io/repos/shakacode/react_on_rails/badge.svg?branch=master&service=github)](https://coveralls.io/github/shakacode/react_on_rails?branch=master) [![](https://ruby-gem-downloads-badge.herokuapp.com/react_on_rails?type=total)](https://rubygems.org/gems/react_on_rails)
13
13
 
14
- *These are the docs for React on Rails 12, coming soon. To see the version 11 docs, [click here](https://github.com/shakacode/react_on_rails/tree/11.3.0).*
14
+ *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).*
15
15
 
16
16
  #### News
17
- **June 24, 2020**:
18
- 1. Gem version: 12.0.0.pre.beta.3 and npm version 12.0.0-beta.3 released! Please try this out!
19
- Major enhancements:
20
- 1. Proper support for React Hooks
21
- 2. Typescript bindings
22
- 3. i18n support for generating a JSON file rather than a JS file.
23
- 4. Tighter rails/webpacker integration
17
+
18
+ **October 14, 2020**: [RUBY ROGUES
19
+ 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/).
20
+
21
+ **October 1, 2020**: See the [spec/dummy](./spec/dummy) example repo for a simple configuration of webpack via the rails/webpacker gem
22
+ that supports SSR.
23
+
24
+ **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`.
25
+
26
+ **July 8, 2020**: Release v12 of React on Rails.
27
+
28
+ **Major Improvements**
29
+
30
+ 1. **React Hooks Support** for top level components
31
+ 2. **Typescript bindings**
32
+ 3. **rails/webpacker** "just works" with React on Rails by default.
33
+ 4. i18n support for generating a JSON file rather than a JS file.
24
34
 
25
- Be sure to see the [CHANGELOG.md](./CHANGELOG.md) and read the upgrade instructions:
26
- [docs/basics/upgrading-react-on-rails](./docs/basics/upgrading-react-on-rails.md#upgrading-to-v12).
35
+ Be sure to see the [CHANGELOG.md](./CHANGELOG.md) and read the upgrade instructions:
36
+ [docs/basics/upgrading-react-on-rails](./docs/basics/upgrading-react-on-rails.md#upgrading-to-v12).
27
37
 
28
- 2. See Justin's RailsConf talk: [Webpacker, It-Just-Works, But How?](http://railsconf.com/2020/video/justin-gordon-webpacker-it-just-works-but-how).
29
-
30
- **April 2, 2020**:
31
- * Are you interested in support for React on Rails? If so check out [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
38
+ * See Justin's RailsConf talk: [Webpacker, It-Just-Works, But How?](http://railsconf.com/2020/video/justin-gordon-webpacker-it-just-works-but-how).
39
+ * 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).
32
40
  * HMR is working with [Loadable Components](https://loadable-components.com) for a both amazing hot-reloading developer experience and great runtime performance. Please [email me](mailto:justin@shakacode.com) if you'd like to use [Loadable Components Code Splitting](https://loadable-components.com/docs/code-splitting/) to speed up your app by reducing your bundle sizes and lazily loading the code that's needed.
33
- * `react_on_rails` fully supports `rails/webpacker`. The example test app in `spec/dummy` was recently converted over to use rails/webpacker v4. It's a good example of how to leverage rails/webpacker's webpack configuration.
34
-
35
41
  ---
36
42
 
37
43
  #### About
38
44
  React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework.
39
45
 
40
- This project is sponsored by the software consulting firm [ShakaCode](https://www.shakacode.com). We focus on React front-ends, often with Ruby on Rails or Gatsby. The best way to see what we do is to see the details of [our recent work](https://www.shakacode.com/recent-work).
46
+ 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.
41
47
 
42
- Interested in optimizing your webpack setup for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable with server side rendering? We just did this for Popmenu, [lowering Heroku costs 20-25% while getting a 73% decrease in average response times](https://www.shakacode.com/recent-work/popmenu/).
48
+ Interested in optimizing your webpack setup for React on Rails including code
49
+ splitting with [react-router](https://github.com/ReactTraining/react-router#readme),
50
+ and [loadable-components](https://loadable-components.com/) with server-side rendering?
51
+ 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/).
43
52
 
44
53
  Feel free to contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakacode.com), maintainer of React on Rails, for more information.
45
54
 
@@ -49,18 +58,18 @@ Feel free to contact Justin Gordon, [justin@shakacode.com](mailto:justin@shakaco
49
58
 
50
59
  ## Project Objective
51
60
 
52
- To provide a high performance framework for integrating Ruby on Rails with React via the [**Webpacker**](https://github.com/rails/webpacker) gem especially in regards to React Server Rendering.
61
+ 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.
53
62
 
54
63
  ## Features and Why React on Rails?
55
64
 
56
- Given that rails/webpacker gem already provides basic React integration, why would you use "React on Rails"?
65
+ Given that `rails/webpacker` gem already provides basic React integration, why would you use "React on Rails"?
57
66
 
58
- 1. The easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate request to your API.
67
+ 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.
59
68
  1. Tight integration with [rails/webpacker](https://github.com/rails/webpacker).
60
- 1. Server rendering, often used for SEO crawler indexing and UX performance, is not offered by rails/webpacker.
61
- 1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/reactjs/react-router) integration with server-side-rendering.
69
+ 1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance, is not offered by `rails/webpacker`.
70
+ 1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration with server-side-rendering.
62
71
  1. [Internationalization (I18n) and (localization)](https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md)
63
- 1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com/).
72
+ 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/).
64
73
  1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example).
65
74
 
66
75
  See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code.
@@ -141,7 +150,7 @@ issue.
141
150
  * Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](docs/basics/configuration.md) for documentation of all configuration options.
142
151
  * Configure `config/webpacker.yml`. If you used the generator and the default webpacker setup, you don't need to touch this file. If you are customizing your setup, then consult the [spec/dummy/config/webpacker.yml](./spec/dummy/config/webpacker.yml) example or the official default [webpacker.yml](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml).
143
152
  * 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.
144
- * Your `public_output_path` must match your Webpack configuration for `output` of your bundles.
153
+ * Your `public_output_path` must match your custom Webpack configuration for `output` of your bundles.
145
154
  * Only set `cache_manifest` to `true` in your production env.
146
155
 
147
156
  ## Including your React Component on your Rails Views
@@ -166,7 +175,7 @@ issue.
166
175
  ReactOnRails.register({ HelloWorld });
167
176
  ```
168
177
 
169
- 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.
178
+ 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.
170
179
 
171
180
  - `@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:
172
181
 
@@ -175,7 +184,7 @@ issue.
175
184
  <%= react_component("HelloWorld", props: { name: "Stranger" }) %>
176
185
  ```
177
186
 
178
- - This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://reactjs.org/docs/context.html). See [render functions and the RailsContext](docs/basics/render-functions-and-railscontext.md) for more details on this topic.
187
+ - This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://reactjs.org/docs/context.html). See [Render-Functions and the RailsContext](docs/basics/render-functions-and-railscontext.md) for more details on this topic.
179
188
 
180
189
  ```js
181
190
  import React from 'react';
@@ -193,32 +202,6 @@ issue.
193
202
 
194
203
  See the [View Helpers API](./docs/api/view-helpers-api.md) for more details on `react_component` and its sibling function `react_component_hash`.
195
204
 
196
- ## Fragment Caching
197
-
198
- Fragment caching is a [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro) feature. Fragment caching is a **HUGE** performance booster for your apps. Use the `cached_react_component` and `cached_react_component_hash`. The API is the same as `react_component` and `react_component_hash`, but for 2 differences:
199
-
200
- 1. The `cache_key` takes the same parameters as any Rails `cache` view helper.
201
- 1. The **props** are passed via a block so that evaluation of the props is not done unless the cache is broken. Suppose you put your props calculation into some method called `some_slow_method_that_returns_props`:
202
-
203
- ```ruby
204
- <%= cached_react_component("App", cache_key: [@user, @post], prerender: true) do
205
- some_slow_method_that_returns_props
206
- end %>
207
- ```
208
-
209
- Such fragment caching saves a ton of CPU work for your web server and greatly reduces the request time. It completely skips the evaluation costs of:
210
-
211
- 1. Database calls to compute the props.
212
- 2. Serialization the props values hash into a JSON string for evaluating JavaScript to server render.
213
- 3. Costs associated with evaluating JavaScript from your Ruby code.
214
- 4. Creating the HTML string containing the props and the server-rendered JavaScript code.
215
-
216
- Note, even without server rendering (without step 3 above), fragment caching is still effective.
217
-
218
- ## Integration with Node.js for Server Rendering
219
-
220
- Default server rendering is done by ExecJS. If you want to use a Node.js server for better performing server rendering, [email justin@shakacode.com](mailto:justin@shakacode.com). ShakaCode has built a premium Node rendering server that is part of [React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
221
-
222
205
  ## Globally Exposing Your React Components
223
206
 
224
207
  For the React on Rails view helper `react_component` to use your React components, you will have to **register** them in your JavaScript code.
@@ -246,15 +229,15 @@ For details on techniques to use different code for client and server rendering,
246
229
 
247
230
  You have two ways to specify your React components. You can either register the React component (either function or class component) directly, or you can create a function that returns a React component, which we using the name of a "render-function". Creating a render-function allows:
248
231
 
249
- 1. You have access to the `railsContext`. See documentation for the railsContext in terms of why you might need it. You **need** a render function to access the `railsContext`.
232
+ 1. You to have access to the `railsContext`. See [documentation for the railsContext](./docs/basics/render-functions-and-railscontext.md) in terms of why you might need it. You **need** a Render-Function to access the `railsContext`.
250
233
  2. You can use the passed-in props to initialize a redux store or set up react-router.
251
234
  3. You can return different components depending on what's in the props.
252
235
 
253
- Note, the return value of a **render function** should be either a React Function or Class Component, or an object representing server rendering results.
236
+ Note, the return value of a **Render-Function** should be either a React Function or Class Component, or an object representing server rendering results.
254
237
 
255
238
  Do not return a React Element (JSX).
256
239
 
257
- ReactOnRails will automatically detect a registered render function by the fact that the function takes
240
+ ReactOnRails will automatically detect a registered Render-Function by the fact that the function takes
258
241
  more than 1 parameter. In other words, if you want the ability to provide a function that returns the
259
242
  React component, then you need to specify at least a second parameter. This is the `railsContext`.
260
243
  If you're not using this parameter, declare your function with the unused param:
@@ -268,13 +251,13 @@ const MyComponentGenerator = (props, _railsContext) => {
268
251
  }
269
252
  ```
270
253
 
271
- Thus, there is no difference between registering a React function or class Component versus a "render function."
254
+ Thus, there is no difference between registering a React Function Component or class Component versus a "Render-Function." Just call `ReactOnRails.register`.
272
255
 
273
- ## react_component_hash for render functions
256
+ ## react_component_hash for Render-Functions
274
257
 
275
- Another reason to use a render function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: { renderedHtml, redirectLocation, error }. Make sure you use this function with `react_component_hash`.
258
+ Another reason to use a Render-Function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: { renderedHtml, redirectLocation, error }. Make sure you use this function with `react_component_hash`.
276
259
 
277
- For server rendering, if you wish to return multiple HTML strings from a render function, you may return an Object from your render function with a single top-level property of `renderedHtml`. Inside this Object, place a key called `componentHtml`, along with any other needed keys. An example scenario of this is when you are using side effects libraries like [React Helmet](https://github.com/nfl/react-helmet). Your Ruby code will get this Object as a Hash containing keys componentHtml and any other custom keys that you added:
260
+ 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:
278
261
 
279
262
  ```js
280
263
  { renderedHtml: { componentHtml, customKey1, customKey2} }
@@ -299,7 +282,6 @@ Browse the links in the [Summary Table of Contents](./SUMMARY.md)
299
282
  Here are some highly recommended next articles to read:
300
283
 
301
284
  1. [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
302
- 1. [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
303
285
  1. [Webpack Configuration](./docs/basics/webpack-configuration.md)
304
286
  1. [View Helpers API](./docs/api/view-helpers-api.md)
305
287
  1. [Caching and Performance: React on Rails Pro](https://www.shakacode.com/react-on-rails-pro).
@@ -336,16 +318,42 @@ React on Rails Pro provides Node server rendering, fragment caching, code-splitt
336
318
 
337
319
  The [React on Rails Pro Support Plan](https://www.shakacode.com/react-on-rails-pro) can help!
338
320
 
339
- * Optimizing your webpack setup to Webpack v4 for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable.
340
- * Upgrading your app to use the current Webpack setup that skips the Sprockets asset pipeline.
321
+ * Optimizing your webpack setup to the latest Webpack for React on Rails including code splitting with loadable-components.
322
+ * Upgrading your app to use the current `rails/webpacker` setup that skips the Sprockets asset pipeline.
341
323
  * Better performance client and server side.
342
- * Best practices based on over four years of React on Rails experience.
324
+ * Best practices based on over 6 years of React on Rails experience on many production projects.
343
325
  * Using [Reason](https://reasonml.github.io/) with (or without) React on Rails.
344
326
 
345
- ShakaCode can also help you with your custom software development needs. We specialize in marketplace and e-commerce applications that utilize both Rails and React. Because we own [HawaiiChee.com](https://www.hawaiichee.com), we can leverage that code for your app!
327
+ 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!
346
328
 
347
329
  Please email Justin Gordon [justin@shakacode.com](mailto:justin@shakacode.com), the maintainer of React on Rails, for more information.
348
330
 
331
+ ### Fragment Caching
332
+
333
+ 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:
334
+
335
+ 1. The `cache_key` takes the same parameters as any Rails `cache` view helper.
336
+ 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`:
337
+
338
+ ```ruby
339
+ <%= cached_react_component("App", cache_key: [@user, @post], prerender: true) do
340
+ some_slow_method_that_returns_props
341
+ end %>
342
+ ```
343
+
344
+ Such fragment caching saves CPU work for your web server and greatly reduces the request time. It completely skips the evaluation costs of:
345
+
346
+ 1. Database calls to compute the props.
347
+ 2. Serialization the props values hash into a JSON string for evaluating JavaScript to server render.
348
+ 3. Costs associated with evaluating JavaScript from your Ruby code.
349
+ 4. Creating the HTML string containing the props and the server-rendered JavaScript code.
350
+
351
+ Note, even without server rendering (without step 3 above), fragment caching is still effective.
352
+
353
+ ## Integration with Node.js for Server Rendering
354
+
355
+ 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).
356
+
349
357
  ## Testimonials for ShakaCode
350
358
  [HVMN Testimonial, by Paul Benigeri, October 12, 2018](./docs/testimonials/hvmn.md)
351
359
  > 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.
data/SUMMARY.md CHANGED
@@ -8,7 +8,6 @@
8
8
  + [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
9
9
  + [Client vs. Server Rendering](./docs/basics/client-vs-server-rendering.md)
10
10
  + [React Server Rendering](./docs/basics/react-server-rendering.md)
11
- + [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
12
11
  + [Render-Functions and the RailsContext](docs/basics/render-functions-and-railscontext.md)
13
12
  + [Caching and Performance: React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
14
13
  + [Deployment](docs/basics/deployment.md).
@@ -28,6 +27,7 @@
28
27
  + [Updating Dependencies](./docs/additional-reading/updating-dependencies.md)
29
28
  + [Manual Installation Overview](docs/outdated/manual-installation-overview.md)
30
29
  + [Upgrading from rails/webpacker v3 to v4](docs/additional-reading/upgrade-webpacker-v3-to-v4.md)
30
+ + [Recommended Project Structure](docs/additional-reading/recommended-project-structure.md)
31
31
 
32
32
  ## **Rails**
33
33
  + [Rails Engine Integration](./docs/additional-reading/rails-engine-integration.md)
@@ -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#L14-L48
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
@@ -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.
@@ -0,0 +1,69 @@
1
+ # Recommended Project structure
2
+
3
+ The React on Rails generator uses the standard `rails/webpacker` convention of this structure:
4
+
5
+ ```yml
6
+ app/javascript:
7
+ ├── bundles:
8
+ │ # Logical groups of files that can be used for code splitting
9
+ │ └── hello-world-bundle.js
10
+ ├── packs:
11
+ │ # only webpack entry files here
12
+ │ └── hello-world-bundle.js
13
+ ```
14
+
15
+ Per the example repo [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh),
16
+ you should consider keeping your codebase mostly consistent with the defaults for [rails/webpacker](https://github.com/rails/webpacker).
17
+
18
+ ## Steps to convert from the generator defaults to use a `/client` directory for source code
19
+
20
+ 1. Move the directory:
21
+
22
+ ```
23
+ mv app/javascript client
24
+ ```
25
+
26
+ 2. Edit your `/config/webpacker.yml` file. Change the `default/source_path`:
27
+
28
+ ```yml
29
+ source_path: client
30
+ ```
31
+
32
+ ## Moving node_modules from `/` to `/client` with a custom webpack setup.
33
+
34
+ `rails/webpacker` probably doesn't support having your main node_modules directory under `/client`, so only follow these steps if you want to use your own webpack configuration.
35
+
36
+ 1. Move the `/package.json` to `/client/package.json`
37
+ 2. Create a `/package.json` that delegates to `/client/package.json`.
38
+ ```
39
+ "scripts": {
40
+ "heroku-postbuild": "cd ./client && yarn"
41
+ },
42
+ ```
43
+ 3. If your node_modules directory is not at the top level of the Rails project, then you will need to set the
44
+ ENV value of WEBPACKER_CONFIG to the location of the `config/webpacker.yml` file per [rails/webpacker PR 2561](https://github.com/rails/webpacker/pull/2561).
45
+
46
+ ## CSS, Sass, Fonts, and Images
47
+ Should you move your styling assets to Webpack? Or stick with the plain Rails asset pipeline. It depends!
48
+
49
+ Here's a good discussion of this option: [Why does Rails 6 include both Webpacker and Sprockets?](https://rossta.net/blog/why-does-rails-install-both-webpacker-and-sprockets.html).
50
+
51
+ You have 2 basic choices:
52
+
53
+ ### Simple Rails Way
54
+ This isn't really any technique, as you keep handling all your styling assets using Rails standard tools, such as using the [sass-rails gem](https://rubygems.org/gems/sass-rails/versions/5.0.4). Basically, Webpack doesn't get involved with styling. Your Rails layouts just doing the styling the standard Rails way.
55
+
56
+ #### Advantages to the Simple Rails Way
57
+ 1. Much simpler! There's no changes really from your current processes.
58
+
59
+ ### Using Webpack to Manage Styling Assets
60
+ This technique involves customization of the webpack config files to generate CSS, image, and font assets.
61
+
62
+ #### Directory structure
63
+ 1. `/client/app/assets`: Assets for CSS for client app.
64
+ 1. `/client/app/assets/fonts` and `/client/app/assets/styles`: Globally shared assets for styling. Note, most Sass and image assets will be stored next to the JavaScript files.
65
+
66
+ #### Advantages to having Webpack Manage Styles
67
+ 1. You can use [CSS modules](https://github.com/css-modules/css-modules), which is super compelling once you seen the benefits.
68
+ 1. You can use CSS in JS.
69
+ 1. You can do hot reloading of your assets. Thus, you do not have to refresh your web page to see asset change, including changing styles.