Code Splitting
Code Splitting is an experimental feature in Rspeedy.
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.
Lazy-loading components
Usually, you import components with the static import declaration:
To defer loading this component’s code until it’s rendered for the first time, replace this import with:
This code relies on dynamic 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:
The split components will only start downloading when they are rendered.
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:
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 for details.
Lazy-loading standalone project
You may also lazy-load modules that being built in a standalone Rspeedy 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 project using create-rspeedy:
Then add experimental_isLazyBundle to the options of pluginReactLynx in the lynx.config.js:
Finally, change the index.tsx to export the 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.
This would provide essential APIs that the Producer needs.
Then, the Producer could be loaded using dynamic import().
Developing Producer project
It is recommended to create a separated Consumer in the Producer project.
Then, create a separated lynx.config.consumer.js:
Use npx rspeedy dev --config lynx.config.consumer.js to start developing the producer project.

