Server Bundles
Server bundles allow you to split your server-side code into multiple output files, enabling different routes to be deployed to different servers or serverless functions.Overview
By default, React Router builds a single server bundle containing all routes. Server bundles let you:- Deploy different routes to different servers
- Split large applications across multiple serverless functions
- Deploy to different regions or platforms
- Optimize cold start times for serverless deployments
- Reduce bundle sizes for individual functions
Configuration
Define server bundles inreact-router.config.ts:
Server Bundle Function
TheserverBundles function is called for each route:
branch
An array of routes from root to the current route:
Examples
Split by Route Prefix
Split by Path Pattern
Split by File Location
Regional Deployment
Build Output
With server bundles enabled, the build output structure changes:Without Server Bundles
With Server Bundles
Build Manifest
The build manifest includes server bundle information:buildEnd hook:
Deployment
Deploy to Different Servers
Serverless Functions
Deploy each bundle as a separate function:Running Multiple Bundles Locally
For development or preview, you may need to run multiple bundles:Vite Environment API
With the Vite Environment API enabled, server bundles create separate environments:ssrBundle_<bundleId>
Shared Code
Code shared between bundles is automatically deduplicated by Vite:Bundle Size Optimization
Minimize Shared Dependencies
Keep bundles small by minimizing shared dependencies:Split Heavy Routes
Caveats
- Client bundle is shared - Only server code is split, client code is always in one bundle
- Route discovery still works - All routes are discoverable regardless of bundle
- Shared code is duplicated - Dependencies are included in each bundle that uses them
- Cold starts may vary - Different bundles have different cold start times
- Deployment complexity - Multiple bundles require coordinated deployment
Best Practices
- Group related routes - Keep related functionality in the same bundle
- Consider bundle size - Balance number of bundles vs. size of each
- Test locally - Ensure all bundles work together correctly
- Monitor performance - Track bundle sizes and cold start times
- Document bundle strategy - Make it clear which routes go in which bundles