> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# performance.parallelBuild

- **Type:** `boolean`
- **Default:** `false`
- **Status:** experimental

Whether to compile the `server` build environment in a worker process during production builds. It applies to every application whose build has a `server` environment, which means SSR (`server.ssr`, `server.ssrByEntries`) and SSG (`output.ssg`, `output.ssgByEntries`).

By default the `client` and `server` environments are compiled by two Rspack compilers inside one Node.js process. They share one main thread and one V8 heap, so JavaScript loaders, plugin hooks and garbage collection of one compiler stall the other. With this option the main process compiles the client and a worker process compiles the server at the same time. Planning (entries, routes, generated code) and everything after the compile (`server/` and `api/` compilation, SSG, deploy) still run once, in the main process, and the output is identical to a regular build.

```ts title="modern.config.ts"
export default defineConfig({
  performance: {
    parallelBuild: true,
  },
});
```

## When to enable it

The gain comes from JavaScript work on the main thread, then from the number of modules. Measured on generated applications that have a `server` environment (SSR is used as the example) (module count per compiler, median of several runs, 64-core machine):

| Application                              | Modules | Regular build | Parallel build | Time | Peak memory |
| ---------------------------------------- | ------- | ------------- | -------------- | ---- | ----------- |
| Default toolchain                        | 2.6k    | 10.3 s        | 9.8 s          | -5%  | -9%         |
| Default toolchain                        | 5k      | 17.9 s        | 16.8 s         | -6%  | -12%        |
| Default toolchain                        | 10k     | 31.6 s        | 29.4 s         | -7%  | -14%        |
| Default toolchain                        | 20k     | 62.4 s        | 57.5 s         | -8%  | -18%        |
| Default toolchain                        | 40k     | 144.5 s       | 137.9 s        | -5%  | -6%         |
| Default toolchain                        | 60k     | 255.0 s       | 244.4 s        | -4%  | 0%          |
| With a JavaScript loader on every module | 5k      | 33.1 s        | 25.0 s         | -24% | -10%        |
| With a JavaScript loader on every module | 10k     | 61.0 s        | 44.9 s         | -26% | -15%        |
| With a JavaScript loader on every module | 20k     | 120.5 s       | 85.6 s         | -29% | -7%         |
| With a JavaScript loader on every module | 40k     | 262.3 s       | 192.9 s        | -26% | -16%        |

- Enable it when a production build takes longer than about 30 seconds and the project uses JavaScript loaders or Rspack plugins written in JavaScript (Babel, custom instrumentation, code generation): the build gets about a quarter faster.
- With the default toolchain only, the time gain stays in single digits (4% to 8%). Peak memory drops by 9% to 18% up to 20k modules; the drop narrows beyond that and is gone at 60k modules.
- For small applications that build in a few seconds the difference is within noise; leave it off.

## What runs in which process

| Step                                                                                                                                 | Process                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
| Loading the configuration, entries, routes, generated entry code                                                                     | Main, once. The worker loads the configuration again but takes entries and routes from the main process |
| Rspack compile of the `client` environment (browser bundles, HTML, static assets)                                                    | Main                                                                                                    |
| Rspack compile of the `server` environment (the Node.js bundles under `dist/bundles`: server render bundles and server data loaders) | Worker                                                                                                  |
| Compilation of the `server/` directory (custom web server) and the `api/` directory (BFF)                                            | Main, once                                                                                              |
| SSG prerendering, `route.json`, deploy output                                                                                        | Main, once                                                                                              |

## Scope

| Application                                           | Behaviour with the option on                                                        |
| ----------------------------------------------------- | ----------------------------------------------------------------------------------- |
| SSR, including partial SSR with `server.ssrByEntries` | `server` environment compiled in the worker                                         |
| SSG                                                   | `server` environment compiled in the worker; prerendering stays in the main process |
| CSR only, with or without BFF or a custom web server  | No `server` environment; single process                                             |
| RSC (`server.rsc`)                                    | Not supported; single process                                                       |
| Worker SSR (`deploy.worker.ssr`)                      | A third environment is involved; single process                                     |

The option only affects `build` and `deploy`. `dev`, `--watch`, `--analyze` and builds with Rsdoctor enabled use the single process.

## Notes

The worker process loads the configuration and sets up plugins again, because functions in the build configuration cannot be sent to another process. Custom plugins in the project, and any logic written in lifecycle hooks, run as follows:

| Hook                                                                                                                                                                                                                                                             | Main process | Worker process                                       |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------------------------------------------------- |
| `config`, `modifyResolvedConfig`, `onPrepare`, the hooks that modify the build configuration (`modifyBundlerChain`, `modifyRsbuildConfig`, `modifyRspackConfig`), `onBeforeCreateCompiler`, `onAfterCreateCompiler`, and the Rsbuild plugins in `builderPlugins` | Runs         | Runs                                                 |
| `modifyServerRoutes` and the other hooks that compute entries and routes                                                                                                                                                                                         | Runs         | Does not run; the result of the main process is used |
| `onBeforeBuild`, `onAfterBuild` and the deploy hooks                                                                                                                                                                                                             | Runs         | Does not run                                         |

Logic that only modifies the configuration is not affected. Errors and warnings of the `server` compile are printed by the worker with a `[server]` prefix, and a failure there fails the build.

### Checking that a project can enable it

1. Build once with the option disabled and keep the `dist` directory.
2. Build once with the option enabled and compare the two `dist` directories, in particular the `server` output under `dist/bundles`. The contents should be the same.
3. Read the log of the build with the option enabled: check whether the project's own reporting, file writes or summaries appear twice, once with the `[server]` prefix.

If the output is the same and nothing runs twice, the option can be enabled.

### Common problems and fixes

| Problem                                                                                                                                                                                                                                            | Symptom                                                                                                                    | Fix                                                                                                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| A hook that runs in both processes does something that should happen once, such as reporting, writing a file or sending a request                                                                                                                  | The logic runs twice, without an error                                                                                     | Move it to `onBeforeBuild` or `onAfterBuild`                                                                                                                              |
| `onBeforeBuild` reads the `bundlerConfigs` of the `server` environment, or `onAfterBuild` reads its `stats`                                                                                                                                        | Both arguments cover the `client` environment only; the `server` part is `undefined`                                       | Read the resolved app config through `api.getNormalizedConfig()` instead, or move the logic to a hook that modifies the build configuration, such as `modifyRspackConfig` |
| A variable in a plugin instance or closure passes data between the `client` and `server` compiles                                                                                                                                                  | Each process has its own state, so the `server` compile cannot read a value written in the main process; there is no error | Let each compile derive the data from the configuration or from a file; keep the option disabled if that is not possible                                                  |
| A part of the configuration that both compiles must agree on (`source.define`, `source.globalVars`, output paths, SSR settings, entries) evaluates differently in the two processes, for example when it depends on `Date.now()` or a random value | The build fails with `The configuration evaluated differently in the build worker` and lists the fields that differ        | Make the value deterministic, for example by reading it from an environment variable                                                                                      |
