react_on_rails 12.0.1 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -13
  3. data/.eslintrc +2 -1
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.github/workflows/lint-js-and-ruby.yml +53 -0
  7. data/.github/workflows/main.yml +178 -0
  8. data/.github/workflows/package-js-tests.yml +35 -0
  9. data/.github/workflows/rspec-package-specs.yml +45 -0
  10. data/.gitignore +0 -3
  11. data/.prettierignore +2 -0
  12. data/.prettierrc +0 -3
  13. data/.rubocop.yml +48 -39
  14. data/.travis.yml +3 -1
  15. data/CHANGELOG.md +112 -25
  16. data/CONTRIBUTING.md +4 -3
  17. data/Gemfile +1 -33
  18. data/Gemfile.development_dependencies +52 -0
  19. data/NEWS.md +14 -4
  20. data/REACT-ON-RAILS-PRO-LICENSE +1 -1
  21. data/README.md +42 -324
  22. data/Rakefile +1 -1
  23. data/SUMMARY.md +22 -13
  24. data/docs/{basics → additional-details}/generator-details.md +1 -6
  25. data/docs/{outdated → additional-details}/manual-installation-overview.md +6 -6
  26. data/docs/additional-details/recommended-project-structure.md +69 -0
  27. data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
  28. data/docs/api/javascript-api.md +2 -2
  29. data/docs/api/redux-store-api.md +3 -3
  30. data/docs/api/view-helpers-api.md +7 -8
  31. data/docs/contributor-info/linters.md +5 -6
  32. data/docs/contributor-info/pull-requests.md +2 -4
  33. data/docs/contributor-info/releasing.md +1 -1
  34. data/docs/{basics → deployment}/heroku-deployment.md +16 -5
  35. data/docs/{basics → guides}/client-vs-server-rendering.md +4 -2
  36. data/docs/{basics → guides}/configuration.md +65 -31
  37. data/docs/guides/deployment.md +4 -0
  38. data/docs/guides/getting-started.md +183 -0
  39. data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +104 -0
  40. data/docs/{outdated → guides}/how-react-on-rails-works.md +10 -10
  41. data/docs/guides/how-to-conditionally-server-render-based-on-device-type.md +39 -0
  42. data/docs/guides/how-to-use-different-files-for-client-and-server-rendering.md +98 -0
  43. data/docs/{basics → guides}/installation-into-an-existing-rails-app.md +14 -7
  44. data/docs/guides/rails-webpacker-react-integration-options.md +182 -0
  45. data/docs/guides/react-on-rails-overview.md +30 -0
  46. data/docs/{basics → guides}/react-server-rendering.md +8 -5
  47. data/docs/{basics → guides}/rspec-configuration.md +11 -11
  48. data/docs/{tutorial.md → guides/tutorial.md} +46 -50
  49. data/docs/{basics → guides}/upgrading-react-on-rails.md +56 -18
  50. data/docs/guides/webpack-configuration.md +42 -0
  51. data/docs/home.md +23 -0
  52. data/docs/{outdated → javascript}/code-splitting.md +5 -5
  53. data/docs/{additional-reading → javascript}/converting-from-custom-webpack-config-to-rails-webpacker-config.md +4 -4
  54. data/docs/{additional-reading → javascript}/images.md +5 -6
  55. data/docs/{articles.md → misc/articles.md} +1 -1
  56. data/docs/misc/doctrine.md +5 -6
  57. data/docs/outdated/deferred-rendering.md +39 -0
  58. data/docs/outdated/rails-assets-relative-paths.md +3 -3
  59. data/docs/outdated/rails-assets.md +8 -8
  60. data/docs/outdated/rails3.md +2 -2
  61. data/docs/{additional-reading → rails}/convert-rails-5-api-only-app.md +1 -1
  62. data/docs/{additional-reading → rails}/rails-engine-integration.md +3 -12
  63. data/docs/{additional-reading → rails}/turbolinks.md +13 -1
  64. data/docs/react-on-rails-pro/react-on-rails-pro.md +43 -0
  65. data/docs/testimonials/testimonials.md +6 -6
  66. data/lib/generators/react_on_rails/base_generator.rb +64 -12
  67. data/lib/generators/react_on_rails/dev_tests_generator.rb +1 -1
  68. data/lib/generators/react_on_rails/generator_helper.rb +4 -0
  69. data/lib/generators/react_on_rails/templates/.eslintrc +3 -1
  70. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -8
  71. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +9 -0
  72. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
  73. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css +4 -0
  74. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorldServer.js +5 -0
  75. data/lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js +8 -0
  76. data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +1 -0
  77. data/lib/generators/react_on_rails/templates/base/base/babel.config.js.tt +32 -0
  78. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +3 -3
  79. data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt +17 -0
  80. data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +15 -0
  81. data/lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt +25 -0
  82. data/lib/generators/react_on_rails/templates/base/base/config/webpack/production.js.tt +9 -0
  83. data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt +117 -0
  84. data/lib/generators/react_on_rails/templates/base/base/config/webpack/test.js.tt +6 -0
  85. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpack.config.js.tt +15 -0
  86. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpackConfig.js.tt +36 -0
  87. data/lib/generators/react_on_rails/templates/base/base/config/webpacker.yml +62 -0
  88. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +8 -2
  89. data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
  90. data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
  91. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +2 -1
  92. data/lib/react_on_rails/configuration.rb +42 -41
  93. data/lib/react_on_rails/git_utils.rb +3 -3
  94. data/lib/react_on_rails/helper.rb +54 -28
  95. data/lib/react_on_rails/json_parse_error.rb +1 -1
  96. data/lib/react_on_rails/locales/base.rb +11 -15
  97. data/lib/react_on_rails/locales/to_js.rb +0 -4
  98. data/lib/react_on_rails/locales/to_json.rb +0 -4
  99. data/lib/react_on_rails/prerender_error.rb +6 -3
  100. data/lib/react_on_rails/react_component/render_options.rb +30 -7
  101. data/lib/react_on_rails/server_rendering_js_code.rb +13 -0
  102. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +41 -13
  103. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +1 -1
  104. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +3 -3
  105. data/lib/react_on_rails/test_helper.rb +4 -2
  106. data/lib/react_on_rails/utils.rb +24 -4
  107. data/lib/react_on_rails/version.rb +1 -1
  108. data/lib/react_on_rails/webpacker_utils.rb +9 -10
  109. data/lib/tasks/assets.rake +19 -50
  110. data/lib/tasks/locale.rake +1 -2
  111. data/package.json +24 -29
  112. data/rakelib/docker.rake +0 -5
  113. data/rakelib/example_type.rb +10 -3
  114. data/rakelib/examples.rake +3 -5
  115. data/rakelib/lint.rake +3 -9
  116. data/rakelib/release.rake +35 -15
  117. data/rakelib/run_rspec.rake +10 -11
  118. data/rakelib/task_helpers.rb +16 -4
  119. data/react_on_rails.gemspec +5 -20
  120. data/yarn.lock +2616 -5190
  121. metadata +86 -256
  122. data/.release-it.json +0 -3
  123. data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +0 -10
  124. data/docs/additional-reading/webpack-dev-server.md +0 -15
  125. data/docs/basics/deployment.md +0 -4
  126. data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +0 -49
  127. data/docs/basics/recommended-project-structure.md +0 -77
  128. data/docs/basics/webpack-configuration.md +0 -50
  129. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +0 -26
  130. data/ruby-lint.yml +0 -25
  131. /data/docs/{basics → additional-details}/migrating-from-react-rails.md +0 -0
  132. /data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
  133. /data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
  134. /data/docs/{basics → guides}/i18n.md +0 -0
  135. /data/docs/{basics → guides}/minitest-configuration.md +0 -0
  136. /data/docs/{basics → guides}/render-functions-and-railscontext.md +0 -0
  137. /data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
  138. /data/docs/{additional-reading → javascript}/asset-pipeline.md +0 -0
  139. /data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
  140. /data/docs/{additional-reading → javascript}/credits.md +0 -0
  141. /data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
  142. /data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
  143. /data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
  144. /data/docs/{additional-reading → javascript}/react-helmet.md +0 -0
  145. /data/docs/{additional-reading → javascript}/react-router.md +0 -0
  146. /data/docs/{additional-reading → javascript}/server-rendering-tips.md +0 -0
  147. /data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
  148. /data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
  149. /data/docs/{additional-reading → javascript}/webpack.md +0 -0
  150. /data/docs/{coding-style → misc}/style.md +0 -0
  151. /data/docs/{additional-reading → misc}/tips.md +0 -0
  152. /data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +0 -0
