-
-
Notifications
You must be signed in to change notification settings - Fork 638
Document content_for pattern to prevent FOUC with SSR and auto_load_bundle (#1864) #1865
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
WalkthroughDocumentation updates: rework FOUC troubleshooting for file-system auto-bundling to explain SSR Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Layout as Layout (ERB)
participant Head as Head (stylesheet_pack_tag)
participant Body as Body (render body)
participant Renderer as Server Renderer (react_component)
Note over Layout,Renderer: Original problematic flow
Layout->>Head: render head
Layout->>Body: render body
Body->>Renderer: react_component (calls append_stylesheet_pack_tag)
Renderer-->>Head: append_stylesheet_pack_tag happens after head rendered
Note over Head: Result: stylesheets missing from head -> FOUC
sequenceDiagram
autonumber
participant Layout as Layout (ERB with content_for)
participant Collector as content_for(:body_content)
participant Renderer as Server Renderer (react_component)
participant Head as Head (stylesheet_pack_tag)
participant Body as Body (yield :body_content)
Note over Layout,Renderer: Revised flow using content_for to collect body first
Layout->>Collector: capture body rendering (react_component -> append_stylesheet_pack_tag collected)
Collector-->>Layout: body content + collected appends available
Layout->>Head: render head with collected append_stylesheet_pack_tag before stylesheet_pack_tag
Layout->>Body: render body via yield :body_content
Note over Head,Body: Result: stylesheets present in head before body -> avoids FOUC
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (1)**/*.{js,jsx,ts,tsx,css,scss,json,yml,yaml,md}📄 CodeRabbit inference engine (CLAUDE.md)
Files:
🪛 LanguageTooldocs/core-concepts/auto-bundling-file-system-based-automated-bundle-generation.md[grammar] ~572-~572: There might be a mistake here. (QB_NEW_EN) [grammar] ~573-~573: There might be a mistake here. (QB_NEW_EN) ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
🔇 Additional comments (5)
Comment |
PR Review: Document content_for pattern to prevent FOUC with SSR and auto_load_bundleSummaryThis PR provides excellent documentation for a non-trivial SSR + auto_load_bundle issue. The changes are well-structured, clear, and provide actionable solutions. Strengths1. Clear Problem Definition ✅The PR does an excellent job explaining the root cause:
2. Well-Structured Solutions ✅The three-option approach is excellent:
This gives users flexibility based on their specific needs. 3. Code Examples ✅The ERB examples are:
4. Documentation Quality ✅
Minor Suggestions1. Clarify the "counter-intuitive" noteLine 561 mentions that the pattern "may seem counter-intuitive" but could be slightly clearer: **Note**: While this pattern may seem counter-intuitive (rendering body content before `<!DOCTYPE html>`), it ensures proper stylesheet loading order and prevents FOUC. Rails processes the `content_for` block first, capturing the content, but renders the actual HTML in document order. This addition clarifies that Rails still outputs proper HTML structure. 2. Consider adding a decision treeFor users wondering which option to choose, consider adding: **Which option should I use?**
- Using SSR + auto_load_bundle? → Use Option 1 (content_for)
- Need fine-grained control over pack loading? → Use Option 2 (manual)
- Seeing FOUC only in development with HMR? → See Option 3 (expected behavior) 3. Trailing newline checkPer CLAUDE.md requirements, verify the file ends with a newline character. 4. Cross-reference opportunityConsider adding a forward reference in the auto_load_bundle introduction (earlier in the doc) mentioning that SSR users should read the FOUC troubleshooting section. Code Quality Assessment✅ Documentation: Excellent Potential IssuesNone identified - but verify:
Test CoverageAs this is documentation-only, traditional test coverage doesn't apply. However:
Security Concerns✅ None - documentation only, no code changes Performance Considerations✅ None - the documented pattern doesn't add performance overhead (content_for is zero-cost abstraction) Recommendations
Overall AssessmentScore: 9.5/10 This PR significantly improves the documentation by solving a real pain point users encounter with SSR + auto_load_bundle. The explanations are clear, solutions are practical, and the organization is excellent. The minor suggestions above are truly minor - this PR is ready to merge as-is, pending the standard formatting checks. Great work documenting this tricky interaction! 🎯 |
Code Review: PR #1865Overall Assessment✅ Approved with minor suggestions This is an excellent documentation update that addresses a real pain point for users implementing SSR with auto_load_bundle. The technical explanation is accurate and the solutions are practical. Strengths1. Clear Problem Definition ✨
2. Comprehensive Solutions
3. Code Example Quality
Suggestions for Improvement1. Minor Typo/Formatting (Line 560)- **Note**: While this pattern may seem counter-intuitive (body content before `<!DOCTYPE html>`), it ensures proper stylesheet loading order and prevents FOUC.
+ **Note**: While this pattern may seem counter-intuitive (body content defined before `<!DOCTYPE html>`), it ensures proper stylesheet loading order and prevents FOUC. The word "defined" adds clarity that we're defining the content block, not literally rendering the body before DOCTYPE. 2. Enhanced Explanation (Optional)Consider adding a brief note about execution order: <% # Step 1: This block executes first, defining body_content AND triggering auto-appends %>
<% content_for :body_content do %>
<%= react_component "NavigationBarApp", prerender: true %>
<% end %>
<% # Step 2: Then the head renders with all accumulated stylesheet appends %>
<head>
<%= stylesheet_pack_tag(media: 'all') %>
</head>
<% # Step 3: Finally, the defined body_content is yielded here %>
<body>
<%= yield :body_content %>
</body> This could help readers understand the execution flow more clearly. 3. Cross-Reference Opportunity (Line 594+)The new section mentions "Additional checks" but these seem to be generic CSS troubleshooting. Consider explicitly noting: **Additional checks** (for all FOUC issues):
- **Layout**: Verify your layout includes `<%= stylesheet_pack_tag %>` placeholder
- **Component imports**: Check that CSS files are properly imported: `import styles from './Component.module.css';`
For SSR-specific FOUC with auto_load_bundle, see Options 1-2 above. Testing Verification✅ Documentation-only change - No code execution tests needed
Security & Performance✅ No concerns
Alignment with CLAUDE.md✅ Follows repository conventions
Final RecommendationLGTM - Approve and Merge 🚀 This PR significantly improves the documentation for a common pitfall. The only suggestions above are minor enhancements that could be addressed in a follow-up if desired, but they shouldn't block merging. Impact: High positive impact - will save users significant debugging time on a confusing issue. |
921af01
to
32c22aa
Compare
…undle 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 execution flow - Added step-by-step comments showing Rails execution order - 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 - Addresses code review feedback from PR #1865 comment #3412140475 Aligns with Shakapacker PR #737 preventing_fouc.md documentation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Code Review - PR #1865✅ Overall AssessmentThis is an excellent documentation improvement that addresses a real pain point for developers using SSR with 🎯 Strengths1. Clear Problem Statement
2. Comprehensive Solution Documentation
3. Good Resource Links
4. Code Quality
💡 Suggestions for Enhancement1. Technical Clarification - Line 549Consider adding a more explicit comment about why the stylesheet appends work: <%# Step 2: Head renders with all accumulated stylesheet/JS appends %>
<%# Note: Empty stylesheet_pack_tag() calls render ALL accumulated appends %>
<%= stylesheet_pack_tag(media: 'all') %>
<%= javascript_pack_tag(defer: true) %> This would help developers understand that calling 2. Potential Gotcha - Line 559-564When documenting the alternative solution, consider mentioning the trade-off: # config/initializers/react_on_rails.rb
config.auto_load_bundle = false
# Trade-off: You'll need to manually add pack tags for each component bundle
# Example: <%= javascript_pack_tag 'NavigationBarApp' %> This helps developers make an informed decision. 3. Minor: Consistency in Line 550The HTML comment syntax differs from ERB comments: <%# Step 2: Head renders with all accumulated stylesheet/JS appends %>
<%= stylesheet_pack_tag(media: 'all') %> vs. the original: <!-- All auto-appended stylesheets will be included here --> Recommendation: Keep the HTML comment style for consistency with typical Rails layouts, or convert to ERB comment style (I personally prefer ERB comments in 4. Educational EnhancementConsider adding a brief "Why this works" explanation: **How this pattern solves the problem**:
1. Rails processes the `content_for :body_content` block first
2. During this processing, `react_component` calls trigger `append_stylesheet_pack_tag`
3. These appends are queued in memory
4. When `stylesheet_pack_tag` is called in `<head>`, it renders all queued appends
5. Finally, the captured body content is yielded into the `<body>` 🔒 Security Considerations✅ No security concerns - This is documentation-only change with no code execution or data handling modifications. 🧪 Test Coverage✅ Appropriate for documentation - The PR references a working implementation in react-webpack-rails-tutorial PR #686, which serves as validation. Optional: Consider adding a spec file test in 🎨 Style & Conventions✅ Follows CLAUDE.md requirements:
⚡ Performance Considerations✅ No performance impact - Documentation-only change. The 🐛 Potential IssuesNone identified - The solution is technically sound and well-documented. 📝 Final Recommendation✅ APPROVE with minor suggestions This PR significantly improves the developer experience by documenting a critical SSR+auto-bundling pattern. The suggestions above are optional enhancements that could make good documentation even better, but they don't block merging. Action Items (all optional):
Great work addressing issue #1864! 🎉 Review conducted following repository CLAUDE.md guidelines |
Pull Request ReviewSummaryThis PR provides excellent documentation improvements for addressing FOUC issues with SSR and auto_load_bundle. The changes are clear, well-structured, and provide practical solutions. Strengths1. Clear Problem Definition
2. Comprehensive Solution Documentation
3. Quality Code Examples
4. Excellent Supporting Resources
5. Proper Scope Separation
Minor Suggestions1. Typo (Line 561) 2. Code Comment (Line 555) 3. Technical Precision (Line 527) AssessmentDocumentation Standards: Excellent OverallRating: 5/5 Excellent Recommendation: Approve with minor optional refinements The suggested improvements are cosmetic and do not block approval. The PR is ready to merge as-is. Additional Notes
Great work on improving the developer experience! |
…undle 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 execution flow - Added step-by-step comments showing Rails execution order - 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 - Addresses code review feedback from PR #1865 comment #3412140475 - Adds FOUC troubleshooting section to docs/deployment/troubleshooting.md - Includes quick fix example and link to detailed guide Aligns with Shakapacker PR #737 preventing_fouc.md documentation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
32c22aa
to
53f08fd
Compare
Code Review: PR #1865 - Document content_for pattern to prevent FOUCOverall Assessment: ✅ Excellent Documentation ImprovementThis PR provides clear, actionable documentation for a common FOUC issue when using SSR with auto_load_bundle. The changes are well-structured, technically accurate, and provide multiple solution paths. ✅ Strengths1. Clear Problem Definition
2. Comprehensive Solutions
3. Code Quality
4. Documentation Structure
💡 Suggestions for Improvement1. Minor: Link ValidationThe PR references several external resources. Consider verifying these links are accessible and anchors work correctly. 2. Minor: Example CompletenessIn troubleshooting.md:180-193, the Quick fix example is minimal. Consider adding a comment indicating it's a simplified example. 3. Terminology ConsistencyThe phrase 'body content before ' (line 561) might confuse beginners. Consider clarifying that Rails processes blocks in execution order, not file order. 🔒 Security Considerations✅ No security concerns - This is documentation-only with safe ERB examples. ⚡ Performance Considerations✅ Positive impact - The documented solution prevents FOUC, improving perceived performance and user experience. 🧪 Test Coverage✅ Documentation tested - The PR mentions testing in react-webpack-rails-tutorial, which is appropriate for documentation changes. 🎯 RecommendationAPPROVE - This is a high-quality documentation improvement that solves a real pain point. The suggestions above are minor enhancements that don't block approval. The PR successfully:
Great work! This will help many developers avoid a confusing FOUC issue. Files reviewed:
|
Summary
Documents the
content_for
pattern to prevent FOUC (Flash of Unstyled Content) when usingauto_load_bundle = true
with server-side rendering.Fixes #1864
Problem
When using
auto_load_bundle = true
with SSR, there's a chicken-and-egg problem:<head>
to prevent FOUCreact_component
calls in<body>
triggerappend_stylesheet_pack_tag
after the head has renderedSolution
This PR documents three solutions in the troubleshooting section:
Option 1: content_for pattern (Recommended)
Render body content BEFORE the head using
content_for
blocks, ensuring all auto-appends happen before the head renders:Option 2: Disable auto_load_bundle
Set
auto_load_bundle = false
and manually manage pack loading.Option 3: HMR-related FOUC
Clarifies that HMR FOUC in development is separate from the SSR issue.
Changes
docs/core-concepts/auto-bundling-file-system-based-automated-bundle-generation.md
content_for
workaround pattern with full exampleReferences
Test Plan
🤖 Generated with Claude Code
This change is
Summary by CodeRabbit