react_on_rails 11.0.3 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +320 -0
  3. data/.eslintignore +2 -1
  4. data/.eslintrc +30 -2
  5. data/.github/FUNDING.yml +1 -0
  6. data/.gitignore +3 -1
  7. data/.prettierignore +10 -0
  8. data/.prettierrc +23 -0
  9. data/.release-it.json +3 -0
  10. data/.rubocop.yml +39 -11
  11. data/.travis.yml +11 -22
  12. data/CHANGELOG.md +209 -4
  13. data/CONTRIBUTING.md +64 -84
  14. data/Gemfile +3 -5
  15. data/KUDOS.md +4 -1
  16. data/{docs/LICENSE.md → LICENSE.md} +1 -1
  17. data/PROJECTS.md +7 -1
  18. data/REACT-ON-RAILS-PRO-LICENSE +95 -0
  19. data/README.md +233 -634
  20. data/Rakefile +1 -5
  21. data/SUMMARY.md +42 -29
  22. data/book.json +5 -5
  23. data/docs/additional-reading/asset-pipeline.md +8 -16
  24. data/docs/additional-reading/convert-rails-5-api-only-app.md +19 -0
  25. data/docs/additional-reading/credits.md +10 -0
  26. data/docs/additional-reading/images.md +1 -1
  27. data/docs/additional-reading/rails-engine-integration.md +7 -0
  28. data/docs/additional-reading/rails_view_rendering_from_inline_javascript.md +2 -1
  29. data/docs/additional-reading/react-helmet.md +30 -10
  30. data/docs/additional-reading/react-router.md +52 -75
  31. data/docs/additional-reading/server-rendering-tips.md +14 -11
  32. data/docs/additional-reading/upgrade-webpacker-v3-to-v4.md +10 -0
  33. data/docs/additional-reading/webpack.md +2 -2
  34. data/docs/api/javascript-api.md +24 -4
  35. data/docs/api/redux-store-api.md +102 -0
  36. data/docs/api/view-helpers-api.md +133 -0
  37. data/docs/articles.md +20 -0
  38. data/docs/basics/client-vs-server-rendering.md +23 -0
  39. data/docs/basics/configuration.md +145 -61
  40. data/docs/basics/deployment.md +4 -0
  41. data/docs/basics/{generator.md → generator-details.md} +5 -8
  42. data/docs/basics/heroku-deployment.md +24 -0
  43. data/docs/basics/hmr-and-hot-reloading-with-the-webpack-dev-server.md +49 -0
  44. data/docs/basics/i18n.md +45 -23
  45. data/docs/basics/installation-into-an-existing-rails-app.md +59 -0
  46. data/docs/basics/minitest-configuration.md +31 -0
  47. data/docs/basics/react-server-rendering.md +29 -0
  48. data/docs/{additional-reading → basics}/recommended-project-structure.md +38 -10
  49. data/docs/basics/render-functions-and-railscontext.md +205 -0
  50. data/docs/basics/rspec-configuration.md +73 -0
  51. data/docs/basics/upgrading-react-on-rails.md +74 -3
  52. data/docs/basics/webpack-configuration.md +50 -0
  53. data/docs/contributor-info/errors-with-hooks.md +45 -0
  54. data/docs/contributor-info/pull-requests.md +44 -0
  55. data/docs/misc/doctrine.md +2 -2
  56. data/docs/{additional-reading → outdated}/code-splitting.md +19 -9
  57. data/docs/outdated/how-react-on-rails-works.md +44 -0
  58. data/docs/{basics/installation-overview.md → outdated/manual-installation-overview.md} +8 -13
  59. data/docs/{additional-reading → outdated}/rails-assets-relative-paths.md +3 -3
  60. data/docs/{additional-reading → outdated}/rails-assets.md +5 -13
  61. data/docs/testimonials/hvmn.md +25 -0
  62. data/docs/testimonials/resortpass.md +13 -0
  63. data/docs/testimonials/testimonials.md +28 -0
  64. data/docs/tutorial.md +182 -44
  65. data/jest.config.js +4 -0
  66. data/lib/generators/USAGE +1 -1
  67. data/lib/generators/react_on_rails/base_generator.rb +2 -2
  68. data/lib/generators/react_on_rails/dev_tests_generator.rb +3 -2
  69. data/lib/generators/react_on_rails/generator_helper.rb +4 -6
  70. data/lib/generators/react_on_rails/install_generator.rb +2 -0
  71. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev +3 -1
  72. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-hmr +26 -0
  73. data/lib/generators/react_on_rails/templates/base/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +20 -40
  74. data/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb +4 -1
  75. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/components/HelloWorld.jsx +4 -8
  76. data/lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/store/helloWorldStore.js +1 -3
  77. data/lib/react_on_rails/configuration.rb +171 -100
  78. data/lib/react_on_rails/error.rb +2 -0
  79. data/lib/react_on_rails/git_utils.rb +2 -0
  80. data/lib/react_on_rails/{react_on_rails_helper.rb → helper.rb} +133 -161
  81. data/lib/react_on_rails/json_output.rb +1 -1
  82. data/lib/react_on_rails/json_parse_error.rb +28 -0
  83. data/lib/react_on_rails/locales/base.rb +150 -0
  84. data/lib/react_on_rails/locales/to_js.rb +37 -0
  85. data/lib/react_on_rails/locales/to_json.rb +27 -0
  86. data/lib/react_on_rails/prerender_error.rb +56 -18
  87. data/lib/react_on_rails/react_component/render_options.rb +31 -3
  88. data/lib/react_on_rails/server_rendering_js_code.rb +42 -0
  89. data/lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb +93 -63
  90. data/lib/react_on_rails/server_rendering_pool.rb +1 -16
  91. data/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +7 -8
  92. data/lib/react_on_rails/test_helper/webpack_assets_compiler.rb +17 -0
  93. data/lib/react_on_rails/test_helper/webpack_assets_status_checker.rb +10 -6
  94. data/lib/react_on_rails/test_helper.rb +18 -7
  95. data/lib/react_on_rails/utils.rb +65 -25
  96. data/lib/react_on_rails/version.rb +1 -1
  97. data/lib/react_on_rails/version_checker.rb +5 -1
  98. data/lib/react_on_rails/version_syntax_converter.rb +14 -12
  99. data/lib/react_on_rails/webpacker_utils.rb +44 -10
  100. data/lib/react_on_rails.rb +7 -2
  101. data/lib/tasks/assets.rake +33 -46
  102. data/lib/tasks/locale.rake +4 -2
  103. data/package-scripts.yml +49 -0
  104. data/package.json +49 -40
  105. data/rakelib/dummy_apps.rake +1 -9
  106. data/rakelib/example_type.rb +3 -1
  107. data/rakelib/examples.rake +3 -0
  108. data/rakelib/lint.rake +2 -7
  109. data/rakelib/node_package.rake +2 -2
  110. data/rakelib/release.rake +4 -8
  111. data/rakelib/run_rspec.rake +5 -18
  112. data/react_on_rails.gemspec +5 -6
  113. data/tsconfig.json +14 -0
  114. data/webpackConfigLoader.js +5 -4
  115. data/yarn.lock +7645 -1821
  116. metadata +70 -60
  117. data/Gemfile.rails32 +0 -74
  118. data/docs/additional-reading/babel.md +0 -5
  119. data/docs/additional-reading/caching-and-performance.md +0 -4
  120. data/docs/additional-reading/heroku-deployment.md +0 -92
  121. data/docs/additional-reading/hot-reloading-rails-development.md +0 -57
  122. data/docs/additional-reading/node-server-rendering.md +0 -5
  123. data/docs/additional-reading/rspec-configuration.md +0 -56
  124. data/docs/api/ruby-api-hot-reload-view-helpers.md +0 -44
  125. data/docs/api/ruby-api.md +0 -8
  126. data/lib/generators/react_on_rails/templates/base/base/Procfile.dev-server +0 -12
  127. data/lib/react_on_rails/assets_precompile.rb +0 -150
  128. data/lib/react_on_rails/locales_to_js.rb +0 -134
  129. /data/docs/{misc → outdated}/rails3.md +0 -0