@@ -0,0 +1,183 @@
1
+ # Getting Started
2
+
3
+ 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.
4
+
5
+ 1. Do the quick [tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/).
6
+ 2. Add React on Rails to an existing Rails app per [the instructions](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).
7
+ 3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy), a simple, no DB example.
8
+ 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://reactrails.com).
9
+
10
+ ## Basic Installation
11
+
12
+ *See also [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).*
13
+
14
+ 1. Add the `react_on_rails` gem to Gemfile:
15
+
16
+ ```bash
17
+ bundle add react_on_rails --strict
18
+ ```
19
+
20
+ 2. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
21
+
22
+ 3. Run the generator:
23
+
24
+ ```bash
25
+ rails generate react_on_rails:install
26
+ ```
27
+
28
+ 4. Start the app:
29
+
30
+ ```bash
31
+ rails s
32
+ ```
33
+
34
+ 5. Visit http://localhost:3000/hello_world.
35
+
36
+
37
+ ### Turning on server rendering
38
+
39
+ With the code from running the React on Rails generator above:
40
+
41
+ 1. Edit `app/views/hello_world/index.html.erb` and set the `prerender` option to `true`.
42
+ 2. Refresh the page.
43
+
44
+ Below is the line where you turn server rendering on by setting `prerender` to true:
45
+
46
+ ```erb
47
+ <%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
48
+ ```
49
+
50
+ Note, if you got an error in your console regarding "ReferenceError: window is not defined",
51
+ then you need to edit `config/webpacker.yml` and set `hmr: false` and `inline: false`.
52
+ See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this
53
+ issue.
54
+
55
+ ## Basic Usage
56
+
57
+ ### Configuration
58
+
59
+ * 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/guides/configuration/) for documentation of all configuration options.
60
+ * 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).
61
+ * Most apps should rely on the rails/webpacker setup for Webpack. v6 of rails/webpacker includes support for v5 of webpack.
62
+
63
+ ## Including your React Component on your Rails Views
64
+
65
+ - React component are rendered via your Rails Views. Here's an ERB sample:
66
+
67
+ ```erb
68
+ <%= react_component("HelloWorld", props: @some_props) %>
69
+ ```
70
+
71
+ - **Server-Side Rendering**: Your react component is first rendered into HTML on the server. Use the **prerender** option:
72
+
73
+ ```erb
74
+ <%= react_component("HelloWorld", props: @some_props, prerender: true) %>
75
+ ```
76
+
77
+ - 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:
78
+
79
+ ```js
80
+ import ReactOnRails from 'react-on-rails';
81
+ import HelloWorld from './HelloWorld';
82
+ ReactOnRails.register({ HelloWorld });
83
+ ```
84
+
85
+ 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.
86
+
87
+ - `@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:
88
+
89
+ ```ruby
90
+ # Rails View
91
+ <%= react_component("HelloWorld", props: { name: "Stranger" }) %>
92
+ ```
93
+
94
+ - 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/guides/render-functions-and-railscontext/) for more details on this topic.
95
+
96
+ ```js
97
+ import React from 'react';
98
+
99
+ export default (props, railsContext) => {
100
+ // Note wrap in a function to make this a React function component
101
+ return () => (
102
+ <div>
103
+ Your locale is {railsContext.i18nLocale}.<br/>
104
+ Hello, {props.name}!
105
+ </div>
106
+ );
107
+ };
108
+ ```
109
+
110
+ 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`.
111
+
112
+ ## Globally Exposing Your React Components
113
+
114
+ For the React on Rails view helper `react_component` to use your React components, you will have to **register** them in your JavaScript code.
115
+
116
+ 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**.
117
+
118
+ This is how to expose a component to the `react_component` view helper.
119
+
120
+ ```javascript
121
+ // app/javascript/packs/hello-world-bundle.js
122
+ import HelloWorld from '../components/HelloWorld';
123
+ import ReactOnRails from 'react-on-rails';
124
+ ReactOnRails.register({ HelloWorld });
125
+ ```
126
+
127
+ #### Different Server-Side Rendering Code (and a Server-Specific Bundle)
128
+
129
+ 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() }`.
130
+
131
+ 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.
132
+
133
+ 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._)
134
+
135
+ ## Specifying Your React Components: Register directly or use render-functions
136
+
137
+ 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:
138
+
139
+ 1. You to have access to the `railsContext`. See [documentation for the railsContext](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) in terms of why you might need it. You **need** a Render-Function to access the `railsContext`.
140
+ 2. You can use the passed-in props to initialize a redux store or set up react-router.
141
+ 3. You can return different components depending on what's in the props.
142
+
143
+ Note, the return value of a **Render-Function** should be either a React Function or Class Component, or an object representing server rendering results.
144
+
145
+ **Do not return a React Element (JSX).**
146
+
147
+ ReactOnRails will automatically detect a registered Render-Function by the fact that the function takes
148
+ more than 1 parameter. In other words, if you want the ability to provide a function that returns the
149
+ React component, then you need to specify at least a second parameter. This is the `railsContext`.
150
+ If you're not using this parameter, declare your function with the unused param:
151
+
152
+ ```js
153
+ const MyComponentGenerator = (props, _railsContext) => {
154
+ if (props.print) {
155
+ // This is a React FunctionComponent because it is wrapped in a function.
156
+ return () => <H1>{JSON.stringify(props)}</H1>;
157
+ }
158
+ }
159
+ ```
160
+
161
+ Thus, there is no difference between registering a React Function Component or class Component versus a "Render-Function." Just call `ReactOnRails.register`.
162
+
163
+ ## react_component_hash for Render-Functions
164
+
165
+ 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`.
166
+
167
+ 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:
168
+
169
+ ```js
170
+ { renderedHtml: { componentHtml, customKey1, customKey2} }
171
+ ```
172
+
173
+ 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/).
174
+
175
+ ## Error Handling
176
+
177
+ * All errors from ReactOnRails will be of type ReactOnRails::Error.
178
+ * Prerendering (server rendering) errors get context information for HoneyBadger and Sentry for easier debugging.
179
+
180
+ ## I18n
181
+
182
+ React on Rails provides an option for automatic conversions of Rails `*.yml` locale files into `*.json` or `*.js*.
183
+ See the [How to add I18n](https://www.shakacode.com/react-on-rails/docs/guides/i18n/) for a summary of adding I18n.
@@ -0,0 +1,104 @@
1
+ # HMR and Hot Reloading with the webpack-dev-server
2
+
3
+ The webpack-dev-server provides:
4
+
5
+ 1. Speedy compilation of client side assets
6
+ 2. Optional HMR which means that the page will reload automatically when after
7
+ compilation completes. Note, some developers do not like this, as you'll
8
+ abruptly lose any tweaks within the Chrome development tools.
9
+ 3. Optional hot-reloading. The older react-hot-loader has been deprecated in
10
+ favor of [fast-refresh](https://reactnative.dev/docs/fast-refresh).
11
+ For use with webpack, see **Client Side rendering and HMR using react-refresh-webpack-plugin** section bellow or visit [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) for additional details.
12
+
13
+ If you are ***not*** using server-side rendering (***not*** using `prerender: true`),
14
+ then you can follow all the regular docs for using the `bin/webpacker-dev-server`
15
+ during development.
16
+
17
+ # Server Side Rendering with the Default rails/webpacker bin/webpacker-dev-server
18
+
19
+ If you are using server-side rendering, then you have a couple options. The
20
+ recommended technique is to have a different webpack configuration for server
21
+ rendering.
22
+
23
+ ## If you use the same Webpack setup for your server and client bundles
24
+ If you do use the webpack-dev-server for prerendering, be sure to set the
25
+ `config/initializers/react_on_rails.rb` setting of
26
+
27
+ ```
28
+ config.same_bundle_for_client_and_server = true
29
+ ```
30
+
31
+ `dev_server.hmr` maps to [devServer.hot](https://webpack.js.org/configuration/dev-server/#devserverhot).
32
+ This must be false if you're using the webpack-dev-server for client and server bundles.
33
+
34
+ `dev_server.inline` maps to [devServer.inline](https://webpack.js.org/configuration/dev-server/#devserverinline).
35
+ This must also be false.
36
+
37
+ If you don't configure these two to false, you'll see errors like:
38
+
39
+ * "ReferenceError: window is not defined" (if hmr is true)
40
+ * "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
41
+
42
+ # Client Side rendering with HMR using react-refresh-webpack-plugin
43
+ ## Basic installation
44
+ To enable HMR functionality you have to use `./bin/webpacker-dev-server`
45
+ 1. In `config/webpacker.yml` set **hmr** and **inline** `dev_server` properties to true.
46
+ ```
47
+ dev_server:
48
+ https: false
49
+ host: localhost
50
+ port: 3035
51
+ public: localhost:3035
52
+ hmr: true
53
+ # Inline should be set to true if using HMR
54
+ inline: true
55
+ ```
56
+
57
+ 2. Add react refresh packages:
58
+ ` yarn add @pmmmwh/react-refresh-webpack-plugin react-refresh -D`
59
+
60
+ 3. HMR is for use with the webpack-dev-server, so we only add this for the webpack-dev-server.
61
+ ```
62
+ const { devServer } = require('shakapacker')
63
+
64
+ const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER
65
+
66
+ //plugins
67
+ if (isWebpackDevServer) {
68
+ environment.plugins.append(
69
+ 'ReactRefreshWebpackPlugin',
70
+ new ReactRefreshWebpackPlugin({
71
+ overlay: {
72
+ sockPort: devServer.port
73
+ }
74
+ })
75
+ )
76
+ }
77
+ ```
78
+ We added overlay.sockedPort option in `ReactRefreshWebpackPlugin` to match the webpack dev-server port specified in config/webpacker.yml. Thats way we make sockjs works properly and suppress error in browser console `GET http://localhost:[port]/sockjs-node/info?t=[xxxxxxxxxx] 404 (Not Found)`.
79
+
80
+ 4. Add react-refresh plugin in `babel.config.js`
81
+ ```
82
+ module.export = function(api) {
83
+ return {
84
+ plugins: [process.env.WEBPACK_DEV_SERVER && 'react-refresh/babel'].filter(Boolean)
85
+ }
86
+ }
87
+ ```
88
+ That's it :).
89
+ Now Browser should reflect .js along with .css changes without reloading.
90
+
91
+ If by some reason plugin doesn't work you could revert changes and left only devServer hmr/inline to true affecting only css files.
92
+
93
+ These plugins are working and tested with
94
+ - babel 7
95
+ - webpacker 5
96
+ - bootstrap 4
97
+ - jest 26
98
+ - core-js 3
99
+ - node 12.10.0
100
+ - react-refresh-webpack-plugin@0.4.1
101
+ - react-refresh 0.8.3
102
+ - react_on_rails 11.1.4
103
+
104
+ configuration.
@@ -1,21 +1,21 @@
1
1
  # How React on Rails Works (with rails/webpacker)
