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
* docs: Improve file-system-based bundle generation guide
Add comprehensive improvements to the file-system-based automated bundle generation documentation to address gaps that cause confusion for both human developers and AI coding agents.
## Key improvements:
### 1. Layout Integration Clarification
- Added detailed section explaining empty pack tag placeholders in Rails layouts
- Clarified how react_component helper automatically calls append_javascript_pack_tag
- Explained the connection between component usage and automatic bundle injection
- Showed the complete flow from component rendering to CSS/JS loading
### 2. Complete Working Example
- Added step-by-step setup guide from scratch
- Included real component implementations with CSS modules and dynamic imports
- Provided complete Rails controller, view, and layout code examples
- Demonstrated proper directory structure with concrete file paths
- Showed bundle splitting benefits with actual size comparisons (50KB vs 2.7MB)
### 3. Comprehensive Troubleshooting Section
- Documented all common issues encountered during implementation
- Added solutions for FOUC handling across different development modes
- Covered SSR compatibility issues with dynamic imports
- Included installation order requirements (Shakapacker before React on Rails)
- Provided debug techniques for troubleshooting bundle loading
- Addressed bundle size optimization and development vs production differences
### 4. Enhanced Developer Experience
- Clear explanations of "why" behind configuration choices
- Explicit file naming conventions and patterns
- Debug mode instructions for investigating bundle loading issues
- Common failure modes and their solutions
These improvements make the documentation significantly more practical and reduce the likelihood of implementation errors. The additions are based on real-world implementation experience and address the most common stumbling blocks.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
* Improve docs
---------
Co-authored-by: Claude <[email protected]>
Copy file name to clipboardExpand all lines: docs/guides/file-system-based-automated-bundle-generation.md
+385Lines changed: 385 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,6 +219,276 @@ For example, if you wanted to utilize our file-system based entrypoint generatio
219
219
220
220
The default value of the `auto_load_bundle` parameter can be specified by setting `config.auto_load_bundle` in `config/initializers/react_on_rails.rb` and thus removed from each call to `react_component`.
221
221
222
+
### Layout Integration with Auto-Loading
223
+
224
+
When using `auto_load_bundle: true`, your Rails layout needs to include empty pack tag placeholders where React on Rails will inject the component-specific CSS and JavaScript bundles automatically:
225
+
226
+
```erb
227
+
<!DOCTYPE html>
228
+
<html>
229
+
<head>
230
+
<!-- Your regular head content -->
231
+
<%= csrf_meta_tags %>
232
+
<%= csp_meta_tag %>
233
+
234
+
<!-- Empty pack tags - React on Rails injects component CSS/JS here -->
235
+
<%= stylesheet_pack_tag %>
236
+
<%= javascript_pack_tag %>
237
+
</head>
238
+
<body>
239
+
<%= yield %>
240
+
</body>
241
+
</html>
242
+
```
243
+
244
+
**How it works:**
245
+
246
+
1.**Component calls automatically append bundles**: When you use `<%= react_component("ComponentName", props, auto_load_bundle: true) %>` in a view, React on Rails automatically calls `append_javascript_pack_tag "generated/ComponentName"` and `append_stylesheet_pack_tag "generated/ComponentName"` (in static/production modes).
247
+
248
+
2.**Layout renders appended bundles**: The empty `<%= stylesheet_pack_tag %>` and `<%= javascript_pack_tag %>` calls in your layout are where the appended component bundles get rendered.
249
+
250
+
3.**No manual bundle management**: You don't need to manually specify which bundles to load - React on Rails handles this automatically based on which components are used in each view.
initialContent: "# Welcome to the Heavy Editor\n\nThis component demonstrates:\n- Dynamic imports for SSR\n- Bundle splitting\n- Automatic CSS loading"
- **HeavyMarkdownEditor**: 2.2MB total resources (2.7MB with markdown libraries)
475
+
- HeavyMarkdownEditor.js: 26.5 kB
476
+
- HeavyMarkdownEditor.css: 5.5 kB
477
+
- Markdown libraries: 1,081 kB additional
478
+
- Shared runtime: ~1.1MB (React, webpack runtime)
479
+
480
+
**Bundle splitting benefit**: Each page loads only its required components - the HelloWorld page doesn't load the heavy markdown libraries, saving ~1.1MB (50% reduction)!
*Screenshots show browser dev tools network analysis demonstrating the dramatic difference in bundle sizes and load times between the two components.*
491
+
222
492
### Server Rendering and Client Rendering Components
223
493
224
494
If server rendering is enabled, the component will be registered for usage both in server and client rendering. To have separate definitions for client and server rendering, name the component files `ComponentName.server.jsx` and `ComponentName.client.jsx`. The `ComponentName.server.jsx` file will be used for server rendering and the `ComponentName.client.jsx` file for client rendering. If you don't want the component rendered on the server, you should only have the `ComponentName.client.jsx` file.
@@ -233,3 +503,118 @@ Once generated, all server entrypoints will be imported into a file named `[Reac
233
503
### Using Automated Bundle Generation Feature with already defined packs
234
504
235
505
As of version 13.3.4, bundles inside directories that match `config.components_subdirectory` will be automatically added as entrypoints, while bundles outside those directories need to be manually added to the `Shakapacker.config.source_entry_path` or Webpack's `entry` rules.
506
+
507
+
## Troubleshooting
508
+
509
+
### Common Issues and Solutions
510
+
511
+
#### 1. "Component not found" errors
512
+
513
+
**Problem**: `react_component` helper throws "Component not found" error.
514
+
515
+
**Solutions**:
516
+
- Ensure your component is in a `ror_components` directory (or your configured `components_subdirectory`)
517
+
- Run `rake react_on_rails:generate_packs` to generate the component bundles
518
+
- Check that your component exports a default export: `export default MyComponent;`
519
+
- Verify the component name matches the directory structure
520
+
521
+
#### 2. CSS not loading (FOUC - Flash of Unstyled Content)
522
+
523
+
**Problem**: Components load but CSS styles are missing or delayed.
524
+
525
+
**Important**: FOUC (Flash of Unstyled Content) **only occurs with HMR (Hot Module Replacement)**. Static and production modes work perfectly without FOUC.
526
+
527
+
**Solutions**:
528
+
- **Development with HMR** (`./bin/dev`): FOUC is expected behavior due to dynamic CSS injection - **not a bug**
529
+
- **Development static** (`./bin/dev static`): No FOUC - CSS is extracted to separate files like production
530
+
- **Production** (`./bin/dev prod`): No FOUC - CSS is extracted and optimized
531
+
- **Layout**: Verify your layout includes empty `<%= stylesheet_pack_tag %>` placeholder for CSS injection
532
+
- **Component imports**: Check that CSS files are properly imported: `import styles from './Component.module.css';`
533
+
534
+
**Key insight**: Choose your development mode based on your current needs:
535
+
- Use HMR for fastest development (accept FOUC)
536
+
- Use static mode when testing styling without FOUC
537
+
- Use production mode for final testing
538
+
539
+
#### 3. "document is not defined" errors during SSR
540
+
541
+
**Problem**: Server-side rendering fails with browser-only API access.
0 commit comments