data/Rakefile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # Rake will automatically load any *.rake files inside of the "rakelib" folder
4
4
  # See rakelib/
5
+
5
6
  tasks = %w[run_rspec lint]
6
7
  prepare_for_ci = %w[node_package dummy_apps]
7
8
 
@@ -11,11 +12,6 @@ if ENV["USE_COVERALLS"] == "TRUE"
11
12
  tasks << "coveralls:push"
12
13
  end
13
14
 
14
- if File.basename(ENV["BUNDLE_GEMFILE"] || "") == "Gemfile.rails32"
15
- tasks = %w[run_rspec:gem_rails32 run_rspec:dummy_no_webpacker]
16
- prepare_for_ci = %w[node_package dummy_apps:dummy_no_webpacker]
17
- end
18
-
19
15
  desc "Run all tests and linting"
20
16
  task default: tasks
21
17
 
data/SUMMARY.md CHANGED
@@ -1,50 +1,63 @@
1
- # Table of Content
1
+ # Table of Contents
2
2
 
3
- ### **Rails**
4
- + [Rails Assets](./docs/additional-reading/rails-assets.md)
3
+ + [Home](./README.md)
4
+
5
+ ## **Basics**
6
+ + [React on Rails Basic Installation Tutorial](./docs/tutorial.md)
7
+ + [Webpack Configuration](./docs/basics/webpack-configuration.md)
8
+ + [How React on Rails Works](docs/outdated/how-react-on-rails-works.md)
9
+ + [Client vs. Server Rendering](./docs/basics/client-vs-server-rendering.md)
10
+ + [React Server Rendering](./docs/basics/react-server-rendering.md)
11
+ + [Recommended Project Structure](./docs/basics/recommended-project-structure.md)
12
+ + [Render-Functions and the RailsContext](docs/basics/render-functions-and-railscontext.md)
13
+ + [Caching and Performance: React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
14
+ + [Deployment](docs/basics/deployment.md).
15
+ + [React on Rails Internationalization (I18n, localization)](docs/basics/i18n.md)
16
+ + [RSpec Test Helpers Configuration](docs/basics/rspec-configuration.md)
17
+ + [Minitest Configuration](docs/basics/minitest-configuration.md)
18
+ + [Upgrading React on Rails](docs/basics/upgrading-react-on-rails.md)
19
+
20
+ ## **API**
21
+ - [View Helpers API](./docs/api/view-helpers-api.md)
22
+ - [JavaScript API](./docs/api/javascript-api.md)
23
+ - [Redux Store API](./docs/api/redux-store-api.md)
24
+
25
+ ## **Additional Details**
26
+ + [Migration from react-rails](./docs/basics/migrating-from-react-rails.md)
27
+ + [Generator Details](docs/basics/generator-details.md)
28
+ + [Updating Dependencies](./docs/additional-reading/updating-dependencies.md)
29
+ + [Manual Installation Overview](docs/outdated/manual-installation-overview.md)
30
+ + [Upgrading from rails/webpacker v3 to v4](docs/additional-reading/upgrade-webpacker-v3-to-v4.md)
31
+
32
+ ## **Rails**
5
33
  + [Rails Engine Integration](./docs/additional-reading/rails-engine-integration.md)
6
34
  + [Rails View Rendering from Inline JavaScript](./docs/additional-reading/rails_view_rendering_from_inline_javascript.md)
7
- + [RSpec Configuration](./docs/additional-reading/rspec-configuration.md)
8
35
  + [Turbolinks](./docs/additional-reading/turbolinks.md)
9
- + [React on Rails Internationalization (I18n)](https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md)
36
+ + [Converting a Rails 5 API only app to a Rails app](./docs/additional-reading/convert-rails-5-api-only-app.md)
10
37
 
11
- ### **Javascript**
38
+ ## **Javascript**
12
39
  + [Node Dependencies, NPM, and Yarn](./docs/additional-reading/node-dependencies-and-npm.md)
13
- + [Babel](./docs/additional-reading/babel.md)
14
40
  + [React Router](./docs/additional-reading/react-router.md)
15
41
  + [React & Redux](./docs/additional-reading/react-and-redux.md)
16
- + [Webpack](./docs/additional-reading/webpack.md)
17
- + [Webpack Configuration](./docs/additional-reading/webpack.md)
18
- + [Webpack Cookbook](https://christianalfoni.github.io/react-webpack-cookbook/index.html)
19
- + [Developing with the Webpack Dev Server](docs/additional-reading/webpack-dev-server.md)
20
- + [Node Server Rendering](./docs/additional-reading/node-server-rendering.md)
42
+ + [Webpack Tips](./docs/additional-reading/webpack.md)
21
43
  + [Server Rendering Tips](./docs/additional-reading/server-rendering-tips.md)
22
- + [Code Splitting](./docs/additional-reading/code-splitting.md)
44
+ + [Code Splitting](docs/outdated/code-splitting.md)
23
45
  + [AngularJS Integration and Migration to React on Rails](./docs/additional-reading/angular-js-integration-migration.md)
24
- + [Webpack, the Asset Pipeline, and Using Assets w/ React](./docs/additional-reading/rails-assets-relative-paths.md)
25
46
 
26
- ### **Development**
27
- + [React on Rails Basic Installation Tutorial](./docs/tutorial.md)
28
- + [Installation Overview](./docs/basics/installation-overview.md)
29
- + [Migration from react-rails](./docs/basics/migrating-from-react-rails.md)
30
- + [Recommended Project Structure](./docs/additional-reading/recommended-project-structure.md)
31
- + [Generator Tips](./docs/basics/generator.md)
32
- + [Hot Reloading of Assets For Rails Development](./docs/additional-reading/hot-reloading-rails-development.md)
33
- + [Heroku Deployment](./docs/additional-reading/heroku-deployment.md)
47
+ ## **Deployment**
48
+ + [Heroku Deployment](docs/basics/heroku-deployment.md)
34
49
  + [Elastic Beanstalk Deployment](./docs/additional-reading/elastic-beanstalk.md)
35
- + [Updating Dependencies](./docs/additional-reading/updating-dependencies.md)
36
50
 
37
- ### **API**
38
- + [JavaScript API](./docs/api/javascript-api.md)
39
- + [Ruby API](./docs/api/ruby-api.md)
40
- + [Setting up Hot Reloading during Rails Development, API docs](./docs/api/ruby-api-hot-reload-view-helpers.md)
51
+ ## Outdated Non-Webpack Docs
52
+ + [Developing with the Webpack Dev Server](./docs/additional-reading/webpack-dev-server.md)
53
+ + [Webpack, the Asset Pipeline, and Using Assets w/ React](./docs/outdated/rails-assets-relative-paths.md)
41
54
 
42
- ### **[CONTRIBUTING](CONTRIBUTING.md)**
55
+ ## **[CONTRIBUTING](CONTRIBUTING.md)**
43
56
  + [Generator Testing](./docs/contributor-info/generator-testing.md)
44
57
  + [Linting](./docs/contributor-info/linters.md)
45
58
  + [Releasing](./docs/contributor-info/releasing.md)
46
59
 
47
- ### **Misc**
60
+ ## **Misc**
48
61
  + [Tips](./docs/additional-reading/tips.md)
49
62
  + [Changelog](./CHANGELOG.md)
50
63
  + [Projects](./PROJECTS.md)
data/book.json CHANGED
@@ -7,12 +7,12 @@
7
7
  "url": "https://github.com/shakacode/react_on_rails/"
8
8
  },
9
9
  "sharing": {
10
- "facebook": true,
11
- "twitter": true,
12
- "google": true,
13
- "weibo": true,
10
+ "facebook": true,
11
+ "twitter": true,
12
+ "google": true,
13
+ "weibo": true,
14
14
  "instapaper": true,
15
- "vk": true
15
+ "vk": true
16
16
  }
17
17
  }
