-
Notifications
You must be signed in to change notification settings - Fork 380
Fix FOUC by moving stylesheet loading to head (#TBD) #684
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
WalkthroughMoved Changes
Sequence Diagram(s)sequenceDiagram
participant Browser
participant RailsView as Layout ERB
Note over RailsView: Before — stylesheet_pack_tag emitted in <body>
Browser->>RailsView: GET /
RailsView-->>Browser: HTML (CSS tag in <body>)
Note over Browser: CSS loads late (possible FOUC)
par After change
Note over RailsView: Now emits stylesheet_pack_tag in <head>
Browser->>RailsView: GET /
RailsView-->>Browser: HTML (CSS tag in <head>)
Note over Browser: CSS loads earlier during head parse
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🔇 Additional comments (2)
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 |
Code Review - PR #684: Fix FOUC by moving stylesheet to headSummaryThis PR moves the stylesheet_pack_tag from the bottom of the body to the head section to fix Flash of Unstyled Content (FOUC). The change is well-justified and technically correct. Strengths
Code Quality ReviewFile: app/views/layouts/application.html.erb:9
Potential Issues and Questions
Security Considerations✅ No security concerns - only moves an existing tag. Performance Considerations✅ Positive impact: Moving CSS to head improves perceived performance. Browsers can parse CSS before rendering content, preventing reflows and repaints. Test CoverageRecommendationsBefore merging:
Nice to have:
VerdictLGTM with minor verification needed ✅ The core change is correct and follows best practices. Please verify the pack name parameter behavior and consider the Turbolinks/Turbo syntax update. Once confirmed, this is ready to merge. Great work identifying and fixing this FOUC issue! |
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
🧹 Nitpick comments (1)
app/views/layouts/application.html.erb (1)
9-9
: Use "reload" for data-turbolinks-track
Update to the documented standard in app/views/layouts/application.html.erb:9:- <%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %> + <%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': 'reload') %>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/views/layouts/application.html.erb
(1 hunks)
🔇 Additional comments (1)
app/views/layouts/application.html.erb (1)
9-9
: Good approach to fix FOUC.Moving the stylesheet loading to the
<head>
is the correct solution to prevent flash of unstyled content. This ensures styles are loaded and parsed before content renders, which should eliminate the FOUC issues described in the PR objectives.After fixing the pack name issue, verify the fix by visiting the staging URLs mentioned in the PR objectives and confirming no FOUC occurs.
<title>RailsReactTutorial</title> | ||
|
||
<%= append_stylesheet_pack_tag('stimulus-bundle') %> | ||
<%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %> |
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.
Missing pack name in stylesheet_pack_tag
.
The stylesheet_pack_tag
helper requires a pack name as the first positional argument. The current syntax stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true)
is missing this required argument, which will likely cause a runtime error or fail to load any stylesheets.
Apply this diff to add the pack name (assuming 'application' is the main pack):
- <%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %>
+ <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': true %>
If a different pack name should be used, please adjust accordingly. Verify this matches the pack name used in your Webpack/Shakapacker configuration.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
<%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %> | |
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': true %> |
🤖 Prompt for AI Agents
In app/views/layouts/application.html.erb around line 9, the stylesheet_pack_tag
call is missing the required pack name; update the helper to pass the main pack
(e.g., "application") as the first argument so it reads stylesheet_pack_tag
'application', media: 'all', 'data-turbolinks-track': true (or replace
"application" with the correct pack name used in your
Webpack/webpacker/shakapacker config), ensuring the pack name matches your build
configuration.
🚀 Quick Review App CommandsWelcome! Here are the commands you can use in this PR:
|
/deploy-review-app |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit fa9361a 🎮 Control Plane Console |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit aecf463 🎮 Control Plane Console |
Pull Request Review - PR #684SummaryThis PR moves the ✅ Positive Aspects1. Correct Solution to FOUCMoving stylesheets to the 2. Clear Problem DescriptionThe PR description clearly explains:
3. Performance Best Practice MaintainedJavaScript remains deferred at the bottom of the body with 4. Minimal ChangeThe change is surgical - only one line moved, reducing risk of introducing bugs.
|
/deploy-review-app |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit 9e89bf4 🎮 Control Plane Console |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit 9e89bf4 🎮 Control Plane Console |
PR Review: Fix FOUC by moving stylesheet loading to headSummaryThis PR addresses a Flash of Unstyled Content (FOUC) issue by moving the ✅ Code Quality & Best PracticesExcellent:
Recommendation: <%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %> 🐛 Potential IssuesCritical - Duplicate Layout:
Minor - Parameter Syntax:
⚡ Performance ConsiderationsPositive Impact:
Potential Trade-off:
Recommendations:
🔒 Security ConcernsNo Issues Found:
🧪 Test CoverageMissing:
Recommendations:
📋 Action ItemsBefore Merging:
Post-Merge:
🎯 Overall AssessmentVerdict: Approve with Required Changes This is a well-intentioned fix that addresses a real UX issue. The approach is correct and follows best practices. However, the PR is incomplete because it only fixes one of two layout files. Score: 8/10
Great work identifying and fixing this issue! Once you address the duplicate layout file and verify the syntax, this will be ready to merge. |
/deploy-review-app |
The stylesheet_pack_tag was loading at the bottom of the body, causing a flash of unstyled content (FOUC) as React components rendered before CSS loaded. Moving it to the head ensures styles are loaded before content renders. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit fcaf7f1 🎮 Control Plane Console |
🎉 ✨ Deploy Complete! 🚀🌐 ➡️ Open Review AppDeployment successful for PR #684, commit fcaf7f1 🎮 Control Plane Console |
Pull Request Review - PR #684OverviewThis PR moves ✅ Positive Findings1. Code Quality & Best Practices
2. Performance Considerations
3. Security
🔍 Observations & Suggestions1. Testing Considerations
2. Minor: Turbolinks Attribute Syntax <%= stylesheet_pack_tag(media: 'all', 'data-turbolinks-track': true) %> This works, but you might consider the more explicit Turbolinks helper pattern if available: <%= stylesheet_pack_tag(media: 'all', data: { turbolinks_track: 'reload' }) %> (Only if your Rails/Turbolinks version supports this pattern - the current syntax is perfectly valid) 3. Consistency Check
This ordering looks correct and consistent. 📊 Test Coverage Assessment
🎯 Overall AssessmentVerdict: ✅ LGTM (Looks Good To Me) This is a solid, well-documented fix for a common web performance issue. The changes are:
Recommendation: Approve and merge after:
Great work on identifying and fixing this issue! Review generated by Claude Code - focusing on code quality, security, performance, and best practices |
Closing this PR. The original layout structure was correct - moving FOUC needs to be addressed through alternative approaches like server-side rendering, inline critical CSS, or webpack/rspack configuration optimizations. |
✅ Review app for PR #684 was successfully deleted |
Reopening with proper fix: disabled auto_load_bundle to prevent react_component from calling append_stylesheet_pack_tag during render, allowing us to safely move stylesheet_pack_tag to the head. |
Summary
stylesheet_pack_tag
from bottom of<body>
to<head>
section in application.html.erbProblem
The stylesheet was loading at the end of the body (line 23), after React components had already started rendering. This caused a noticeable FOUC where:
Solution
Stylesheets should always be loaded in the
<head>
to ensure styles are available before any content renders. Thestylesheet_pack_tag
is now loaded in the head alongside other pack tags, while JavaScript remains deferred at the bottom for optimal performance.Test Plan
🤖 Generated with Claude Code
This change is
Summary by CodeRabbit
Bug Fixes
Refactor