Skip to content

Commit dda1405

Browse files
alexeyr-ci2alexeyralexeyr-ciJudahmeek
authored
Update react-on-rails import to /client in 14.2.x (#1731)
* Update the optimization opportunity warning * Update react-on-rails import to /client as needed * Update pack generator * Fix remaining warning * Fix generator tests (#1706) * Allow for different shakapacker versions in convert script * Fix the generator tests --------- Co-authored-by: Alexey Romanov <alexey.v.romanov@gmail.com> Co-authored-by: Judah Meek <judah.meek@gmail.com> --------- Co-authored-by: Alexey Romanov <alexey.v.romanov@gmail.com> Co-authored-by: Alexey Romanov <104450112+alexeyr-ci@users.noreply.github.com> Co-authored-by: Judah Meek <judah.meek@gmail.com>
1 parent f8773fc commit dda1405

18 files changed

+34
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Changes since the last non-beta release.
2222

2323
#### Fixed
2424
- Fixed a bug where the `load` event was not firing in Safari by postponing hydration to the next JavaScript task using `setTimeout(callback, 0)`. [PR 1729](https://github.com/shakacode/react_on_rails/pull/1729) by [Romex91](https://github.com/Romex91).
25+
- Generated client packs now import from `react-on-rails/client` instead of `react-on-rails`. [PR 1706](https://github.com/shakacode/react_on_rails/pull/1706) by [alexeyr-ci](https://github.com/alexeyr-ci).
26+
- The "optimization opportunity" message when importing the server-side `react-on-rails` instead of `react-on-rails/client` in browsers is now a warning for two reasons:
27+
- Make it more prominent
28+
- Include a stack trace when clicked
2529

2630
### [14.2.0] - 2025-03-03
2731

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
react_on_rails (14.1.1)
4+
react_on_rails (14.2.1)
55
addressable
66
connection_pool
77
execjs (~> 2.5)

docs/guides/how-to-use-different-files-for-client-and-server-rendering.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Many projects will have different entry points for client and server rendering.
88
Your Client Entry can look like this:
99

1010
```js
11-
import ReactOnRails from 'react-on-rails';
11+
import ReactOnRails from 'react-on-rails/client';
1212
import App from './ClientApp';
1313
ReactOnRails.register({ App })
1414
```
@@ -21,7 +21,7 @@ import App from './ServerApp';
2121
ReactOnRails.register({ App })
2222
```
2323

24-
Note that the only difference is on the second line of each of these examples.
24+
Note that the only difference is in the imports.
2525

2626
## B. Two Options for Using Webpack Resolve Alias in the Webpack Config
2727
Per [Webpack Docs](https://webpack.js.org/configuration/resolve/#resolve-alias).

docs/javascript/code-splitting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Here's an example of how you might use this in practice:
3737

3838
#### clientRegistration.js
3939
```js
40-
import ReactOnRails from 'node_package/lib/ReactOnRails';
40+
import ReactOnRails from 'react-on-rails/client';
4141
import NavigationApp from './NavigationApp';
4242

4343
// Note that we're importing a different RouterApp than in serverRegistration.js
@@ -71,7 +71,7 @@ Note that you should not register a renderer on the server, since there won't be
7171

7272
#### RouterAppRenderer.jsx
7373
```jsx
74-
import ReactOnRails from 'react-on-rails';
74+
import ReactOnRails from 'react-on-rails/client';
7575
import React from 'react';
7676
import ReactDOM from 'react-dom';
7777
import Router from 'react-router/lib/Router';

lib/generators/react_on_rails/dev_tests_generator.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ def replace_prerender_if_server_rendering
4747

4848
def add_yarn_relative_install_script_in_package_json
4949
package_json = File.join(destination_root, "package.json")
50-
contents = File.read(package_json)
51-
replacement_value = <<-STRING
52-
"scripts": {
53-
"postinstall": "yalc link react-on-rails",
54-
STRING
55-
new_client_package_json_contents = contents.gsub(/ {2}"scripts": {/,
56-
replacement_value)
57-
File.open(package_json, "w+") { |f| f.puts new_client_package_json_contents }
50+
contents = JSON.parse(File.read(package_json))
51+
contents["scripts"] ||= {}
52+
contents["scripts"]["postinstall"] = "yalc link react-on-rails"
53+
File.open(package_json, "w+") { |f| f.puts JSON.pretty_generate(contents) }
5854
end
5955
end
6056
end

lib/generators/react_on_rails/templates/base/base/app/javascript/packs/registration.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ReactOnRails from 'react-on-rails';
1+
import ReactOnRails from 'react-on-rails/client';
22

33
import <%= config[:component_name] %> from '<%= config[:app_relative_path] %>';
44

lib/react_on_rails/packs_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def create_pack(file_path)
4747
def pack_file_contents(file_path)
4848
registered_component_name = component_name(file_path)
4949
<<~FILE_CONTENT
50-
import ReactOnRails from 'react-on-rails';
50+
import ReactOnRails from 'react-on-rails/client';
5151
import #{registered_component_name} from '#{relative_component_path_from_generated_pack(file_path)}';
5252
5353
ReactOnRails.register({#{registered_component_name}});

node_package/src/ReactOnRails.full.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import type {
99
import Client from './ReactOnRails.client';
1010

1111
if (typeof window !== 'undefined') {
12-
console.log('Optimization opportunity: "react-on-rails" includes ~14KB of server-rendering code. Browsers may not need it. See https://forum.shakacode.com/t/how-to-use-different-versions-of-a-file-for-client-and-server-rendering/1352 (Requires creating a free account)');
12+
// warn to include a collapsed stack trace
13+
console.warn(
14+
'Optimization opportunity: "react-on-rails" includes ~14KB of server-rendering code. Browsers may not need it. See https://forum.shakacode.com/t/how-to-use-different-versions-of-a-file-for-client-and-server-rendering/1352 (Requires creating a free account). Click this for the stack trace.',
15+
);
1316
}
1417

1518
/**

script/convert

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ new_config = File.expand_path("../spec/dummy/config/webpacker.yml", __dir__)
1313

1414
File.rename(old_config, new_config)
1515

16-
gsub_file_content("../Gemfile.development_dependencies", 'gem "shakapacker", "8.0.0"', 'gem "shakapacker", "6.6.0"')
16+
gsub_file_content("../Gemfile.development_dependencies", /gem "shakapacker", "[^"]*"/, 'gem "shakapacker", "6.6.0"')
1717

1818
# The below packages don't work on the oldest supported Node version and aren't needed there anyway
1919
gsub_file_content("../package.json", /"knip": "[^"]*",/, "")
2020
gsub_file_content("../package.json", %r{"@arethetypeswrong/cli": "[^"]*",}, "")
2121

22-
gsub_file_content("../spec/dummy/package.json", '"shakapacker": "8.0.0",', '"shakapacker": "6.6.0",')
22+
gsub_file_content("../spec/dummy/package.json", /"shakapacker": "[^"]*",/, '"shakapacker": "6.6.0",')
2323

2424
gsub_file_content("../spec/dummy/config/webpack/commonWebpackConfig.js", /generateWebpackConfig(\(\))?/,
2525
"webpackConfig")

spec/dummy/app/views/pages/client_side_hello_world.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<br/>
2323
<pre>
2424
import HelloWorld from '../components/HelloWorld';
25-
import ReactOnRails from 'react-on-rails';
25+
import ReactOnRails from 'react-on-rails/client';
2626
ReactOnRails.register({ HelloWorld });
2727
</pre>
2828
</li>

0 commit comments

Comments
 (0)