react_on_rails 11.0.5 → 13.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +338 -0
  3. data/.eslintignore +2 -1
  4. data/.eslintrc +32 -3
  5. data/.github/FUNDING.yml +1 -0
  6. data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
  7. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  8. data/.github/PULL_REQUEST_TEMPLATE.md +19 -0
  9. data/.github/workflows/lint-js-and-ruby.yml +54 -0
  10. data/.github/workflows/main.yml +183 -0
  11. data/.github/workflows/package-js-tests.yml +35 -0
  12. data/.github/workflows/rspec-package-specs.yml +46 -0
  13. data/.gitignore +3 -4
  14. data/.prettierignore +14 -0
  15. data/.prettierrc +20 -0
  16. data/.rubocop.yml +76 -34
  17. data/.travis.yml +15 -22
  18. data/CHANGELOG.md +443 -55
  19. data/CONTRIBUTING.md +62 -80
  20. data/Gemfile +1 -35
  21. data/Gemfile.development_dependencies +50 -0
  22. data/KUDOS.md +4 -1
  23. data/{docs/LICENSE.md → LICENSE.md} +1 -1
  24. data/NEWS.md +14 -4
  25. data/REACT-ON-RAILS-PRO-LICENSE +95 -0
  26. data/README.md +107 -802
  27. data/Rakefile +1 -8
  28. data/SUMMARY.md +51 -29
  29. data/book.json +5 -5
  30. data/docs/{basics/generator.md → additional-details/generator-details.md} +5 -13
  31. data/docs/{basics/installation-overview.md → additional-details/manual-installation-overview.md} +9 -14
  32. data/docs/{basics → additional-details}/migrating-from-react-rails.md +1 -1
  33. data/docs/additional-details/recommended-project-structure.md +69 -0
  34. data/docs/additional-details/tips-for-usage-with-sp6.md +15 -0
  35. data/docs/additional-details/upgrade-webpacker-v3-to-v4.md +10 -0
  36. data/docs/api/javascript-api.md +35 -6
  37. data/docs/api/redux-store-api.md +102 -0
  38. data/docs/api/view-helpers-api.md +133 -0
  39. data/docs/contributor-info/errors-with-hooks.md +45 -0
  40. data/docs/contributor-info/linters.md +5 -6
  41. data/docs/contributor-info/pull-requests.md +42 -0
  42. data/docs/contributor-info/releasing.md +1 -1
  43. data/docs/deployment/heroku-deployment.md +39 -0
  44. data/docs/getting-started.md +196 -0
  45. data/docs/guides/client-vs-server-rendering.md +27 -0
  46. data/docs/guides/configuration.md +289 -0
  47. data/docs/guides/deployment.md +5 -0
  48. data/docs/guides/file-system-based-automated-bundle-generation.md +197 -0
  49. data/docs/guides/hmr-and-hot-reloading-with-the-webpack-dev-server.md +104 -0
  50. data/docs/guides/how-react-on-rails-works.md +44 -0
  51. data/docs/guides/how-to-conditionally-server-render-based-on-device-type.md +40 -0
  52. data/docs/guides/how-to-use-different-files-for-client-and-server-rendering.md +98 -0
  53. data/docs/guides/i18n.md +87 -0
  54. data/docs/guides/installation-into-an-existing-rails-app.md +66 -0
  55. data/docs/guides/minitest-configuration.md +31 -0
  56. data/docs/guides/rails-webpacker-react-integration-options.md +213 -0
  57. data/docs/guides/react-on-rails-overview.md +29 -0
  58. data/docs/guides/react-server-rendering.md +32 -0
  59. data/docs/guides/render-functions-and-railscontext.md +205 -0
  60. data/docs/guides/rspec-configuration.md +73 -0
  61. data/docs/guides/tutorial.md +371 -0
  62. data/docs/{basics → guides}/upgrading-react-on-rails.md +126 -3
  63. data/docs/guides/webpack-configuration.md +42 -0
  64. data/docs/home.md +23 -0
  65. data/docs/javascript/asset-pipeline.md +12 -0
  66. data/docs/{additional-reading → javascript}/code-splitting.md +21 -11
  67. data/docs/javascript/converting-from-custom-webpack-config-to-rails-webpacker-config.md +10 -0
  68. data/docs/javascript/credits.md +10 -0
  69. data/docs/{additional-reading → javascript}/images.md +5 -6
  70. data/docs/javascript/react-helmet.md +100 -0
  71. data/docs/javascript/react-router.md +90 -0
  72. data/docs/{additional-reading → javascript}/server-rendering-tips.md +15 -12
  73. data/docs/javascript/troubleshooting-when-using-shakapacker.md +77 -0
  74. data/docs/{additional-reading → javascript}/webpack.md +2 -2
  75. data/docs/misc/articles.md +20 -0
  76. data/docs/misc/doctrine.md +5 -6
  77. data/docs/outdated/deferred-rendering.md +39 -0
  78. data/docs/{additional-reading → outdated}/rails-assets-relative-paths.md +4 -4
  79. data/docs/{additional-reading → outdated}/rails-assets.md +12 -20
  80. data/docs/{misc → outdated}/rails3.md +2 -2
  81. data/docs/rails/convert-rails-5-api-only-app.md +19 -0
  82. data/docs/rails/rails-engine-integration.md +32 -0
  83. data/docs/{additional-reading → rails}/rails_view_rendering_from_inline_javascript.md +2 -1
  84. data/docs/{additional-reading → rails}/turbolinks.md +13 -1
  85. data/docs/react-on-rails-pro/react-on-rails-pro.md +43 -0
  86. data/docs/testimonials/hvmn.md +25 -0
  87. data/docs/testimonials/resortpass.md +13 -0
  88. data/docs/testimonials/testimonials.md +28 -0
  89. data/jest.config.js +4 -0
  90. data/lib/generators/USAGE +1 -1
  91. data/lib/generators/react_on_rails/adapt_for_older_shakapacker_generator.rb +41 -0
  92. data/lib/generators/react_on_rails/base_generator.rb +55 -43
  93. data/lib/generators/react_on_rails/bin/dev +30 -0
  94. data/lib/generators/react_on_rails/bin/dev-static +30 -0
  95. data/lib/generators/react_on_rails/dev_tests_generator.rb +4 -3
  96. data/lib/generators/react_on_rails/generator_helper.rb +8 -6
  97. data/lib/generators/react_on_rails/generator_messages.rb +40 -0
  98. data/lib/generators/react_on_rails/install_generator.rb +37 -0
  99. data/lib/generators/react_on_rails/templates/.eslintrc +3 -1
  100. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +4 -6
  101. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-static +9 -0
  102. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +21 -40
  103. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.module.css +4 -0
  104. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorldServer.js +5 -0
  105. data/lib/generators/react_on_rails/templates/base/base/app/javascript/packs/server-bundle.js +8 -0
  106. data/lib/generators/react_on_rails/templates/base/base/app/views/layouts/hello_world.html.erb +2 -1
  107. data/lib/generators/react_on_rails/templates/base/base/babel.config.js.tt +32 -0
  108. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +20 -4
  109. data/lib/generators/react_on_rails/templates/base/base/config/shakapacker.yml +62 -0
  110. data/lib/generators/react_on_rails/templates/base/base/config/webpack/clientWebpackConfig.js.tt +17 -0
  111. data/lib/generators/react_on_rails/templates/base/base/config/webpack/commonWebpackConfig.js.tt +17 -0
  112. data/lib/generators/react_on_rails/templates/base/base/config/webpack/development.js.tt +25 -0
  113. data/lib/generators/react_on_rails/templates/base/base/config/webpack/production.js.tt +9 -0
  114. data/lib/generators/react_on_rails/templates/base/base/config/webpack/serverWebpackConfig.js.tt +117 -0
  115. data/lib/generators/react_on_rails/templates/base/base/config/webpack/test.js.tt +9 -0
  116. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpack.config.js.tt +15 -0
  117. data/lib/generators/react_on_rails/templates/base/base/config/webpack/webpackConfig.js.tt +36 -0
  118. data/lib/generators/react_on_rails/templates/dev_tests/spec/rails_helper.rb +8 -2
  119. data/lib/generators/react_on_rails/templates/dev_tests/spec/simplecov_helper.rb +1 -1
  120. data/lib/generators/react_on_rails/templates/dev_tests/spec/{features → system}/hello_world_spec.rb +2 -2
  121. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +6 -9
  122. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/store/helloWorldStore.js +1 -3
  123. data/lib/react_on_rails/configuration.rb +198 -145
  124. data/lib/react_on_rails/error.rb +2 -0
  125. data/lib/react_on_rails/git_utils.rb +5 -3
  126. data/lib/react_on_rails/{react_on_rails_helper.rb → helper.rb} +201 -190
  127. data/lib/react_on_rails/json_output.rb +1 -1
  128. data/lib/react_on_rails/json_parse_error.rb +28 -0
  129. data/lib/react_on_rails/locales/base.rb +169 -0
  130. data/lib/react_on_rails/locales/to_js.rb +33 -0
  131. data/lib/react_on_rails/locales/to_json.rb +23 -0
  132. data/lib/react_on_rails/packs_generator.rb +234 -0
  133. data/lib/react_on_rails/prerender_error.rb +35 -27
  134. data/lib/react_on_rails/react_component/render_options.rb +64 -9
  135. data/lib/react_on_rails/server_rendering_js_code.rb +55 -0
  136. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +126 -76
  137. data/lib/react_on_rails/server_rendering_pool.rb +0 -1
  138. data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +9 -8
  139. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
  140. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +13 -12
  141. data/lib/react_on_rails/test_helper.rb +24 -3
  142. data/lib/react_on_rails/utils.rb +94 -25
  143. data/lib/react_on_rails/version.rb +1 -1
  144. data/lib/react_on_rails/version_checker.rb +5 -1
  145. data/lib/react_on_rails/version_syntax_converter.rb +14 -12
  146. data/lib/react_on_rails/webpacker_utils.rb +105 -5
  147. data/lib/react_on_rails.rb +8 -2
  148. data/lib/tasks/assets.rake +28 -60
  149. data/lib/tasks/generate_packs.rake +11 -0
  150. data/lib/tasks/locale.rake +5 -4
  151. data/package-scripts.yml +49 -0
  152. data/package.json +52 -47
  153. data/rakelib/docker.rake +0 -5
  154. data/rakelib/dummy_apps.rake +5 -8
  155. data/rakelib/example_type.rb +12 -3
  156. data/rakelib/examples.rake +5 -4
  157. data/rakelib/lint.rake +5 -16
  158. data/rakelib/node_package.rake +2 -2
  159. data/rakelib/release.rake +37 -23
  160. data/rakelib/run_rspec.rake +16 -44
  161. data/rakelib/task_helpers.rb +16 -4
  162. data/react_on_rails.gemspec +6 -22
  163. data/tsconfig.json +14 -0
  164. data/webpackConfigLoader.js +5 -4
  165. data/yarn.lock +5935 -3106
  166. metadata +122 -272
  167. data/Gemfile.rails32 +0 -74
  168. data/docs/additional-reading/asset-pipeline.md +0 -20
  169. data/docs/additional-reading/babel.md +0 -5
  170. data/docs/additional-reading/caching-and-performance.md +0 -4
  171. data/docs/additional-reading/heroku-deployment.md +0 -92
  172. data/docs/additional-reading/hot-reloading-rails-development.md +0 -57
  173. data/docs/additional-reading/node-server-rendering.md +0 -5
  174. data/docs/additional-reading/rails-engine-integration.md +0 -34
  175. data/docs/additional-reading/react-helmet.md +0 -80
  176. data/docs/additional-reading/react-router.md +0 -113
  177. data/docs/additional-reading/recommended-project-structure.md +0 -49
  178. data/docs/additional-reading/rspec-configuration.md +0 -56
  179. data/docs/additional-reading/webpack-dev-server.md +0 -15
  180. data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
  181. data/docs/api/ruby-api.md +0 -8
  182. data/docs/basics/configuration.md +0 -163
  183. data/docs/basics/i18n.md +0 -77
  184. data/docs/tutorial.md +0 -220
  185. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-server +0 -12
  186. data/lib/react_on_rails/assets_precompile.rb +0 -150
  187. data/lib/react_on_rails/locales_to_js.rb +0 -134
  188. data/ruby-lint.yml +0 -25
  189. /data/docs/{additional-reading → additional-details}/updating-dependencies.md +0 -0
  190. /data/docs/{additional-reading → deployment}/elastic-beanstalk.md +0 -0
  191. /data/docs/{additional-reading → javascript}/angular-js-integration-migration.md +0 -0
  192. /data/docs/{additional-reading → javascript}/capistrano-deployment.md +0 -0
  193. /data/docs/{additional-reading → javascript}/foreman-issues.md +0 -0
  194. /data/docs/{additional-reading → javascript}/node-dependencies-and-npm.md +0 -0
  195. /data/docs/{additional-reading → javascript}/react-and-redux.md +0 -0
  196. /data/docs/{additional-reading → javascript}/troubleshooting-when-using-webpacker.md +0 -0
  197. /data/docs/{additional-reading → javascript}/webpack-v1-notes.md +0 -0
  198. /data/docs/{coding-style → misc}/style.md +0 -0
  199. /data/docs/{additional-reading → misc}/tips.md +0 -0
