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

# Code Splitting

> Rspack supports code splitting, which allows splitting the code into other chunks. You have the full control about size and number of generated assets, which allow you to gain performance improvements in loading time.
>
> [Rspack - Code Splitting](https://rspack.rs/guide/optimization/code-splitting)

## Lazy-loading components

Usually, you import components with the static import declaration:

<!-- eslint-disable import/no-unresolved -->

```jsx
import LazyComponent from './LazyComponent.jsx';

export function App() {
  return (
    <view>
      <LazyComponent />
    </view>
  );
}
```

To defer loading this component’s code until it’s rendered for the first time, replace this import with:

<!-- eslint-disable import/no-unresolved -->

```diff
- import LazyComponent from './LazyComponent.jsx'
+ import { lazy } from '@lynx-js/react'
+ const LazyComponent = lazy(() => import('./LazyComponent.jsx'))
```

This code relies on [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import), which is supported by Rspack. Using this pattern requires that the lazy component you are importing was exported as the default export.

Now that your component’s code loads on demand, you also need to specify what should be displayed while it is loading. You can do this by wrapping the lazy component or any of its parents into a `<Suspense>` boundary:

:::info
The split components will only start downloading when they are rendered.
:::

<!-- eslint-disable import/no-unresolved -->

```jsx title="src/App.tsx"
import { Suspense, lazy } from '@lynx-js/react';

const LazyComponent = lazy(() => import('./LazyComponent.jsx'));

export function App() {
  return (
    <view>
      <Suspense fallback={<text>Loading...</text>}>
        <LazyComponent />
      </Suspense>
    </view>
  );
}
```

### Load lazy component when needed

In this example, the code for `LazyComponent` won’t be loaded until you attempt to render it. If `LazyComponent` hasn’t loaded yet, a "Loading..." will be shown in its place. For example:

<!-- eslint-disable import/no-unresolved -->

```jsx title="src/App.tsx"
import { Suspense, lazy, useState } from '@lynx-js/react';

const LazyComponent = lazy(() => import('./LazyComponent.jsx'));

export function App() {
  const [shouldDisplay, setShouldDisplay] = useState(false);
  const handleClick = () => {
    setShouldDisplay(true);
  };
  return (
    <view>
      <view bindtap={handleClick}>Load Component</view>
      {shouldDisplay && (
        <Suspense fallback={<text>Loading...</text>}>
          <LazyComponent />
        </Suspense>
      )}
    </view>
  );
}
```

:::warning Differences from the Web
In Lynx, CSS is scoped to each [Bundle](/next/api/lynx-native-api/template-bundle.md). Since lazy-loaded components generate their own Bundles (see output structure at [Output Files](/next/guide/build/index.md#output-files)), you shouldn’t expect them to behave like on the Web. Global CSS defined in the main Bundle will not affect lazy-loaded components, and vice versa.
:::

### Error handling

#### Use ErrorBoundary

If loading is completed, lazy-loaded components are essentially also a React component, so the error handling practices in React are still applicable.

Checkout [React - Catching rendering errors with an error boundary](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) for details.

## Lazy-loading standalone project

You may also lazy-load modules that are built in a standalone Rsbuild project.

### Glossary of Terms

- Producer (Remote): An application that exposes modules to be consumed by other Lynx applications.
- Consumer (Host): An application that consumes modules from other Producers.

### Create a standalone Producer project

Create a standalone Rsbuild project using [`@lynx-js/create-lynx`](https://www.npmjs.com/package/@lynx-js/create-lynx):

```bash
pnpm create @lynx-js/lynx@latest --template rsbuild
```

Then add [`experimental_isLazyBundle`] to the options of `pluginReactLynx` in the `rsbuild.config.ts`:

```ts title="rsbuild.config.ts"
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
  source: {
    entry: {
      main: './src/index.tsx',
    },
  },
  plugins: [
    pluginReactLynx({
      experimental_isLazyBundle: true,
    }),
  ],
});
```

Finally, change the `index.tsx` to export the `App`.

<!-- eslint-disable-next-line import/no-unresolved -->

```js title="src/index.tsx"
import { App } from './App.jsx';

export default App;
```

### Modify the Consumer project

To load the Producer project, add an import to `@lynx-js/react/experimental/lazy/import` at the beginning of the entry.

<!-- eslint-disable import/no-unresolved -->

```jsx title="src/index.tsx"
import '@lynx-js/react/experimental/lazy/import';
import { root } from '@lynx-js/react';

import { App } from './App.jsx';

root.render(<App />);
```

This would provide essential APIs that the Producer needs.

Then, the Producer could be loaded using [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import).

<!-- eslint-disable import/no-unresolved -->

```jsx title="src/App.tsx"
import { Suspense, lazy } from '@lynx-js/react';

const LazyComponent = lazy(
  () =>
    import('https://<host>:<port>/path/to/lynx.bundle', {
      with: { type: 'component' },
    }),
);

export function App() {
  return (
    <view>
      <Suspense fallback={<text>Loading...</text>}>
        <LazyComponent />
      </Suspense>
    </view>
  );
}
```

:::details Before deploying: version compatibility

Before deploying a newly built standalone lazy bundle, upgrade the Consumer to the same or a newer `@lynx-js/react` version. In particular, a Consumer earlier than 0.115.0 cannot load a lazy bundle built with 0.115.0 or later, while a 0.115.0 or later Consumer can load bundles built with older versions. See the [`@lynx-js/react` changelog](https://github.com/lynx-family/lynx-stack/blob/main/packages/react/CHANGELOG.md#01150).

:::

### Developing Producer project

It is recommended to create a separated Consumer in the Producer project.

<!-- eslint-disable import/no-unresolved -->

```jsx title="src/Consumer.tsx"
import { Suspense, lazy, root } from '@lynx-js/react';

// You may use static import if you want
const App = lazy(() => import('./App.jsx'));

root.render(
  <Suspense>
    <App />
  </Suspense>,
);
```

Then, create a separated `rsbuild.config.consumer.ts`:

```ts title="rsbuild.config.consumer.ts"
import { pluginReactLynx } from '@lynx-js/react-rsbuild-plugin';
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
  source: {
    entry: {
      main: './src/Consumer.tsx',
    },
  },
  plugins: [pluginReactLynx()],
});
```

Use `npx rsbuild dev --config rsbuild.config.consumer.ts` to start developing the producer project.

## Lazy bundle loaders

Lazy bundles are fetched through one of two loaders. Which one a build uses is decided at build time and exposed to your code as the [`__LAZY_BUNDLE_FETCHER__`](/next/api/react/Documents/Built-in_macros.md) macro.

| Loader           | How bundles are fetched                                                  |
| ---------------- | ------------------------------------------------------------------------ |
| `FetchBundle`    | `lynx.fetchBundle`, and the `import(..., { with: { mode } })` mode hints |
| `QueryComponent` | The legacy `lynx.QueryComponent` path                                    |

By default the loader follows [`engineVersion`](/next/api/packages/react-rsbuild-plugin.md#engineversion): `FetchBundle` when it is `3.9` or higher, `QueryComponent` otherwise. `engineVersion` defaults to `3.2`, so a build that does not raise it to `3.9` gets `QueryComponent`.

The `REACT_LAZY_BUNDLE_FETCHER` environment variable overrides that choice:

```bash
REACT_LAZY_BUNDLE_FETCHER=QueryComponent rspeedy build
```

- `QueryComponent` forces the legacy loader even when `engineVersion` is `3.9` or higher.
- `FetchBundle` is only accepted when `engineVersion` is `3.9` or higher. On a lower declared version the build fails, because older hosts do not expose `lynx.fetchBundle` / `lynx.loadScript`.
- Any other value is ignored, and the loader falls back to the `engineVersion`-derived choice.

:::info Version scope
The `FetchBundle` loader and this selection logic arrived in `@lynx-js/react@0.123.0` / `@lynx-js/react-rsbuild-plugin@0.18.0`, which release together. Earlier versions always use `QueryComponent`.
:::

[`experimental_isLazyBundle`]: /next/api/packages/react-rsbuild-plugin.md#experimental-islazybundle
