Skip to main content

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 in react-router.config.ts:

Server Bundle Function

The serverBundles 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:
Access in 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:
Environments are named: ssrBundle_<bundleId>

Shared Code

Code shared between bundles is automatically deduplicated by Vite:
If used by routes in different bundles, it’s included in each bundle but the code is identical.

Bundle Size Optimization

Minimize Shared Dependencies

Keep bundles small by minimizing shared dependencies:

Split Heavy Routes

Caveats

  1. Client bundle is shared - Only server code is split, client code is always in one bundle
  2. Route discovery still works - All routes are discoverable regardless of bundle
  3. Shared code is duplicated - Dependencies are included in each bundle that uses them
  4. Cold starts may vary - Different bundles have different cold start times
  5. Deployment complexity - Multiple bundles require coordinated deployment

Best Practices

  1. Group related routes - Keep related functionality in the same bundle
  2. Consider bundle size - Balance number of bundles vs. size of each
  3. Test locally - Ensure all bundles work together correctly
  4. Monitor performance - Track bundle sizes and cold start times
  5. Document bundle strategy - Make it clear which routes go in which bundles

See Also