-
-
Notifications
You must be signed in to change notification settings - Fork 634
Fix FOUC in Pro spec/dummy app by inlining critical CSS #2336
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95b64f4
Fix FOUC in Pro spec/dummy app with critical CSS partial
justin808 68ddf8e
Add documentation for Tailwind/utility-first CSS FOUC prevention
justin808 ec0ca3c
Remove broken links to files that don't exist on master yet
justin808 224ec3d
Docs: Add missing border-slate-700 to FOUC prevention example
justin808 0904794
Docs: Add missing spacing utilities to FOUC prevention example
justin808 45e8d52
Add detailed FOUC reproduction and maintenance notes
justin808 c9307b9
Refactor FOUC critical CSS to use semantic selectors
justin808 62b0b60
Fix FOUC prevention test to handle CSS inlining in CI
justin808 56233ac
Assert critical_pos before ordering comparison in FOUC test
justin808 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
react_on_rails_pro/spec/dummy/app/views/layouts/_critical_styles.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <%# | ||
| Critical CSS for preventing Flash of Unstyled Content (FOUC) | ||
| ============================================================= | ||
|
|
||
| ## What is the problem? | ||
|
|
||
| The layout uses Tailwind utility classes (flex, h-screen, bg-slate-100, etc.) | ||
| in the HTML. These classes have no effect until the Tailwind CSS bundle | ||
| (client-bundle.css) loads from webpack. On slow connections, the page renders | ||
| with browser defaults first: single-column, no sidebar, no colors. Then CSS | ||
| loads and the layout jumps into place. This is FOUC. | ||
|
|
||
| ## How does this fix work? | ||
|
|
||
| By inlining critical layout CSS using stable semantic selectors (.app-shell, | ||
| .app-sidebar, .app-main), the browser can render the correct layout | ||
| immediately - before any external CSS loads. This means the sidebar, flex | ||
| layout, and background colors render correctly on the very first paint. | ||
|
|
||
| The semantic selectors are decoupled from Tailwind class names, so this | ||
| partial does not need to change when Tailwind utility classes are added | ||
| or removed. It only needs updating when the fundamental layout structure | ||
| changes (e.g., sidebar width, layout direction). | ||
|
|
||
| Once the full Tailwind CSS bundle loads, it naturally takes over (identical | ||
| property values, so no visual change). | ||
|
|
||
| ## How to reproduce the FOUC (to verify this fix is still needed) | ||
|
|
||
| 1. Comment out the `render "layouts/critical_styles"` line in application.html.erb | ||
| 2. Set up the dummy app environment: | ||
| cd react_on_rails_pro/spec/dummy | ||
| bundle install && pnpm install | ||
| RAILS_ENV=development bin/shakapacker # compile webpack assets | ||
| bin/rails db:migrate | ||
| rails s -p 3000 | ||
| 3. Open Chrome DevTools > Network > Throttle to "Slow 3G" | ||
| 4. Load http://localhost:3000/ and observe unstyled single-column layout flash | ||
| before CSS loads and the proper two-column layout appears. | ||
|
|
||
| Alternatively, use Playwright with CDP network throttling: | ||
| const client = await context.newCDPSession(page); | ||
| await client.send("Network.emulateNetworkConditions", { | ||
| offline: false, | ||
| downloadThroughput: (20 * 1024) / 8, | ||
| uploadThroughput: (20 * 1024) / 8, | ||
| latency: 500, | ||
| }); | ||
| await page.goto("http://localhost:3000/", { waitUntil: "commit" }); | ||
| await page.screenshot({ path: "fouc-check.png" }); | ||
|
|
||
| ## How to maintain this file | ||
|
|
||
| This file uses semantic selectors (.app-body, .app-shell, .app-sidebar, etc.) | ||
| that are added alongside Tailwind classes in application.html.erb. You only | ||
| need to update this file when the fundamental layout structure changes | ||
| (e.g., sidebar width, layout direction, major spacing). | ||
|
|
||
| The semantic selectors correspond to these elements in application.html.erb: | ||
| <body class="app-body ..."> | ||
| <div class="app-shell ..."> (full-screen flex row container) | ||
| <div class="app-sidebar ..."> (fixed-width sidebar) | ||
| <div class="app-sidebar-header ..."> (logo + title row) | ||
| <div class="app-main ..."> (flexible main content area) | ||
| <div class="app-main-content ..."> (padded content wrapper) | ||
| %> | ||
| <!-- Critical inline styles to prevent FOUC - see _critical_styles.html.erb for docs --> | ||
| <style data-critical-styles> | ||
| /* App shell: full-screen flex row */ | ||
| .app-body { background-color: #fff; } | ||
| .app-shell { display: flex; flex-direction: row; height: 100vh; width: 100vw; } | ||
|
|
||
| /* Sidebar: fixed-width column with background and border */ | ||
| .app-sidebar { | ||
| display: flex; | ||
| flex-direction: column; | ||
| overflow-y: auto; | ||
| padding: 1.25rem; | ||
| background-color: #f1f5f9; | ||
| border-style: solid; | ||
| border-right-width: 2px; | ||
| border-color: #334155; | ||
| min-width: 400px; | ||
| max-width: 400px; | ||
| } | ||
| .app-sidebar-header { display: flex; flex-direction: row; } | ||
|
|
||
| /* Main content: flexible, scrollable area */ | ||
| .app-main { flex: 1 1 0%; overflow-x: hidden; overflow-y: auto; } | ||
| .app-main-content { padding: 1.25rem; } | ||
| </style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nil comparison in test
If
client-bundle.cssisn’t present,stylesheet_posis nil and the testskips, butcritical_poscan also be nil (e.g., regression where the partial stops rendering). In that caseexpect(critical_pos).to be < stylesheet_poscomparesnil < Integer/nil < niland will raise before the skip logic is reached in other branches. Consider assertingcritical_posis present (like the first spec) before any ordering comparisons, and useskiponly for the stylesheet position.Also appears in:
react_on_rails_pro/spec/dummy/spec/system/integration_spec.rb:48-56.