-
Notifications
You must be signed in to change notification settings - Fork 380
Fix FOUC by moving stylesheets to head #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughReworks both layouts to build main body content into a Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Browser
participant Rails as Rails Controller
participant Layout as Layout (application / stimulus)
participant View as View Template
Browser->>Rails: GET page
Rails->>Layout: Render layout
Layout->>Layout: Render <head>:\n- append_stylesheet_pack_tag / append_javascript_pack_tag (Stimulus)\n- stylesheet_pack_tag (media: all, data-turbolinks-track)\n- javascript_pack_tag (defer, data-turbolinks-track)
Layout->>View: Render view/body\n(inside View: content_for :body_content may be defined)
View-->>Layout: content_for :body_content returned
Layout->>Browser: Send HTML with assembled head and `<%= yield :body_content %>` in body
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🚀 Quick Review App CommandsWelcome! Here are the commands you can use in this PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/views/layouts/application.html.erb
(1 hunks)config/initializers/react_on_rails.rb
(1 hunks)
🔇 Additional comments (2)
config/initializers/react_on_rails.rb (1)
7-7
: Configuration change looks good.Disabling
auto_load_bundle
correctly prevents automatic stylesheet appending during component rendering, which enables movingstylesheet_pack_tag
to the head. Ensure all required packs are now manually specified in the layout files.app/views/layouts/application.html.erb (1)
24-24
: Specify pack name in javascript_pack_tag.Similar to line 9, this
javascript_pack_tag
call lacks an explicit pack name. It should likely specify which pack to load:<%= javascript_pack_tag('application', 'data-turbolinks-track': true, defer: true) %>Verify which JavaScript pack should be loaded here and whether the current syntax correctly loads the intended assets.
PR Review: Fix FOUC by moving stylesheets to headSummaryThis PR addresses a Flash of Unstyled Content (FOUC) issue by relocating stylesheet loading from the end of the Code Quality & Best Practices ✅Strengths:
Observations:
Potential Issues & Considerations1. Missing pack argument in
|
Code Review - PR #686: Fix FOUC by moving stylesheets to headSummaryThis PR addresses the Flash of Unstyled Content (FOUC) issue by relocating stylesheet loading from the end of Code Quality & Best Practices✅ Strengths:
|
Problem: - Stylesheets were loading at bottom of body, causing FOUC with SSR - Moving stylesheet_pack_tag to head failed because react_component with auto_load_bundle=true would call append_stylesheet_pack_tag during rendering, which must come before stylesheet_pack_tag Solution: - Set auto_load_bundle = false in react_on_rails config - Manually specify required packs in layout (stimulus-bundle) - Move stylesheet_pack_tag to head after append calls - This ensures styles load before content renders, preventing FOUC 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
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]>
…ack tags Solution: - Wrap body content in content_for :body_content at TOP of layout - This executes all react_component calls first, triggering auto-appends - Then render head with append calls followed by main pack tags - Finally yield body content Execution order: 1. content_for :body_content executes (all react_component auto-appends happen) 2. Explicit append_*_pack_tag calls in head 3. stylesheet_pack_tag and javascript_pack_tag in head (with all appends registered) 4. yield :body_content outputs the body Result: Stylesheets load in head, eliminating FOUC, while preserving auto_load_bundle functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
PR Review: Fix FOUC by moving stylesheets to headOverviewThis PR addresses Flash of Unstyled Content (FOUC) by restructuring the layout templates to load stylesheets in the Code Quality & Best Practices✅ Strengths
🔍 Observations & Suggestions
Potential Issues
|
Code Review - PR #686: Fix FOUC by moving stylesheets to headOverviewThis PR addresses Flash of Unstyled Content (FOUC) by relocating stylesheet and JavaScript pack tags from the body to the ✅ Strengths
|
/deploy-review-app |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #686, commit 4cc2aeb 🎮 Control Plane Console |
✅ Review app for PR #686 was successfully deleted |
Summary
stylesheet_pack_tag
to<head>
sectioncontent_for :body_content
pattern to ensure proper execution orderauto_load_bundle = true
for automatic component pack loadingProblem
Stylesheets were loading at the end of the body (after SSR content), causing noticeable FOUC where navigation and content appeared unstyled initially, then styles flashed in after CSS loaded.
Root Cause
With
auto_load_bundle = true
, React on Rails'react_component
helper automatically callsappend_stylesheet_pack_tag
during rendering. Since Shakapacker requires append helpers to execute beforestylesheet_pack_tag
, we couldn't simply move the stylesheet tag to the head - the body would render after the head, causing appends to happen too late.Solution
Render the body content FIRST using
content_for
, then render the head with pack tags:Execution order:
content_for :body_content
executes first, rendering allreact_component
calls and triggering their auto-appendsappend_*_pack_tag
calls in headstylesheet_pack_tag
andjavascript_pack_tag
render with ALL appends registeredyield :body_content
outputs the pre-rendered bodyThis ensures:
append_*_pack_tag
calls (explicit + auto from react_component) happen firstauto_load_bundle
stays enabled for automatic component packsChanges
app/views/layouts/application.html.erb
: Wrapped body incontent_for :body_content
, moved pack tags to headapp/views/layouts/stimulus_layout.html.erb
: Same pattern appliedconfig/initializers/react_on_rails.rb
: No changes - keptauto_load_bundle = true
Impact
auto_load_bundle
still enabledRelated Issues
Test Plan
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]
This change is