Bundle Size Optimization
· 3 min read
Execution Plan
Optimize bundle sizes and load times for both Storybook and the main blog to improve web performance:
Goals
- Reduce Storybook bundle sizes - Currently seeing warnings for large bundles (1.41 MiB for force-graph libraries)
- Improve initial load time - Reduce time to first paint and interactive
- Better code splitting - Ensure heavy libraries load only when needed
- Optimize caching - Separate chunks for better browser caching
Areas to Optimize
-
Storybook Build
- Large bundles:
70.fd609f3c.iframe.bundle.js(1.41 MiB),878.38cfd5e5.iframe.bundle.js(983 KiB) - Main entrypoint: 1.43 MiB combined
- Force-graph libraries (
react-force-graph-2d,d3-force) are heavy and should be code-split
- Large bundles:
-
Docusaurus Blog Build
- Optimize production bundle sizes
- Ensure proper tree-shaking
- Lazy load heavy components (Graph component)
-
Webpack Configuration
- Configure code splitting for better chunk separation
- Optimize chunk sizes and caching strategies
- Enable tree-shaking and dead code elimination
Execution Results / Attempts
✅ Initial Webpack Optimization (2025-01-20)
What We Tried:
-
Added webpack code splitting configuration in
.storybook/main.ts:- Configured
splitChunksto separate large libraries into their own chunks - Created separate chunks for
force-graphlibraries (react-force-graph-2d,d3-force) - Separated Storybook addons into their own chunks
- Set
usedExports: trueto enable tree-shaking
- Configured
-
Chunk Separation Strategy:
forceGraph: {
name: 'force-graph',
test: /[\\/]node_modules[\\/](react-force-graph|force-graph|d3-)[\\/]/,
chunks: 'all',
priority: 30,
enforce: true,
}
Results:
- ✅ Code splitting configuration added successfully
- ✅ Force-graph libraries are now in separate chunks
- ⚠️ Bundle size warnings still present (expected - these are large libraries)
- ⚠️ Main entrypoint still shows 1.43 MiB (includes runtime + main bundle)
What Worked:
- Webpack configuration successfully merged with Storybook's existing optimization settings
- Chunk separation is working - force-graph libraries are isolated
- Configuration is non-breaking and merges safely with Storybook defaults
What Didn't Work / Limitations:
- Bundle size warnings persist because:
react-force-graph-2dandd3-forceare inherently large libraries (~1.4 MiB)- Storybook itself bundles many development dependencies
- These are dev tools, so absolute bundle size is less critical than production
- The warnings are informational - Storybook is primarily a development tool
Current Status:
- Code splitting is configured and working
- Large libraries are properly isolated into separate chunks
- Bundle size warnings remain but are expected for heavy libraries
- Further optimization would require:
- Lazy loading stories that use heavy components
- Removing unused Storybook addons
- Considering alternative lighter libraries (if available)
Next Steps
-
Lazy Load Heavy Stories
- Only load Graph stories when they're actually viewed
- Use React.lazy() or dynamic imports for Graph component stories
-
Review Storybook Addons
- Audit which addons are actually used
- Remove unused addons (
@storybook/addon-interactions,@storybook/addon-linksif not needed)
-
Docusaurus Production Optimization
- Apply similar code splitting strategies to Docusaurus build
- Ensure Graph component is lazy-loaded in production
- Optimize CSS bundle sizes
-
Monitor and Measure
- Track bundle sizes over time
- Measure actual load times (not just bundle sizes)
- Use Lighthouse/WebPageTest for real-world performance metrics