| 1 | ---
|
| 2 | title: Server Bundles
|
| 3 | ---
|
| 4 |
|
| 5 | # Server Bundles
|
| 6 |
|
| 7 | [MODES: framework]
|
| 8 |
|
| 9 | <br/>
|
| 10 | <br/>
|
| 11 |
|
| 12 | <docs-warning>This is an advanced feature designed for hosting provider integrations. When compiling your app into multiple server bundles, there will need to be a custom routing layer in front of your app directing requests to the correct bundle.</docs-warning>
|
| 13 |
|
| 14 | React Router typically builds your server code into a single bundle that exports a request handler function. However, there are scenarios where you might want to split your route tree into multiple server bundles, each exposing a request handler function for a subset of routes. To provide this flexibility, [`react-router.config.ts`][react-router-config] supports a `serverBundles` option, which is a function for assigning routes to different server bundles.
|
| 15 |
|
| 16 | The [`serverBundles` function][server-bundles-function] is called for each route in the tree (except for routes that aren't addressable, e.g., pathless layout routes) and returns a server bundle ID that you'd like to assign that route to. These bundle IDs will be used as directory names in your server build directory.
|
| 17 |
|
| 18 | For each route, this function receives an array of routes leading to and including that route, referred to as the route `branch`. This allows you to create server bundles for different portions of the route tree. For example, you could use this to create a separate server bundle containing all routes within a particular layout route:
|
| 19 |
|
| 20 | ```ts filename=react-router.config.ts lines=[5-13]
|
| 21 | import type { Config } from "@react-router/dev/config";
|
| 22 |
|
| 23 | export default {
|
| 24 | // ...
|
| 25 | serverBundles: ({ branch }) => {
|
| 26 | const isAuthenticatedRoute = branch.some((route) =>
|
| 27 | route.id.split("/").includes("_authenticated"),
|
| 28 | );
|
| 29 |
|
| 30 | return isAuthenticatedRoute
|
| 31 | ? "authenticated"
|
| 32 | : "unauthenticated";
|
| 33 | },
|
| 34 | } satisfies Config;
|
| 35 | ```
|
| 36 |
|
| 37 | Each `route` in the `branch` array contains the following properties:
|
| 38 |
|
| 39 | - `id` — The unique ID for this route, named like its `file` but relative to the app directory and without the extension, e.g., `app/routes/gists.$username.tsx` will have an `id` of `routes/gists.$username`
|
| 40 | - `path` — The path this route uses to match the URL pathname
|
| 41 | - `file` — The absolute path to the entry point for this route
|
| 42 | - `index` — Whether this route is an index route
|
| 43 |
|
| 44 | ## Build manifest
|
| 45 |
|
| 46 | When the build is complete, React Router will call the `buildEnd` hook, passing a `buildManifest` object. This is useful if you need to inspect the build manifest to determine how to route requests to the correct server bundle.
|
| 47 |
|
| 48 | ```ts filename=react-router.config.ts lines=[5-7]
|
| 49 | import type { Config } from "@react-router/dev/config";
|
| 50 |
|
| 51 | export default {
|
| 52 | // ...
|
| 53 | buildEnd: async ({ buildManifest }) => {
|
| 54 | // ...
|
| 55 | },
|
| 56 | } satisfies Config;
|
| 57 | ```
|
| 58 |
|
| 59 | When using server bundles, the build manifest contains the following properties:
|
| 60 |
|
| 61 | - `serverBundles` — An object that maps bundle IDs to the bundle's `id` and `file`
|
| 62 | - `routeIdToServerBundleId` — An object that maps route IDs to their server bundle ID
|
| 63 | - `routes` — A route manifest that maps route IDs to route metadata. This can be used to drive a custom routing layer in front of your React Router request handlers
|
| 64 |
|
| 65 | [react-router-config]: https://api.reactrouter.com/v7/types/_react-router_dev.config.Config.html
|
| 66 | [server-bundles-function]: https://api.reactrouter.com/v7/types/_react-router_dev.config.ServerBundlesFunction.html
|