You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document content_for pattern to prevent FOUC with SSR and auto_load_bundle
Fixes#1864
When using auto_load_bundle = true with server-side rendering, there's a
chicken-and-egg problem: stylesheets must load in the <head>, but
react_component calls in <body> trigger auto-appends after the head has
rendered, causing FOUC (Flash of Unstyled Content).
This commit documents the content_for workaround pattern and references
the comprehensive Shakapacker FOUC prevention guide (PR #737).
Key improvements:
- Explains the root cause of FOUC with SSR + auto_load_bundle
- Documents the content_for :body_content pattern with full example
- References Shakapacker's comprehensive FOUC prevention documentation
- Links to working example in react-webpack-rails-tutorial PR #686
- Provides alternative solution (disable auto_load_bundle)
- Clarifies that HMR FOUC is separate from this SSR issue
Aligns with Shakapacker PR #737 preventing_fouc.md documentation.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Copy file name to clipboardExpand all lines: docs/core-concepts/auto-bundling-file-system-based-automated-bundle-generation.md
+43-12Lines changed: 43 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -522,23 +522,54 @@ As of version 13.3.4, bundles inside directories that match `config.components_s
522
522
523
523
#### 2. CSS not loading (FOUC - Flash of Unstyled Content)
524
524
525
-
**Problem**: Components load but CSS styles are missing or delayed.
525
+
**Problem**: Components load but CSS styles are missing or delayed, particularly with server-side rendering and `auto_load_bundle = true`.
526
526
527
-
**Important**: FOUC (Flash of Unstyled Content) **only occurs with HMR (Hot Module Replacement)**. Static and production modes work perfectly without FOUC.
527
+
**Root Cause**: When using `auto_load_bundle = true`, `react_component` calls automatically invoke `append_stylesheet_pack_tag` during rendering. However, Shakapacker requires these appends to execute BEFORE the main `stylesheet_pack_tag` in your layout's `<head>`. Since Rails renders the layout's `<head>` before the `<body>` (where `react_component` calls typically occur), the appends happen too late, causing FOUC.
528
528
529
-
**Solutions**:
529
+
**Solution**: Use the `content_for :body_content` pattern documented in Shakapacker's [Preventing FOUC guide](https://github.com/shakacode/shakapacker/blob/master/docs/preventing_fouc.md#the-content_for-body_content-pattern).
530
+
531
+
This pattern renders your body content first, ensuring all `react_component` auto-appends execute before the `<head>` renders:
0 commit comments