18
18
  }
@@ -1,20 +1,12 @@
1
- # Asset Pipeline
1
+ # Asset Pipeline with React on Rails
2
2
 
3
- The plumbing of webpack produced assets through the asset pipeline is deprecated as of v9.0.
3
+ In general, you should not be mixing the asset pipeline with rails/webpacker and React on Rails.
4
4
 
5
- The information in this document is here for those that have not yet upgraded.
5
+ If you're using React, then all of your CSS and images should be under either `/client` or
6
+ `/app/javascript` or wherever you want your client side application.
6
7
 
8
+ If you are incrementally migrating a large application, your main concern will be how to minimize
9
+ duplication of styles and images between your old application and the new one.
7
10
 
8
-
9
-
10
- This option still works for your `/config/initializers/react_on_rails.rb` if you are still using the
11
- asset pipeline.
12
- ```
13
- ################################################################################
14
- # MISCELLANEOUS OPTIONS
15
- ################################################################################
16
- # If you want to use webpack for CSS and images, and still use the asset pipeline,
17
- # see https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/rails-assets.md
18
- # And you will use a setting like this.
19
- config.symlink_non_digested_assets_regex = /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/
20
- ```
11
+ Please email [justin@shakacode.com](mailto:justin@shakacode.com) if you would be interested in help
12
+ to migrate a larger application.
@@ -0,0 +1,19 @@
1
+ # Convert Rails 5 API only app to a Rails app
2
+
3
+ 1. Go to the directory where you created your app
4
+
5
+ ```bash
6
+ $ rails new your-current-app-name
7
+ ```
8
+
9
+ Rails will start creating the app and will skip the files you have already created. If there is some conflict then it will stop and you need to resolve it manually. be careful at this step as it might replace you current code in conflicted files.
10
+
11
+ 2. Resolve conflicts
12
+
13
+ ```
14
+ 1. Press "d" to see the difference
15
+ 2. If it is only adding lines then press "y" to continue
16
+ 3. If it is removeing some of your code then press "n" and add all additions manually
17
+ ```
18
+
19
+ 3. Run `bundle install` and follow [the instructions for installing into an existing Rails app](../basics/installation-into-an-existing-rails-app.md).*
@@ -0,0 +1,10 @@
1
+ ## Authors
2
+
3
+ [The Shaka Code team!](http://www.shakacode.com/about/)
4
+
5
+ The origins of the project began with the need to do a rich JavaScript interface for a ShakaCode's client. The choice to use Webpack and Rails is described in [Fast Rich Client Rails Development With Webpack and the ES6 Transpiler](http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/).
6
+
7
+ The gem project started with [Justin Gordon](https://github.com/justin808/) pairing with [Samnang Chhun](https://github.com/samnang) to figure out how to do server rendering with Webpack plus Rails. [Alex Fedoseev](https://github.com/alexfedoseev) then joined in. [Rob Wise](https://github.com/robwise), [Aaron Van Bokhoven](https://github.com/aaronvb), and [Andy Wang](https://github.com/yorzi) did the bulk of the generators. Many others have [contributed](https://github.com/shakacode/react_on_rails/graphs/contributors).
8
+
9
+ The gem was initially inspired by the [react-rails gem](https://github.com/reactjs/react-rails).
10
+
@@ -39,7 +39,7 @@ const assetLoaderRules = [
39
39
 
40
40
 
41
41
 
42
- A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.js](../../spec/dummy/client/app/components/ImageExample/ImageExample.js)
42
+ A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.jsx](../../spec/dummy/client/app/components/ImageExample/ImageExample.jsx)
43
43
 
44
44
  You are free to use images either in image tags or as background images in SCSS files. You can
45
45
  use a "global" location of /client/app/assets/images or a relative path to your JS or SCSS file, as
@@ -32,3 +32,10 @@ This is necessary because React on Rails attaches itself to the rake assets:prec
32
32
  Requiring `react_on_rails` and including the helper will get rid of any issues where react on rails or react_component is undefined.
33
33
 
34
34
  As far as solving the assets issue, `lib/tasks/assets.rake` in `react_on_rails` would somehow have to know that `react_on_rails` was included in an engine, and decide the path accordingly. This might be impossible, especially in the case of multiple engines using `react_on_rails` in a single application. Another solution would be to detach this rake task from the rails assets:precompile task, and let people use it separately.
35
+
36
+ # Github Issues
37
+
38
+ * [Integration with an engine #342](https://github.com/shakacode/react_on_rails/issues/342)
39
+ * [Feature: target destination option for the install generator #459](https://github.com/shakacode/react_on_rails/issues/459)
40
+ * [Integration with Rails 5 Engines #562](https://github.com/shakacode/react_on_rails/issues/562)
41
+ * [Run inside a Rails engine? #257](https://github.com/shakacode/react_on_rails/issues/257)
@@ -12,9 +12,10 @@ You can easily render React components in your JavaScript with `render` method t
12
12
  * @param name Name of your registered component
13
13
  * @param props Props to pass to your component
14
14
  * @param domNodeId
15
+ * @param hydrate [optional] Pass truthy to update server rendered html. Default is falsy
15
16
  * @returns {virtualDomElement} Reference to your component's backing instance
16
17
  */
17
- ReactOnRails.render(componentName, props, elementId)
18
+ ReactOnRails.render(componentName, props, domNodeId)
18
19
  ```
19
20
 
20
21
  ## Why do we need this?
@@ -1,12 +1,13 @@
1
1
  # Using React Helmet to build `<head>` content
2
2
 
3
3
  ## Installation and general usage
4
- See https://github.com/nfl/react-helmet for details. Run `yarn add react-helmet` in your `client` directory to add this package to your application.
4
+ See [nfl/react-helmet](https://github.com/nfl/react-helmet) for details on how to use this package.
5
+ Run `yarn add react-helmet` to add this package to your application.
5
6
 
6
7
  ## Example
7
8
  Here is what you need to do in order to configure your Rails application to work with **ReactHelmet**.
8
9
 
9
- Create generator function for server rendering like this:
10
+ Create a render-function for server rendering like this:
10
11
 
11
12
  ```javascript
12
13
  export default (props, _railsContext) => {
@@ -20,17 +21,35 @@ export default (props, _railsContext) => {
20
21
  return { renderedHtml };
21
22
  };
22
23
  ```
23
- You can add more **helmet** properties to result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage.
24
+ You can add more **helmet** properties to the result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage.
24
25
 
25
- Use regular component or generator function for client-side:
26
+ Use a regular React functional or class component or a render-function for your client-side bundle:
26
27
 
27
28
  ```javascript
28
- export default (props, _railsContext) => (
29
+ // React functional component
30
+ export default (props) => (
29
31
  <App {...props} />
30
32
  );
31
33
  ```
32
34
 
33
- Put **ReactHelmet** component somewhere in your `<App>`:
35
+ Or a render-function. Note you can't return just the JSX (React element), but you need to return
36
+ either a React functional or class component.
37
+ ```javascript
38
+ // React functional component
39
+ export default (props, railsContext) => (
40
+ () => <App {{railsContext, ...props}} />
41
+ );
42
+ ```
43
+
44
+ Note, this doesn't work, because this function just returns a React element rather than a React component
45
+ ```javascript
46
+ // React functional component
47
+ export default (props, railsContext) => (
48
+ <App {{railsContext, ...props}} />
49
+ );
50
+ ```
51
+
52
+ Put the **ReactHelmet** component somewhere in your `<App>`:
34
53
  ```javascript
35
54
  import { Helmet } from 'react-helmet';
36
55
 
@@ -55,15 +74,18 @@ ReactOnRails.register({
55
74
  });
56
75
  ```
57
76
  ```javascript
77
+ // Note the import from the server file.
58
78
  import ReactHelmetApp from '../ReactHelmetServerApp';
59
79
 
60
80
  ReactOnRails.register({
61
81
  ReactHelmetApp
62
82
  });
63
83
  ```
64
- Now when the `react_component_hash` helper is called with **"ReactHelmetApp"** as a first argument it will return a hash instead of HTML string:
84
+ Now when the `react_component_hash` helper is called with **"ReactHelmetApp"** as a first argument it
85
+ will return a hash instead of HTML string. Note, there is no need to specify "prerender" as it would not
86
+ make sense to use react_component_hash without server rendering:
65
87
  ```ruby
66
- <% react_helmet_app = react_component_hash("ReactHelmetApp", prerender: true, props: { hello: "world" }, trace: true) %>
88
+ <% react_helmet_app = react_component_hash("ReactHelmetApp", props: { hello: "world" }, trace: true) %>
67
89
 
68
90
  <% content_for :title do %>
69
91
  <%= react_helmet_app['title'] %>
@@ -76,5 +98,3 @@ So now we're able to insert received title tag to our application layout:
76
98
  ```ruby
77
99
  <%= yield(:title) if content_for?(:title) %>
78
100
  ```
79
-
80
- Note: Use of `react_component` for this functionality is deprecated. Please use `react_component_hash` instead.
@@ -1,39 +1,28 @@
1
+ _This article needs updating for the latest version of React Router_
2
+
1
3
  # Using React Router
2
4
 
3
- React on Rails supports the use of React Router. Client-side code doesn't need any special configuration for the React on Rails gem. Implement React Router how you normally would. Note, you might want to avoid using Turbolinks as both Turbolinks and React-Router will be trying to handle the back and forward buttons. If you get this figured out, please do share with the community! Otherwise, you might have to tweak the basic settings for Turbolinks, and this may or may not be worth the effort.
4
5
 
5
- When attempting to use server-rendering, it is necessary to take steps that prevent rendering when there is a router error or redirect. In these cases, the client code should return an object containing the `error` and a `redirectLocation` instead of the React component. The `react_component` helper method in your Rails view will automatically detect that there was an error/redirect and handle it accordingly.
6
+ React on Rails supports the use of React Router. Client-side code doesn't need any special configuration for the React on Rails gem. Implement React Router how you normally would. Note, you might want to avoid using Turbolinks as both Turbolinks and React-Router will be trying to handle the back and forward buttons. If you get this figured out, please do share with the community! Otherwise, you might have to tweak the basic settings for Turbolinks, and this may or may not be worth the effort.
6
7
 
7
8
  If you are working with the HelloWorldApp created by the react_on_rails generator, then the code below corresponds to the module in `client/app/bundles/HelloWorld/startup/HelloWorldApp.jsx`.
8
9
 
9
10
  ```js
11
+
12
+ import { BrowserRouter, Switch } from 'react-router-dom'
13
+ import routes from './routes.jsx'
14
+
10
15
  const RouterApp = (props, railsContext) => {
11
- let error;
12
- let redirectLocation;
13
- let routeProps;
14
- const { location } = railsContext;
15
-
16
16
  // create your hydrated store
17
17
  const store = createStore(props);
18
-
19
- // See https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md
20
- match({ routes, location }, (_error, _redirectLocation, _routeProps) => {
21
- error = _error;
22
- redirectLocation = _redirectLocation;
23
- routeProps = _routeProps;
24
- });
25
-
26
- // This tell react_on_rails to skip server rendering any HTML. Note, client rendering
27
- // will handle the redirect. What's key is that we don't try to render.
28
- // Critical to return the Object properties to match this { error, redirectLocation }
29
- if (error || redirectLocation) {
30
- return { error, redirectLocation };
31
- }
32
-
33
- // Important that you don't do this if you are redirecting or have an error.
18
+
34
19
  return (
35
20
  <Provider store={store}>
36
- <RouterContext {...routeProps} />
21
+ <BrowserRouter>
22
+ <Switch>
23
+ {routes}
24
+ </Switch>
25
+ </BrowserRouter>
37
26
  </Provider>
38
27
  );
39
28
  };
@@ -50,64 +39,52 @@ For a fleshed out integration of react_on_rails with react-router, check out [Re
50
39
 
51
40
  # Server Rendering Using React Router V4
52
41
 
53
- Your generator 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
54
43
  renderToString() and return an object with this property.
55
44
 
56
45
  This example **only applies to server rendering** and should be only used in the server side bundle.
57
46
 
58
- From the [original example in the ReactRouter docs](https://react-router.now.sh/ServerRouter)
47
+ From the [original example in the ReactRouter docs](https://github.com/ReactTraining/react-router/blob/v4.3.1/packages/react-router-dom/docs/guides/server-rendering.md)
59
48
 
60
49
  ```javascript
61
50
  import React from 'react'
62
51
  import { renderToString } from 'react-dom/server'
63
- import { ServerRouter, createServerRenderContext } from 'react-router'
64
-
65
- const ReactRouterComponent = (props, railsContext) => {
66
-
67
- // first create a context for <ServerRouter>, it's where we keep the
68
- // results of rendering for the second pass if necessary
69
- const context = createServerRenderContext()
70
- const { location } = railsContext;
71
-
72
- // render the first time
73
- let markup = renderToString(
74
- <ServerRouter
75
- location={location}
76
- context={context}
77
- >
78
- <App/>
79
- </ServerRouter>
80
- )
81
-
82
- // get the result
83
- const result = context.getResult()
84
-
85
- // the result will tell you if it redirected, if so, we ignore
86
- // the markup and send a proper redirect.
87
- if (result.redirect) {
88
- return {
89
- redirectLocation: result.redirect.pathname
90
- };
91
- } else {
92
-
93
- // the result will tell you if there were any misses, if so
94
- // we can send a 404 and then do a second render pass with
95
- // the context to clue the <Miss> components into rendering
96
- // this time (on the client they know from componentDidMount)
97
- if (result.missed) {
98
- // React on Rails does not support the 404 status code for the browser.
99
- // res.writeHead(404)
100
-
101
- markup = renderToString(
102
- <ServerRouter
103
- location={location}
104
- context={context}
105
- >
106
- <App/>
107
- </ServerRouter>
108
- )
109
- }
110
- return { renderedHtml: markup };
111
- }
52
+ import { StaticRouter } from 'react-router'
53
+ import { Provider } from 'react-redux'
54
+ import ReactOnRails from 'react-on-rails'
55
+
56
+ // App.jsx from src/client/App.jsx
57
+ import App from '../App'
58
+
59
+ const ReactServerRenderer = (props, railsContext) => {
60
+ const context = {}
61
+
62
+ // commentStore from src/server/store/commentStore
63
+ const store = ReactOnRails.getStore('../store/commentStore')
64
+
65
+ // Route Store generated from react-on-rails
66
+
67
+ const { location } = railsContext
68
+
69
+ const html = ReactDOMServer.renderToString(
70
+ <Provider store={store}>
71
+ <StaticRouter
72
+ location={location}
73
+ context={context}
74
+ props={props}
75
+ >
76
+ <App />
77
+ </StaticRouter>
78
+ </ Provider>
79
+ )
80
+
81
+ if (context.url) {
82
+ // Somewhere a `<Redirect>` was rendered
83
+ redirect(301, context.url)
84
+ } else {
85
+ // we're good, send the response
86
+ return { renderedHtml: html };
87
+ }
88
+ }
112
89
  }
113
90
  ```
@@ -1,18 +1,21 @@
1
1
  # Server Rendering Tips
2
2
 
3
- Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_on_rails/issues/428)
3
+ For the best performance with Server Rendering, consider using [React on Rails Pro]
4
+
5
+ Be sure to use mini_racer. See [issues/428](https://github.com/shakacode/react_on_rails/issues/428).
6
+
7
+
4
8
 
5
9
  ## General Tips
6
- - Your code can't reference `document`. Server side JS execution does not have access to `document`, so jQuery and some
7
- other libs won't work in this environment. You can debug this by putting in `console.log`
8
- statements in your code.
9
- - You can conditionally avoid running code that references document by passing in a boolean prop to your top level react
10
- component. Since the passed in props Hash from the view helper applies to client and server side code, the best way to
11
- do this is to use a generator function.
12
- - If you're serious about server rendering, it's worth the effort to have different entry points for client and server rendering. It's worth the extra complexity.
13
- - You can enable node server rendering if you want. See more information here: https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/node-server-rendering.md
14
-
15
- The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server side.
10
+ - Your code can't reference `document`. Server side JS execution does not have access to `document`,
11
+ so jQuery and some other libs won't work in this environment. You can debug this by putting in
12
+ `console.log` statements in your code.
13
+ - You can conditionally avoid running code that references document by either checking if `window`
14
+ is defined or using the "railsContext"
15
+ your top level react component. Since the passed in props Hash from the view helper applies to
16
+ client and server side code, the best way to do this is to use a render function.
17
+ - If you're serious about server rendering, it's worth the effort to have different entry points for client and server rendering. It's worth the extra complexity. The point is that you have separate files for top level client or server side, and you pass some extra option indicating that rendering is happening server side.
18
+ - You can enable Node.js server rendering via [React on Rails Pro](https://github.com/shakacode/react_on_rails/wiki).
16
19
 
17
20
  ## Troubleshooting Server Rendering
18
21
 
@@ -0,0 +1,10 @@
1
+ # Upgrading rails/webpacker v3.5 to v4
2
+
3
+ The following steps can be followed to update a Webpacker v3.5 app to v4.
4
+
5
+ 1. Update the gem `webpacker` and the package `@rails/webpacker`
6
+ 1. Merge changes from the new default [.babelrc](../lib/install/config/.babelrc) to your `/.babelrc`. If you are using React, you need to add `"@babel/preset-react"`, to the list of `presets`.
7
+ 1. Copy the file [.browserslistrc](../lib/install/config/.browserslistrc) to `/`.
8
+ 1. Merge any differences between [config/webpacker.yml](../lib/install/config/webpacker.yml) and your `/config/webpacker.yml`.
9
+
10
+ Here is an [example commit of these changes](https://github.com/shakacode/react_on_rails-tutorial-v11/pull/1/files).
@@ -18,5 +18,5 @@ Webpack v2 makes this very convenient! See:
18
18
  * [Implicit Common Vendor Chunk](https://webpack.js.org/guides/code-splitting-libraries/#implicit-common-vendor-chunk)
19
19
  * [Manifest File](https://webpack.js.org/guides/code-splitting-libraries/#manifest-file)
20
20
 
21
-
22
-
21
+ ## Webpack v4
22
+ Webpack v4 is heartily recommended. If you need help with migrating your project to Webpack v4, please contact me, [justin@shakacode.com](mailto:justin@shakacode.com).
@@ -1,14 +1,34 @@
1
1
  # ReactOnRails JavaScript API
2
- The best source of docs is the main [ReactOnRails.js](../../node_package/src/ReactOnRails.js) file. Here's a quick summary. No guarantees that this won't be outdated!
2
+ ## CSRF protection
3
+
4
+ Rails has built-in protection for Cross-Site Request Forgery (CSRF), see [Rails Documentation](http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf). To nicely utilize this feature in JavaScript requests, React on Rails provides two helpers that can be used as following for POST, PUT or DELETE requests:
5
+
6
+ ```js
7
+ import ReactOnRails from 'react-on-rails';
8
+
9
+ // reads from DOM csrf token generated by Rails in <%= csrf_meta_tags %>
10
+ csrfToken = ReactOnRails.authenticityToken();
11
+
12
+ // compose Rails specific request header as following { X-CSRF-Token: csrfToken, X-Requested-With: XMLHttpRequest }
13
+ header = ReactOnRails.authenticityHeaders(otherHeader);
14
+ ```
15
+
16
+ If you are using [jquery-ujs](https://github.com/rails/jquery-ujs) for AJAX calls, then these helpers are not needed because the [jquery-ujs](https://github.com/rails/jquery-ujs) library updates header automatically, see [jquery-ujs documentation](https://robots.thoughtbot.com/a-tour-of-rails-jquery-ujs#cross-site-request-forgery-protection).
17
+
18
+
19
+
20
+ ## API
21
+
22
+ The best source of docs is the main [ReactOnRails.js](https://github.com/shakacode/react_on_rails/node_package/src/ReactOnRails.js) file. Here's a quick summary. No guarantees that this won't be outdated!
3
23
 
4
24
  ```js
5
25
  /**
6
26
  * Main entry point to using the react-on-rails npm package. This is how Rails will be able to
7
27
  * find you components for rendering. Components get called with props, or you may use a
8
- * "generator function" to return a React component or an object with the following shape:
28
+ * "render function" to return a React component or an object with the following shape:
9
29
  * { renderedHtml, redirectLocation, error }.
10
- * For server rendering, if you wish to return multiple HTML strings from a generator function,
11
- * you may return an Object from your generator function with a single top level property of
30
+ * For server rendering, if you wish to return multiple HTML strings from a render function,
31
+ * you may return an Object from your render function with a single top level property of
12
32
  * renderedHtml. Inside this Object, place a key called componentHtml, along with any other
13
33
  * needed keys. This is useful when you using side effects libraries like react helmet.
14
34
  * Your Ruby code with get this Object as a Hash containing keys componentHtml and any other