@@ -0,0 +1,205 @@
1
+ # Render-Functions and the Rails Context
2
+
3
+ ## Render-Functions
4
+
5
+ When you use a render-function to create react components (or renderedHtml on the server), or you
6
+ used shared redux stores, you get two params passed to your function that creates a React component:
7
+
8
+ 1. `props`: Props that you pass in the view helper of either `react_component` or `redux_store`
9
+ 2. `railsContext`: Rails contextual information, such as the current pathname. You can customize
10
+ this in your config file. **Note**: The `railsContext` is not related to the concept of a
11
+ ["context" for React components](https://facebook.github.io/react/docs/context.html#how-to-use-context).
12
+
13
+ These parameters (`props` and `railsContext`) will be the same regardless of either client or server
14
+ side rendering, except for the key `serverSide` based on whether or not you are server rendering.
15
+
16
+ While you could manually configure your Rails code to pass the "`railsContext` information" with
17
+ the rest of your "props", the `railsContext` is a convenience because it's passed consistently to
18
+ all invocations of Render-Functions.
19
+
20
+ For example, suppose you create a "render-function" called MyAppComponent.
21
+
22
+ ```js
23
+ import React from 'react';
24
+ const MyAppComponent = (props, railsContext) => (
25
+ // NOTE: need to wrap in a function so this is proper React function component that can use
26
+ // hooks
27
+
28
+ // the props get passed again, but we ignore since we use a closure
29
+ // or should we
30
+ () =>
31
+ <div>
32
+ <p>props are: {JSON.stringify(props)}</p>
33
+ <p>railsContext is: {JSON.stringify(railsContext)}
34
+ </p>
35
+ </div>
36
+ );
37
+ export default MyAppComponent;
38
+ ```
39
+
40
+ ------------------------------
41
+
42
+ _This would be alternate API where you have to call React.createElement and the React on Rails code doesn't do that._
43
+
44
+ ```js
45
+ import React from 'react';
46
+ const MyAppComponent = (props, railsContext) => (
47
+ // NOTE: need to wrap in a function so this is proper React function component that can use
48
+ // hooks
49
+ React.createElement(
50
+ () =>
51
+ <div>
52
+ <p>props are: {JSON.stringify(props)}</p>
53
+ <p>railsContext is: {JSON.stringify(railsContext)}
54
+ </p>
55
+ </div>,
56
+ props)
57
+ );
58
+ export default MyAppComponent;
59
+ ```
60
+
61
+ ------------------------------------
62
+
63
+ *Note: you will get a React browser console warning if you try to serverRender this since the value of `serverSide` will be different for server rendering.*
64
+
65
+ So if you register your render-function `MyAppComponent`, it will get called like:
66
+
67
+ ```js
68
+ reactComponent = MyAppComponent(props, railsContext);
69
+ ```
70
+
71
+ and, similarly, any redux store is always initialized with 2 parameters:
72
+
73
+ ```js
74
+ reduxStore = MyReduxStore(props, railsContext);
75
+ ```
76
+
77
+ Note: you never make these calls. React on Rails makes these calls when it does either client or server rendering. You will define functions that take these 2 params and return a React component or a Redux Store. Naturally, you do not have to use second parameter of the railsContext if you do not need it. If you don't take a second parameter, then you're probably defining a React function component and you will simply return a React Element, often just JSX.
78
+
79
+ (Note: see below [section](#multiple-react-components-on-a-page-with-one-store) on how to setup redux stores that allow multiple components to talk to the same store.)
80
+
81
+ The `railsContext` has: (see implementation in file [ReactOnRails::Helper](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/helper.rb), method `rails_context` for the definitive list).
82
+
83
+ ```ruby
84
+ {
85
+ railsEnv: Rails.env
86
+ inMailer: in_mailer?,
87
+ # Locale settings
88
+ i18nLocale: I18n.locale,
89
+ i18nDefaultLocale: I18n.default_locale,
90
+ rorVersion: ReactOnRails::VERSION,
91
+ rorPro: ReactOnRails::Utils.react_on_rails_pro?
92
+
93
+ # URL settings
94
+ href: request.original_url,
95
+ location: "#{uri.path}#{uri.query.present? ? "?#{uri.query}": ""}",
96
+ scheme: uri.scheme, # http
97
+ host: uri.host, # foo.com
98
+ port: uri.port,
99
+ pathname: uri.path, # /posts
100
+ search: uri.query, # id=30&limit=5
101
+ httpAcceptLanguage: request.env["HTTP_ACCEPT_LANGUAGE"]
102
+
103
+ # Other
104
+ serverSide: boolean # Are we being called on the server or client? Note: if you conditionally
105
+ # render something different on the server than the client, then React will only show the
106
+ # server version!
107
+ }
108
+ ```
109
+
110
+ Plus, you can add your customizations to this. See "rendering extension" below.
111
+
112
+ ## Rails Context
113
+
114
+ The `railsContext` is a second param passed to your render-functions for React components. This is in addition to the props that are passed from the `react_component` Rails helper. For example:
115
+
116
+ ERB view file:
117
+
118
+ ```ruby
119
+ # Rails View
120
+ <%= react_component("HelloWorld", props: { name: "Stranger" }) %>
121
+ ```
122
+
123
+ 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).
124
+
125
+ ```js
126
+ import React from 'react';
127
+
128
+ export default (props, railsContext) => {
129
+ // Note, wrap in a function so this is React function component
130
+ return () => (
131
+ <div>
132
+ Your locale is {railsContext.i18nLocale}.<br/>
133
+ Hello, {props.name}!
134
+ </div>
135
+ );
136
+ };
137
+ ```
138
+
139
+ ## Why is the railsContext only passed to render-functions?
140
+
141
+ There's no reason that the railsContext would ever get passed to your React component unless the value is explicitly put into the props used for rendering. If you create a react component, rather than a render-function, for use by React on Rails, then you get whatever props are passed in from the view helper, which **does not include the Rails Context**. It's trivial to wrap your component in a "render-function" to return a new component that takes both:
142
+
143
+ ```js
144
+ import React from 'react';
145
+ import AppComponent from './AppComponent';
146
+ const AppComponentWithRailsContext = (props, railsContext) => (
147
+ // Create a React Function Component so you can
148
+ // use the React Hooks API in this React Function Component
149
+ () => <AppComponent {...{...props, railsContext}}/>
150
+ )
151
+ export default AppComponentWithRailsContext;
152
+ ```
153
+
154
+ Consider this line in depth:
155
+
156
+ ```js
157
+ <AppComponent {...{ ...props, railsContext }}/>
158
+ ```
159
+
160
+ The outer `{...` is for the [JSX spread operator for attributes](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes) and the innner `{...` is for the [Spread in object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Spread_in_object_literals).
161
+
162
+ ## Use Cases
163
+
164
+ ### Heroku Preboot Considerations
165
+
166
+ [Heroku Preboot](https://devcenter.heroku.com/articles/preboot) is a feature on Heroku that allows for faster deploy times. When you promote your staging app to production, Preboot simply switches the production server to point at the staging app's container. This means it can deploy much faster since it doesn't have to rebuild anything. However, this means that if you use the [Define Plugin](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin) to provide the rails environment to your client code as a variable, that variable will erroneously still have a value of `Staging` instead of `Production`. The `Rails.env` provided at runtime in the railsContext is, however, accurate.
167
+
168
+ ### Needing the current URL path for server rendering
169
+
170
+ Suppose you want to display a nav bar with the current navigation link highlighted by the URL. When you server-render the code, your code will need to know the current URL/path. The new `railsContext` has this information. Your application will apply something like an "active" class on the server rendering.
171
+
172
+ ### Configuring different code for server side rendering
173
+
174
+ Suppose you want to turn off animation when doing server side rendering. The `serverSide` value is just what you need.
175
+
176
+ ## Customization of the rails_context
177
+
178
+ You can customize the values passed in the `railsContext` in your `config/initializers/react_on_rails.rb`. Here's how.
179
+
180
+ Set the config value for the `rendering_extension`:
181
+
182
+ ```ruby
183
+ config.rendering_extension = RenderingExtension
184
+ ```
185
+
186
+ Implement it like this above in the same file. Create a class method on the module called `custom_context` that takes the `view_context` for a param.
187
+
188
+ See [spec/dummy/config/initializers/react_on_rails.rb](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/initializers/react_on_rails.rb) for a detailed example.
189
+
190
+ ```ruby
191
+ module RenderingExtension
192
+
193
+ # Return a Hash that contains custom values from the view context that will get merged with
194
+ # the standard rails_context values and passed to all calls to render-functions used by the
195
+ # react_component and redux_store view helpers
196
+ def self.custom_context(view_context)
197
+ {
198
+ somethingUseful: view_context.session[:something_useful]
199
+ }
200
+ end
201
+ end
202
+ ```
203
+
204
+ In this case, a prop and value for `somethingUseful` will go into the railsContext passed to all react_component and redux_store calls. You may set any values available in the view rendering context.
205
+
@@ -0,0 +1,73 @@
1
+ # RSpec Configuration
2
+ _Click [here for minitest](https://www.shakacode.com/react-on-rails/docs/guides/minitest-configuration/)_
3
+
4
+ # If your webpack configurations correspond to Shakapacker's default setup
5
+ If you're able to configure your webpack configuration to be run by having your webpack configuration
6
+ returned by the files in `/config/webpack`, then you have 2 options to ensure that your files are
7
+ compiled by webpack before running tests and during production deployment:
8
+
9
+ 1. **Use Shakapacker's compile option**: Configure your `config/shakapacker.yml` so that `compile: true` is for `test` and `production`
10
+ environments. Ensure that your `source_path` is correct, or else `Shakapacker` won't correctly
11
+ detect changes.
12
+ 2. **Use the react_on_rails settings and helpers**. Use the settings in `config/initializers/react_on_rails.rb`. Refer to [docs/configuration](https://www.shakacode.com/react-on-rails/docs/guides/configuration/).
13
+
14
+ ```yml
15
+ config.build_test_command = "NODE_ENV=test RAILS_ENV=test bin/shakapacker"
16
+ ```
17
+
18
+ Which should you use? If you're already using the `Shakapacker` way to configure webpack, then
19
+ you can keep things simple and use the `Shakapacker` options.
20
+
21
+ # Checking for stale assets using React on Rails
22
+
23
+ Because you will probably want to run RSpec tests that rely on compiled webpack assets (typically, your integration/feature specs where `js: true`), you will want to ensure you don't accidentally run tests on missing or stale webpack assets. If you did use stale Webpack assets, you will get invalid test results as your tests do not use the very latest JavaScript code.
24
+
25
+ As mentioned above, you can configure `compile: true` in `config/shakapacker.yml` _if_ you've got configuration for
26
+ your webpack in the standard `Shakapacker` spot of `config/webpack/<NODE_ENV>.js`
27
+
28
+ ReactOnRails also provides a helper method called `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`. Call this method from inside of the `RSpec.configure` block in your `spec/rails_helper.rb` file, passing the config as an argument. See file [lib/react_on_rails/test_helper.rb](https://github.com/shakacode/react_on_rails/tree/master/lib/react_on_rails/test_helper.rb) for more details. You can customize this to your particular needs by replacing any of the default components used by `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`.
29
+
30
+ ```ruby
31
+ RSpec.configure do |config|
32
+ ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
33
+ ```
34
+
35
+ You can pass one or more RSpec metatags as an optional second parameter to this helper method if you want this helper to run on examples other than where `:js`, `:server_rendering`, or `:controller` (those are the defaults). The helper will compile webpack files at most once per test run. The helper will not compile the webpack files unless they are out of date (stale). The helper is configurable in terms of what command is used to prepare the files. If you don't specify these metatags for your relevant JavaScript tests, then you'll need to do the following.
36
+
37
+ If you are using Webpack to build CSS assets, you should do something like this to ensure that you assets are built for any specs under `specs/requests` or `specs/features`:
38
+
39
+ ```ruby
40
+ ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config, :requires_webpack_assets)
41
+ config.define_derived_metadata(file_path: %r{spec/(features|requests)}) do |metadata|
42
+ metadata[:requires_webpack_assets] = true
43
+ end
44
+ ```
45
+
46
+ Please take note of the following:
47
+ - If you are using Shakapacker, be **SURE** to configure the `source_path` in your `config/shakapacker.yml` unless you are using the defaults for Shakapacker.
48
+
49
+ - This utility uses your `build_test_command` to build the static generated files. This command **must not** include the `--watch` option. If you have different server and client bundle files, this command **must** create all the bundles. If you are using Shakapacker, the default value will come from the `config/shakapacker.yml` value for the `public_output_path` and the `source_path`
50
+
51
+ - If you add an older file to your source files, that is already older than the produced output files, no new recompilation is done. The solution to this issue is to clear out your directory of webpack generated files when adding new source files that may have older dates.
52
+
53
+ - By default, the webpack processes look in the webpack generated files folder, configured via the `config/shakapacker.yml` config values of `public_root_path` and `public_output_path`. If the webpack generated files folder is missing, is empty, or contains files in the `config.webpack_generated_files` list with `mtime`s older than any of the files in your `client` folder, the helper will recompile your assets.
54
+
55
+ The following `config/react_on_rails.rb` settings **must** match your setup:
56
+ ```ruby
57
+ # Define the files we need to check for webpack compilation when running tests.
58
+ config.webpack_generated_files = %w( manifest.json )
59
+
60
+ # OR if you're not hashing the server-bundle.js, then you should include your server-bundle.js in the list.
61
+ # config.webpack_generated_files = %w( server-bundle.js manifest.json )
62
+
63
+ # If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
64
+ # with rspec then this controls what yarn command is run
65
+ # to automatically refresh your webpack assets on every test run.
66
+ config.build_test_command = "yarn run build:test"
67
+ ```
68
+
69
+ If you want to speed up the re-compiling process so you don't wait to run your tests to build the files, you can run your test compilation with the "watch" flags. For example, `yarn run build:test --watch`
70
+
71
+ ![2016-01-27_02-36-43](https://cloud.githubusercontent.com/assets/1118459/12611951/7c56d070-c4a4-11e5-8a80-9615f99960d9.png)
72
+
73
+ ![2016-01-27_03-18-05](https://cloud.githubusercontent.com/assets/1118459/12611975/a8011654-c4a4-11e5-84f9-1baca4835b4b.png)
@@ -0,0 +1,371 @@
1
+ # React on Rails Basic Tutorial
2
+
3
+ _Also see the example repo of [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_demo_ssr_hmr)_
4
+
5
+ -----
6
+
7
+ *Updated for Ruby 2.7, Rails 7, React on Rails v13, and Shakapacker v7*
8
+
9
+ This tutorial guides you through setting up a new or existing Rails app with **React on Rails**, demonstrating Rails + React + Redux + Server Rendering.
10
+
11
+ After finishing this tutorial you will get an application that can do the following (live on Heroku):
12
+
13
+ ![example](https://cloud.githubusercontent.com/assets/371302/17368567/111cc722-596b-11e6-9b72-ac5967a60e42.gif)
14
+
15
+ You can find it here:
16
+ * [Source code for this app in PR, using the --redux option](https://github.com/shakacode/react_on_rails-test-new-redux-generation/pull/17) and [for Heroku](https://github.com/shakacode/react_on_rails-test-new-redux-generation/pull/18).
17
+ * [Live on Heroku](https://react-on-rails-redux-gen-8-0-0.herokuapp.com/)
18
+
19
+ By the time you read this, the latest may have changed. Be sure to check the versions here:
20
+
21
+ * https://rubygems.org/gems/react_on_rails
22
+ * https://www.npmjs.com/package/react-on-rails
23
+
24
+ # Table of Content:
25
+ - [Installation](#installation)
26
+ - [Setting up your environment](#setting-up-your-environment)
27
+ - [Create a new Ruby on Rails App](#create-a-new-ruby-on-rails-app)
28
+ - [Add the Shakapacker and react_on_rails gems](#add-the-shakapacker-and-react_on_rails-gems)
29
+ - [Run the Shakapacker generator](#run-the-shakapacker-generator)
30
+ - [Run the React on Rails Generator](#run-the-react-on-rails-generator)
31
+ - [Setting up your environment variables](#setting-up-your-environment-variables)
32
+ - [Running the app](#running-the-app)
33
+ - [HMR vs. React Hot Reloading](#hmr-vs-react-hot-reloading)
34
+ - [Deploying to Heroku](#deploying-to-heroku)
35
+ - [Create Your Heroku App](#create-your-heroku-app)
36
+ - [Swap out sqlite for postgres](#swap-out-sqlite-for-postgres)
37
+ - [Other features](#other-features)
38
+ - [Turning on Server Rendering](#turning-on-server-rendering)
39
+ - [Moving from the Rails default `/app/javascript` to the recommended `/client` structure](#moving-from-the-rails-default-appjavascript-to-the-recommended-client-structure)
40
+ - [Using HMR with the shakapacker setup](#using-hmr-with-the-shakapacker-setup)
41
+ - [Custom IP & PORT setup (Cloud9 example)](#custom-ip--port-setup-cloud9-example)
42
+ - [RubyMine performance tip](#rubymine-performance-tip)
43
+ - [Conclusion](#conclusion)
44
+ # Installation
45
+ ## Setting up your environment
46
+
47
+ Trying out **React on Rails** is super easy, so long as you have the basic prerequisites.
48
+
49
+ - **Ruby:** We support all active Ruby versions but recommend using the latest stable Ruby version. Solutions like [rvm](https://rvm.io) or [rbenv](https://github.com/rbenv/rbenv) make it easy to have multiple Ruby versions on your machine.
50
+ - **Rails:** We support Rails 6 and later.
51
+ - **Nodejs:** We support all [active Node versions](https://github.com/nodejs/release#release-schedule) but recommend using the latest LTS release of Nodejs for the longest support. Older inactive node versions might still work but is not guaranteed. We also recommend using [nvm](https://github.com/nvm-sh/nvm/) to ease using different node versions in different projects.
52
+ - **yarn:** We use [yarn classic](https://classic.yarnpkg.com/) as our node package manager.
53
+ - You need to have either [Overmind](https://github.com/DarthSim/overmind) or [Foreman](https://rubygems.org/gems/foreman) as a process manager.
54
+
55
+ ## Create a new Ruby on Rails App
56
+ Then we need to create a fresh Rails application as follows.
57
+
58
+ First, be sure to run `rails -v` and check you are using Rails 5.1.3 or above. If you are using an older version of Rails, you'll need to install webpacker with react per the instructions [here](https://github.com/rails/webpacker).
59
+
60
+ ```bash
61
+ # For Rails 6.x
62
+ rails new test-react-on-rails --skip-javascript
63
+
64
+ # For Rails 7.x
65
+ rails new test-react-on-rails --skip-javascript
66
+
67
+ cd test-react-on-rails
68
+ ```
69
+ Note: You can use `--database=postgresql` option to use Postgresql for the database.
70
+
71
+ ## Add the Shakapacker and react_on_rails gems
72
+ We recommend using the latest version of these gems. Otherwise, specify the
73
+ exact versions of both the gem and npm packages. In other words, don't use
74
+ the `^` or `~` in the version specifications.
75
+
76
+ ```bash
77
+ bundle add react_on_rails --strict
78
+ bundle add shakapacker --strict
79
+ ```
80
+
81
+ Note: The latest released React On Rails version is considered stable. Please use the latest
82
+ version to ensure you get all the security patches and the best support.
83
+
84
+ ## Run the Shakapacker generator
85
+
86
+ ```bash
87
+ bundle exec rails shakapacker:install
88
+ ```
89
+
90
+ Commit all the changes so far to avoid getting errors in the next step.
91
+
92
+ ```bash
93
+ git commit -am "Initial commit"
94
+ ```
95
+
96
+ Alternatively, you can use `--ignore-warnings` in the next step.
97
+
98
+ ## Run the React on Rails Generator
99
+
100
+ ```bash
101
+ rails generate react_on_rails:install
102
+ ```
103
+
104
+ You will be prompted to approve changes in certain files. Press `enter` to proceed
105
+ one by one or enter `a` to replace all configuration files required by the project.
106
+ You can check the diffs before you commit to see what changed.
107
+
108
+ Note, using `redux` is no longer recommended as the basic installer uses React Hooks.
109
+ If you want the redux install, run:
110
+
111
+ ```bash
112
+ rails generate react_on_rails:install --redux
113
+ ```
114
+
115
+ ## Setting up your environment variables
116
+
117
+ Add the following variable to your environment:
118
+
119
+ ```
120
+ EXECJS_RUNTIME=Node
121
+ ```
122
+
123
+ Then run the server with one of the following options:
124
+
125
+ ## Running the app
126
+
127
+ ```bash
128
+ ./bin/dev # For HMR
129
+ # or
130
+ ./bin/dev-static # Without HMR, statically creating the bundles
131
+ ```
132
+
133
+ Visit [http://localhost:3000/hello_world](http://localhost:3000/hello_world) and see your **React On Rails** app running!
134
+
135
+ # HMR vs. React Hot Reloading
136
+
137
+ First, check that the `hmr` and the `inline` options are `true` in your `config/shakapacker.yml` file.
138
+
139
+ The basic setup will have HMR working with the default Shakapacker setup. The basic
140
+ [HMR](https://webpack.js.org/concepts/hot-module-replacement/), without a special
141
+ React setup, will cause a full page refresh each time you save a file.
142
+
143
+ # Deploying to Heroku
144
+
145
+ ## Create Your Heroku App
146
+ *Assuming you can log in to heroku.com and have logged into your shell for Heroku.*
147
+
148
+ 1. Visit [https://dashboard.heroku.com/new](https://dashboard.heroku.com/new) and create an app, say named `my-name-react-on-rails`:
149
+
150
+ ![06](https://cloud.githubusercontent.com/assets/20628911/17465014/1f29bf3c-5cf4-11e6-869f-4215987ae854.png)
151
+
152
+ Run this command that looks like this from your new Heroku app
153
+
154
+ ```bash
155
+ heroku git:remote -a my-name-react-on-rails
156
+ ```
157
+
158
+ Set heroku to use multiple buildpacks:
159
+
160
+ ```bash
161
+ heroku buildpacks:set heroku/ruby
162
+ heroku buildpacks:add --index 1 heroku/nodejs
163
+ ```
164
+
165
+ ## Swap out sqlite for postgres:
166
+ Heroku requires your app to use Postgresql. If you have not setup your app
167
+ with Postgresql, you need to change your app settings to use this database.
168
+
169
+ Run the following command (in Rails 6+):
170
+
171
+ ```bash
172
+ rails db:system:change --to=postgresql
173
+ ```
174
+
175
+ If for any reason you want to do this process manually, run these two commands:
176
+
177
+ ```bash
178
+ bundle remove sqlite3
179
+ bundle add pg
180
+ ```
181
+
182
+ ![07](https://cloud.githubusercontent.com/assets/20628911/17465015/1f2f4042-5cf4-11e6-8287-2fb077550809.png)
183
+
184
+ Now replace your `database.yml` file with this (assuming your app name is "ror").
185
+
186
+ ```yml
187
+ default: &default
188
+ adapter: postgresql
189
+ username:
190
+ password:
191
+ host: localhost
192
+
193
+ development:
194
+ <<: *default
195
+ database: ror_development
196
+
197
+ # Warning: The database defined as "test" will be erased and
198
+ # re-generated from your development database when you run "rake".
199
+ # Do not set this db to the same as development or production.
200
+ test:
201
+ <<: *default
202
+ database: ror_test
203
+
204
+ production:
205
+ <<: *default
206
+ database: ror_production
207
+ ```
208
+
209
+ Then you need to setup postgres so you can run locally:
210
+
211
+ ```bash
212
+ rake db:setup
213
+ rake db:migrate
214
+ ```
215
+
216
+ ![08](https://cloud.githubusercontent.com/assets/20628911/17465016/1f3559f0-5cf4-11e6-8ab4-c5572e4644a5.png)
217
+
218
+ Optionally you can add this line to your `routes.rb`. That way, your root page will go to the Hello World page for React On Rails.
219
+
220
+ ```ruby
221
+ root "hello_world#index"
222
+ ```
223
+
224
+ ![09](https://cloud.githubusercontent.com/assets/20628911/17465018/1f3b685e-5cf4-11e6-93f8-105fc48517d0.png)
225
+
226
+ Next, configure your app for Puma, per the [instructions on Heroku](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server).
227
+
228
+ Create `./Procfile` with the following content. This is what Heroku uses to start your app.
229
+
230
+ ```
231
+ web: bundle exec puma -C config/puma.rb
232
+ ```
233
+
234
+ Note, newer versions of Rails create this file automatically. However, the [docs on Heroku](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#config) have something a bit different, so please make it conform to those docs. As of 2020-06-04, the docs looked like this:
235
+
236
+ `config/puma.rb`
237
+ ```rb
238
+ workers Integer(ENV['WEB_CONCURRENCY'] || 2)
239
+ threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
240
+ threads threads_count, threads_count
241
+
242
+ preload_app!
243
+
244
+ rackup DefaultRackup
245
+ port ENV['PORT'] || 3000
246
+ environment ENV['RACK_ENV'] || 'development'
247
+
248
+ on_worker_boot do
249
+ # Worker specific setup for Rails 4.1+
250
+ # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
251
+ ActiveRecord::Base.establish_connection
252
+ end
253
+ ```
254
+
255
+ Next, update your `package.json` to specify the version of yarn and node. Add this section:
256
+
257
+ ```json
258
+ "engines": {
259
+ "node": "16.19.0",
260
+ "yarn": "1.22.4"
261
+ },
262
+ ```
263
+
264
+ Then after all changes are done don't forget to commit them with git and finally, you can push your app to Heroku!
265
+
266
+ ```bash
267
+ git add -A
268
+ git commit -m "Changes for Heroku"
269
+ git push heroku master
270
+ ```
271
+
272
+ Then run:
273
+
274
+ ```bash
275
+ heroku open
276
+ ```
277
+
278
+ and you will see your live app and you can share this URL with your friends. Congrats!
279
+
280
+ # Other features
281
+ ## Turning on Server Rendering
282
+
283
+ You can turn on server rendering by simply changing the `prerender` option to `true`:
284
+
285
+ ```erb
286
+ <%= react_component("HelloWorld", props: @hello_world_props, prerender: true) %>
287
+ ```
288
+
289
+ If you want to test this out with HMR, then you also need to add this line to your
290
+ `config/intializers/react_on_rails.rb`
291
+
292
+ ```ruby
293
+ config.same_bundle_for_client_and_server = true
294
+ ```
295
+
296
+ More likely, you will create a different build file for server rendering. However, if you want to
297
+ use the same file from the shakapack-dev-server, you'll need to add that line.
298
+
299
+ When you look at the source code for the page (right click, view source in Chrome), you can see the difference between non-server rendering, where your DIV containing your React looks like this:
300
+
301
+ ```html
302
+ <div id="HelloWorld-react-component-b7ae1dc6-396c-411d-886a-269633b3f604"></div>
303
+ ```
304
+
305
+ versus with server rendering:
306
+
307
+ ```html
308
+ <div id="HelloWorld-react-component-d846ce53-3b82-4c4a-8f32-ffc347c8444a"><div data-reactroot=""><h3>Hello, <!-- -->Stranger<!-- -->!</h3><hr/><form><label for="name">Say hello to:</label><input type="text" id="name" value="Stranger"/></form></div></div>
309
+ ```
310
+
311
+ For more details on server rendering, see:
312
+
313
+ + [Client vs. Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/client-vs-server-rendering/)
314
+ + [React Server Rendering](https://www.shakacode.com/react-on-rails/docs/guides/react-server-rendering/)
315
+
316
+ ## Moving from the Rails default `/app/javascript` to the recommended `/client` structure
317
+
318
+ ShakaCode recommends that you use `/client` for your client side app. This way a non-Rails, front-end developer can be at home just by opening up the `/client` directory.
319
+
320
+
321
+ 1. Move the directory:
322
+
323
+ ```bash
324
+ mv app/javascript client
325
+ ```
326
+
327
+ 2. Edit your `/config/shakapacker.yml` file. Change the `default/source_path`:
328
+
329
+ ```yml
330
+ source_path: client
331
+ ```
332
+
333
+ ## Using HMR with the shakapacker setup
334
+
335
+ Start the app using `overmind start -f Procfile.dev` or `foreman start -f Procfile.dev`.
336
+
337
+ When you change and save a JSX file, the browser will automatically refresh!
338
+
339
+ So you get some basics from HMR with no code changes. If you want to go further, take a look at these links:
340
+
341
+ * [webpack-dev-server](https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack-dev-server.md)
342
+ * [DevServer](https://webpack.js.org/configuration/dev-server/)
343
+ * [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/)
344
+
345
+ React on Rails will automatically handle disabling server rendering if there is only one bundle file created by the Webpack development server by `shakapcker`.
346
+
347
+
348
+ ## Custom IP & PORT setup (Cloud9 example)
349
+
350
+ In case you are running some custom setup with different IP or PORT you should also edit Procfile.dev. For example, to be able to run on free Cloud9 IDE we are putting IP 0.0.0.0 and PORT 8080. The default generated file `Procfile.dev` uses `-p 3000`.
351
+
352
+ ``` Procfile.dev
353
+ web: rails s -p 8080 -b 0.0.0.0
354
+ ```
355
+
356
+ Then visit https://your-shared-addr.c9users.io:8080/hello_world
357
+
358
+ ## RubyMine performance tip
359
+
360
+ It's super important to exclude certain directories from RubyMine or else it will slow to a crawl as it tries to parse all the npm files.
361
+
362
+ * Generated files, per the settings in your `config/shakapacker.yml`, which default to `public/packs` and `public/packs-test`
363
+ * `node_modules`
364
+
365
+ # Conclusion
366
+
367
+ * Browse the docs on [our documentation website](https://www.shakacode.com/react-on-rails/docs/)
368
+
369
+ Feedback is greatly appreciated! As are stars on github!
370
+
371
+ If you want personalized help, don't hesitate to get in touch with us at [contact@shakacode.com](mailto:contact@shakacode.com). We offer [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki) and consulting so you can focus on your app and not on how to make Webpack plus Rails work optimally.