Skip to content

Commit 5b0f1f7

Browse files
justin808claude
andcommitted
Address review comments: Enforce immediate_hydration fallback for non-Pro users
This commit addresses several issues identified in code review: 1. Fixed immediate_hydration logic in RenderOptions - Non-Pro users setting immediate_hydration: true now get false enforced - Previously returned true despite warning about fallback - Added explicit return false to match warning message behavior 2. Added warning logic to redux_store methods - Both helper.rb and controller.rb now warn non-Pro users - Enforces fallback by overriding to false when Pro license missing - Provides consistent behavior with react_component helper 3. Updated test expectations - render_options_spec.rb now expects false (not true) for non-Pro users - Added comprehensive redux_store tests for warning behavior - Tests cover all three scenarios: true, false, and nil values 4. Clarified CHANGELOG.md - Explicitly states fallback is enforced (value overridden to false) - Covers both components and stores in the migration notes All tests passing and RuboCop clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 3d68c4c commit 5b0f1f7

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Changes since the last non-beta release.
6767
- Pro users: No action needed - immediate hydration is now enabled automatically for optimal performance
6868
- Non-Pro users: No action needed - standard hydration behavior continues to work as before
6969
- Component-level overrides: You can still override behavior per-component using `react_component("MyComponent", immediate_hydration: false)` or `redux_store("MyStore", immediate_hydration: true)`
70-
- If a non-Pro user explicitly sets `immediate_hydration: true` on a component, a warning will be logged and the component will fall back to standard hydration
70+
- If a non-Pro user explicitly sets `immediate_hydration: true` on a component or store, a warning will be logged and it will be enforced to fall back to standard hydration (the value will be overridden to `false`)
7171

7272
[PR 1997](https://github.com/shakacode/react_on_rails/pull/1997) by [AbanoubGhadban](https://github.com/AbanoubGhadban).
7373

lib/react_on_rails/controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@ module Controller
1515
# Be sure to include view helper `redux_store_hydration_data` at the end of your layout or view
1616
# or else there will be no client side hydration of your stores.
1717
def redux_store(store_name, props: {}, immediate_hydration: nil)
18-
immediate_hydration = ReactOnRails::Utils.react_on_rails_pro? if immediate_hydration.nil?
18+
# If non-Pro user explicitly sets immediate_hydration: true, warn and override to false
19+
if immediate_hydration == true && !ReactOnRails::Utils.react_on_rails_pro?
20+
Rails.logger.warn <<~WARNING
21+
[REACT ON RAILS] Warning: immediate_hydration: true requires a React on Rails Pro license.
22+
Store '#{store_name}' will fall back to standard hydration behavior.
23+
Visit https://www.shakacode.com/react-on-rails-pro/ for licensing information.
24+
WARNING
25+
immediate_hydration = false
26+
elsif immediate_hydration.nil?
27+
immediate_hydration = ReactOnRails::Utils.react_on_rails_pro?
28+
end
29+
1930
redux_store_data = { store_name: store_name,
2031
props: props,
2132
immediate_hydration: immediate_hydration }

lib/react_on_rails/helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,17 @@ def react_component_hash(component_name, options = {})
158158
# immediate_hydration: nil -- React on Rails Pro (licensed) feature. When nil (default), Pro users
159159
# get immediate hydration, non-Pro users don't. Can be explicitly overridden.
160160
def redux_store(store_name, props: {}, defer: false, immediate_hydration: nil)
161-
immediate_hydration = ReactOnRails::Utils.react_on_rails_pro? if immediate_hydration.nil?
161+
# If non-Pro user explicitly sets immediate_hydration: true, warn and override to false
162+
if immediate_hydration == true && !ReactOnRails::Utils.react_on_rails_pro?
163+
Rails.logger.warn <<~WARNING
164+
[REACT ON RAILS] Warning: immediate_hydration: true requires a React on Rails Pro license.
165+
Store '#{store_name}' will fall back to standard hydration behavior.
166+
Visit https://www.shakacode.com/react-on-rails-pro/ for licensing information.
167+
WARNING
168+
immediate_hydration = false
169+
elsif immediate_hydration.nil?
170+
immediate_hydration = ReactOnRails::Utils.react_on_rails_pro?
171+
end
162172

163173
redux_store_data = { store_name: store_name,
164174
props: props,

lib/react_on_rails/react_component/render_options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ def logging_on_server
9898
def immediate_hydration
9999
explicit_value = options[:immediate_hydration]
100100

101-
# Warn if non-Pro user explicitly sets immediate_hydration: true
101+
# If non-Pro user explicitly sets immediate_hydration: true, warn and override to false
102102
if explicit_value == true && !ReactOnRails::Utils.react_on_rails_pro?
103103
Rails.logger.warn <<~WARNING
104104
[REACT ON RAILS] Warning: immediate_hydration: true requires a React on Rails Pro license.
105105
Component '#{react_component_name}' will fall back to standard hydration behavior.
106106
Visit https://www.shakacode.com/react-on-rails-pro/ for licensing information.
107107
WARNING
108+
return false # Force fallback to standard hydration
108109
end
109110

110111
options.fetch(:immediate_hydration) do

spec/dummy/spec/helpers/react_on_rails_helper_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,43 @@ def helper.append_javascript_pack_tag(name, **options)
431431
it {
432432
expect(expect(store).target).to script_tag_be_included(react_store_script)
433433
}
434+
435+
context "without Pro license" do
436+
before do
437+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
438+
end
439+
440+
context "with immediate_hydration option set to true (not recommended)" do
441+
it "returns false for immediate_hydration and logs a warning" do
442+
expect(Rails.logger).to receive(:warn).with(/immediate_hydration: true requires a React on Rails Pro license/)
443+
444+
result = redux_store("reduxStore", props: props, immediate_hydration: true)
445+
446+
# Verify that the store tag does NOT have immediate hydration enabled
447+
expect(result).not_to include('data-immediate-hydration="true"')
448+
end
449+
end
450+
451+
context "with immediate_hydration option set to false" do
452+
it "returns false for immediate_hydration without warning" do
453+
expect(Rails.logger).not_to receive(:warn)
454+
455+
result = redux_store("reduxStore", props: props, immediate_hydration: false)
456+
457+
# Verify that the store tag does NOT have immediate hydration enabled
458+
expect(result).not_to include('data-immediate-hydration="true"')
459+
end
460+
end
461+
462+
context "without immediate_hydration option (nil)" do
463+
it "defaults to false for non-Pro users" do
464+
result = redux_store("reduxStore", props: props)
465+
466+
# Verify that the store tag does NOT have immediate hydration enabled
467+
expect(result).not_to include('data-immediate-hydration="true"')
468+
end
469+
end
470+
end
434471
end
435472

436473
describe "#server_render_js", :js, type: :system do

spec/react_on_rails/react_component/render_options_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ def the_attrs(react_component_name: "App", options: {})
209209
end
210210

211211
context "with immediate_hydration option set to true (not recommended)" do
212-
it "returns true but logs a warning" do
212+
it "returns false and logs a warning (enforces fallback)" do
213213
options = { immediate_hydration: true }
214214
attrs = the_attrs(options: options)
215215

216216
expect(Rails.logger).to receive(:warn).with(/immediate_hydration: true requires a React on Rails Pro license/)
217217

218218
opts = described_class.new(**attrs)
219219

220-
expect(opts.immediate_hydration).to be true
220+
expect(opts.immediate_hydration).to be false
221221
end
222222
end
223223

0 commit comments

Comments
 (0)