2
2
 
3
- *Note, older versions of React on Rails pushed the Webpack bundles through the Asset Pipeline. This older method has *many* disadvantages, such as broken sourcemaps, performance issues, etc. If you need help migrating to the current way of bypassing the Asset Pipeline, [email Justin](mailto:justin@shakacode.com).*
3
+ *Note, older versions of React on Rails pushed the Webpack bundles through the Asset Pipeline. This older method has *many* disadvantages, such as broken sourcemaps, performance issues, etc. If you need help migrating to the current way of bypassing the Asset Pipeline, [email Justin](mailto:justin@shakacode.com).*
4
4
 
5
- Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [rails/webpacker](https://github.com/rails/webpacker) provides view helpers to access the Webpack generated (and fingerprinted) JS and CSS. These files totally skip the Rails asset pipeline. You are responsible for properly configuring your Webpack output. You will either use the standard Webpack configuration (*recommended*) or the `rails/webpacker` setup for Webpack.
5
+ Webpack is used to generate JavaScript and CSS "bundles" directly to your `/public` directory. [rails/webpacker](https://github.com/rails/webpacker) provides view helpers to access the Webpack generated (and fingerprinted) JS and CSS. These files totally skip the Rails asset pipeline. You are responsible for properly configuring your Webpack output. You will either use the standard Webpack configuration (*recommended*) or the `rails/webpacker` setup for Webpack.
6
6
 
7
7
  Ensure these generated bundle files are in your `.gitignore`, as you never want to add the large compiled bundles to git.
8
8
 
9
- Inside your Rails views, you can now use the `react_component` helper method provided by React on Rails. You can pass props directly to the react component helper.
9
+ Inside your Rails views, you can now use the `react_component` helper method provided by React on Rails. You can pass props directly to the react component helper.
10
10
 
11
- Optionally, you can also initialize a Redux store with the view or controller helper `redux_store` so that the redux store can be shared amongst multiple React components.
11
+ Optionally, you can also initialize a Redux store with the view or controller helper `redux_store` so that the redux store can be shared amongst multiple React components.
12
12
 
13
13
  ## Client-Side Rendering vs. Server-Side Rendering
14
14
 
15
15
  In most cases, you should use the `prerender: false` (default behavior) with the provided `react_component` helper method to render the React component from your Rails views. In some cases, such as when SEO is vital, or many users will not have JavaScript enabled, you can enable server-rendering by passing `prerender: true` to your helper, or you can simply change the default in `config/initializers/react_on_rails`.
16
16
 
17
17
  Now the server will interpret your JavaScript. The default is to use [ExecJS](https://github.com/rails/execjs) and pass the resulting HTML to the client. If you want to maximize the perfomance of your server rendering, then you want to use React on Rails Pro which uses NodeJS to do the server rendering. See the [docs for React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
18
-
18
+
19
19
  ## HTML Source Code
20
20
 
21
21
  If you open the HTML source of any web page using React on Rails, you'll see the 3 parts of React on Rails rendering:
@@ -28,17 +28,17 @@ You can see all this on the source for [reactrails.com](https://www.reactrails.c
28
28
 
29
29
  ## Building the Bundles
30
30
 
31
- Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes. Alternately, the `rails/webpacker` library also can ensure that your bundles are built.
31
+ Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included example Foreman `Procfile.dev` files will take care of this for you by starting a webpack process with the watch flag. This will watch your JavaScript code files for changes. Alternately, the `rails/webpacker` library also can ensure that your bundles are built.
32
32
 
33
- For example, you might create a [Procfile.dev](spec/dummy/Procfile.dev).
33
+ For example, you might create a [Procfile.dev](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/Procfile.dev).
34
34
 
35
- On production deployments that use asset precompilation, such as Heroku deployments, `rails/webpacker`, by default, will automatically run webpack to build your JavaScript bundles, running the command `bin/webpack` in your app.
35
+ On production deployments that use asset precompilation, such as Heroku deployments, `rails/webpacker`, by default, will automatically run webpack to build your JavaScript bundles, running the command `bin/webpacker` in your app.
36
36
 
37
37
  However, if you want to run a custom command to run webpack to build your bundles, then you will:
38
- 1. Ensure you do not have a `config/webpack/production.js` file
39
- 1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](docs/basics/configuration.md)
38
+ 1. Define `config.build_production_command` in your [config/initializers/react_on_rails.rb](https://www.shakacode.com/react-on-rails/docs/guides/configuration/)
40
39
 
41
40
  Then React on Rails modifies the `assets:precompile` task to run your `build_production_command`.
42
41
 
43
42
  If you have used the provided generator, these bundles will automatically be added to your `.gitignore` to prevent extraneous noise from re-generated code in your pull requests. You will want to do this manually if you do not use the provided generator.
44
43
 
44
+ You can stop React on Rails from modifying or creating the `assets:precompile` task, by setting a `REACT_ON_RAILS_PRECOMPILE` environment variable to `no`, `false`, `n` or `f`.
@@ -0,0 +1,39 @@
1
+ # How to conditionally render server side based on the device type
2
+
3
+ In general, we want to use CSS to do mobile responsive layouts.
4
+
5
+ However, sometimes we want to have layouts sufficiently different that we can't do this via CSS. If we didn't do server rendering, we can check the device type on the client side. However, if we're doing server rendering, we need to send this data to the client code from the Rails server so that the server rendering can account for this.
6
+
7
+ Here's an example:
8
+
9
+ ## config/initializers/react_on_rails.rb
10
+ ```ruby
11
+ module RenderingExtension
12
+ # Return a Hash that contains custom values from the view context that will get passed to
13
+ # all calls to react_component and redux_store for rendering
14
+ def self.custom_context(view_context)
15
+ if view_context.controller.is_a?(ActionMailer::Base)
16
+ {}
17
+ else
18
+ {
19
+ desktop: !(view_context.browser.device.tablet? || view_context.browser.device.mobile?),
20
+ tablet: view_context.browser.device.tablet?,
21
+ mobile: view_context.browser.device.mobile? || false
22
+ }
23
+ end
24
+ end
25
+ end
26
+
27
+ # Shown below are the defaults for configuration
28
+ ReactOnRails.configure do |config|
29
+ # See https://github.com/shakacode/react_on_rails/blob/master/docs/guides/configuration.md for the rest
30
+
31
+ # This allows you to add additional values to the Rails Context. Implement one static method
32
+ # called `custom_context(view_context)` and return a Hash.
33
+ config.rendering_extension = RenderingExtension
34
+ end
35
+ ```
36
+
37
+ Note, full details of the React on Rails confguration are [here in docs/basics/configuration.md](https://shakacode.com/react-on-rails/docs/guides/configuration/).
38
+
39
+ See the doc file [docs/basics/generator-functions-and-railscontext.md](https://shakacode.com/react-on-rails/docs/guides/generator-functions-and-railscontext/#rails-context) for how your client-side code uses the device information
@@ -0,0 +1,98 @@
1
+ ## How to use different versions of a file for client and server rendering
2
+
3
+ There are 3 main ways to use different code for server vs. client rendering.
4
+
5
+ ## A. Using different Entry Points
6
+ Many projects will have different entry points for client and server rendering. This only works for a top-level entry point such as the entry point for a react-router app component.
7
+
8
+ Your Client Entry can look like this:
9
+
10
+ ```js
11
+ import ReactOnRails from 'react-on-rails';
12
+ import App from './ClientApp';
13
+ ReactOnRails.register({ App })
14
+ ```
15
+
16
+ So your Server Entry can look like:
17
+
18
+ ```js
19
+ import ReactOnRails from 'react-on-rails';
20
+ import App from './ServerApp';
21
+ ReactOnRails.register({ App })
22
+ ```
23
+
24
+ Note that the only difference is on the second line of each of these examples.
25
+
26
+ ## B. Two Options for Using Webpack Resolve Alias in the Webpack Config
27
+ Per [Webpack Docs](https://webpack.js.org/configuration/resolve/#resolve-alias).
28
+
29
+ ### 1. Update `webpack/set-resolve.js` to have a different resolution for the exact file:
30
+
31
+ ```js
32
+ function setResolve(builderConfig, webpackConfig) {
33
+
34
+ // Use a different resolution for Client and Server file
35
+ let SomeJsFile;
36
+ if (builderConfig.serverRendering) {
37
+ SomeJsFile = path.resolve(__dirname, "../bundles/SomeJsFileServer");
38
+ } else {
39
+ SomeJsFile = path.resolve(__dirname, "../bundles/SomeJsFileClient");
40
+ }
41
+
42
+ const resolve = {
43
+ alias: {
44
+ ... // blah blah
45
+ SomeJsFile,
46
+ ... // blah blah
47
+ },
48
+ ```
49
+
50
+ Then you have this import:
51
+
52
+ ```js
53
+ import SomeJsFile from 'SomeJsFile';
54
+ ```
55
+
56
+ ### 2. Use a different resolution for the right directory of client or server files:
57
+
58
+ #### a. Update `webpack/set-resolve.js` to have something like:
59
+ ```js
60
+ function setResolve(builderConfig, webpackConfig) {
61
+
62
+ // Use a different resolution for Client and Server file
63
+ let variant;
64
+ if (builderConfig.serverRendering) {
65
+ variant = path.resolve(__dirname, "../bundles/variant/ClientOnly");
66
+ } else {
67
+ variant = path.resolve(__dirname, "../bundles/variant/serverOnly");
68
+ }
69
+
70
+ const resolve = {
71
+ alias: {
72
+ ... // blah blah
73
+ variant
74
+ ... // blah blah
75
+ },
76
+ ```
77
+
78
+ #### b. Add different versions of the file to the `bundles/variant/ClientOnly` and `bundles/variant/ServerOnly` directories
79
+
80
+ #### c. Use the `variant` in import in a file that can be used both for client and server rendering:
81
+
82
+ ```js
83
+ import SomeJsFile from 'variant/SomeJsFile';
84
+ import AnotherJsFile from 'variant/AnotherJsFile';
85
+ ```
86
+
87
+ ## C. Conditional code that can check if `window` is defined.
88
+
89
+ This is probably the ugliest and hackiest way to do this, but it's quick! Essentially you wrap code that cannot execute server side in a conditional:
90
+
91
+ ```js
92
+ if (window) { // window should be falsy on the server side
93
+ doSomethingClientOnly();
94
+
95
+ // or do an import
96
+ const foobar = require('foobar').default;
97
+ }
98
+ ```
@@ -1,22 +1,29 @@
1
1
  # Getting Started with an existing Rails app
2
2
 
3
- **Also consult the instructions for installing on a fresh Rails app**, see the [React on Rails Basic Tutorial](../../docs/tutorial.md).
3
+ **Also consult the instructions for installing on a fresh Rails app**, see the [React on Rails Basic Tutorial](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/).
4
4
 
5
- **If you have rails-5 API only project**, first [convert the rails-5 API only app to rails app](#convert-rails-5-api-only-app-to-rails-app) before [getting started](#getting-started-with-an-existing-rails-app).
5
+ **If you have rails-5 API only project**, first [convert the rails-5 API only app to rails app](https://www.shakacode.com/react-on-rails/docs/rails/convert-rails-5-api-only-app-to-rails-app/).
6
6
 
7
- 1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `client/package.json` file.
7
+ 1. Add the following to your Gemfile and `bundle install`. We recommend fixing the version of React on Rails, as you will need to keep the exact version in sync with the version in your `package.json` file.
8
8
 
9
9
  ```ruby
10
10
  gem "react_on_rails", "12.0.0" # Update to the current version
11
11
  gem "webpacker", "~> 5"
12
12
  ```
13
13
 
14
- 2. Run the following 2 commands to install Webpacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install webpacker with React per the instructions [here](https://github.com/rails/webpacker).
14
+ 1. Add the webpacker and react_on_rails gems
15
+ _Use the latest version for react_on_rails._
15
16
 
17
+ ```
18
+ bundle add webpacker
19
+ bundle add react_on_rails --version=12.0.4 --strict
20
+ ```
21
+
22
+ 2. Run the following 2 commands to install Webpacker with React. Note, if you are using an older version of Rails than 5.1, you'll need to install webpacker with React per the instructions [here](https://github.com/rails/webpacker).
16
23
 
17
24
  ```bash
18
- $ bundle exec rails webpacker:install
19
- $ bundle exec rails webpacker:install:react
25
+ bundle exec rails webpacker:install
26
+ bundle exec rails webpacker:install:react
20
27
  ```
21
28
 
22
29
  3. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`).
@@ -45,7 +52,7 @@
45
52
 
46
53
  ## Installation
47
54
 
48
- See the [Installation Overview](docs/outdated/manual-installation-overview.md) for a concise set summary of what's in a React on Rails installation.
55
+ See the [Installation Overview](https://www.shakacode.com/react-on-rails/docs/additional-details/manual-installation-overview/) for a concise set summary of what's in a React on Rails installation.
49
56
 
50
57
 
51
58
  ## NPM