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
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]>
Copy file name to clipboardExpand all lines: docs/guides/file-system-based-automated-bundle-generation.md
+360Lines changed: 360 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,6 +219,259 @@ 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"
Now when you visit your pages, React on Rails automatically:
465
+
- Loads only the CSS and JS needed for components on each page
466
+
- Registers components without manual `ReactOnRails.register()` calls
467
+
- Enables optimal bundle splitting and caching
468
+
469
+
**Bundle sizes in this example:**
470
+
- HelloWorld: ~50KB (lightweight)
471
+
- HeavyMarkdownEditor: ~2.7MB (heavy with 58+ dependencies)
472
+
473
+
Each page loads only what it needs!
474
+
222
475
### Server Rendering and Client Rendering Components
223
476
224
477
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 +486,110 @@ Once generated, all server entrypoints will be imported into a file named `[Reac
233
486
### Using Automated Bundle Generation Feature with already defined packs
234
487
235
488
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.
489
+
490
+
## Troubleshooting
491
+
492
+
### Common Issues and Solutions
493
+
494
+
#### 1. "Component not found" errors
495
+
496
+
**Problem**: `react_component` helper throws "Component not found" error.
497
+
498
+
**Solutions**:
499
+
- Ensure your component is in a `ror_components` directory (or your configured `components_subdirectory`)
500
+
- Run `rake react_on_rails:generate_packs` to generate the component bundles
501
+
- Check that your component exports a default export: `export default MyComponent;`
502
+
- Verify the component name matches the directory structure
503
+
504
+
#### 2. CSS not loading (FOUC - Flash of Unstyled Content)
505
+
506
+
**Problem**: Components load but CSS styles are missing or delayed.
507
+
508
+
**Solutions**:
509
+
- **Development with HMR**: FOUC is expected. Use `bin/dev static` mode for development without FOUC
510
+
- **Production**: Ensure CSS extraction is working by checking for `.css` files in `public/packs/css/generated/`
511
+
- **Layout**: Verify your layout includes empty `<%= stylesheet_pack_tag %>` placeholder
512
+
- Check that CSS files are properly imported in your components: `import styles from './Component.module.css';`
513
+
514
+
#### 3. "document is not defined" errors during SSR
515
+
516
+
**Problem**: Server-side rendering fails with browser-only API access.
0 commit comments