Skip to content

Commit 0dce1d0

Browse files
justin808claude
andcommitted
Fix FOUC using content_for to ensure proper pack tag ordering
Problem: - Stylesheets at bottom of body caused FOUC with SSR - Moving to head failed because react_component (with auto_load_bundle) calls append_*_pack_tag during render, which must execute before the main *_pack_tag helpers Solution: - Use content_for :head block in body for explicit append calls - yield :head in head section before calling main pack tags - This ensures all appends (explicit + auto from react_component) execute before main pack tags, allowing styles in head - Keeps auto_load_bundle enabled for automatic component packs Changes: - Wrap append_*_pack_tag calls in content_for :head blocks - Add yield :head in head section before pack tags - Move stylesheet_pack_tag and javascript_pack_tag to head - Applied to both application.html.erb and stimulus_layout.html.erb Result: Stylesheets load in head, eliminating FOUC while maintaining auto_load_bundle functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent bfb4db4 commit 0dce1d0

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

app/views/layouts/application.html.erb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>RailsReactTutorial</title>
77

8-
<%= append_stylesheet_pack_tag('stimulus-bundle') %>
8+
<%= yield :head %>
99
<%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %>
10-
<%= append_javascript_pack_tag('stimulus-bundle') %>
11-
<%= append_javascript_pack_tag('stores-registration') %>
10+
<%= javascript_pack_tag('data-turbolinks-track': true, defer: true) %>
1211

1312
<%= csrf_meta_tags %>
1413
</head>
1514
<body class="min-h-screen flex flex-col bg-sky-50 text-gray-700">
15+
<% content_for :head do %>
16+
<%= append_stylesheet_pack_tag('stimulus-bundle') %>
17+
<%= append_javascript_pack_tag('stimulus-bundle') %>
18+
<%= append_javascript_pack_tag('stores-registration') %>
19+
<% end %>
20+
1621
<%= react_component "NavigationBarApp" %>
1722

1823
<div class="container mx-auto px-4 flex-grow">
@@ -21,8 +26,6 @@
2126

2227
<%= react_component "Footer" %>
2328

24-
<%= javascript_pack_tag('data-turbolinks-track': true, defer: true) %>
25-
2629
<!-- This is a placeholder for ReactOnRails to know where to render the store props for
2730
client side hydration -->
2831
<%= redux_store_hydration_data %>

app/views/layouts/stimulus_layout.html.erb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>RailsReactTutorial</title>
77

8-
<%= append_stylesheet_pack_tag('stimulus-bundle') %>
9-
<%= append_javascript_pack_tag('stimulus-bundle') %>
8+
<%= yield :head %>
9+
<%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %>
10+
<%= javascript_pack_tag('data-turbolinks-track': true, defer: true) %>
1011

1112
<%= csrf_meta_tags %>
1213
</head>
1314
<body class="min-h-screen flex flex-col bg-sky-50 text-gray-700">
15+
<% content_for :head do %>
16+
<%= append_stylesheet_pack_tag('stimulus-bundle') %>
17+
<%= append_javascript_pack_tag('stimulus-bundle') %>
18+
<% end %>
19+
1420
<%= react_component "NavigationBarApp" %>
1521

1622
<div class="container mx-auto px-4 flex-grow">
1723
<%= yield %>
1824
</div>
1925

2026
<%= react_component "Footer" %>
21-
22-
<%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %>
23-
<%= javascript_pack_tag('data-turbolinks-track': true, defer: true) %>
2427
</body>
2528
</html>
2629

config/initializers/react_on_rails.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ReactOnRails.configure do |config|
55
# Auto-registration configuration for v16
66
config.components_subdirectory = "ror_components"
7-
config.auto_load_bundle = false
7+
config.auto_load_bundle = true
88

99
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
1010
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"

0 commit comments

Comments
 (0)