Lazy Load and Prefetch Components
This chapter introduces how to use createLazyComponent to load remote React components on demand in a host application.
createLazyComponent is a higher-level loading API for React component scenarios. Compared with directly loading a module and wiring up React.lazy, Suspense, and error boundaries yourself, it builds those pieces into the API and additionally provides:
- Declarative loading and fallback: no need to manually wrap
React.Suspenseand error boundaries - Data prefetching: use
prefetchto start dependency requests before the component loads, avoiding request waterfalls where data is requested only after the component is ready - SSR control: precisely control whether remote components render on the server, helping avoid CSS flickering issues
Installation
Basic Usage
Step 1: Register lazyLoadComponentPlugin
Register the lazyLoadComponentPlugin plugin at runtime to enable createLazyComponent and prefetch APIs.
Step 2: Call createLazyComponent
After registering the lazyLoadComponentPlugin plugin, you can create lazy-loaded components using the instance.createLazyComponent method.
createLazyComponent API Reference
Function Signature
createLazyComponent
This API requires registering the lazyLoadComponentPlugin plugin first before it can be called.
Type declaration
In addition to loading components, this function also supports the following capabilities:
- In SSR mode, it will inject the corresponding producer's style tags/script resources, which can help avoid CSS flickering issues caused by streaming rendering and accelerate PID (First Paint Interactive Time).
- If the producer has a data fetching function, it will automatically call this function and inject the data.
loader
- Type:
() => Promise<T> - Required: Yes
- Default:
undefined
Function to load remote components, typically ()=>loadRemote(id) or ()=>import(id).
loading
- Type:
React.ReactNode - Required: Yes
- Default:
undefined
Sets the module loading state.
delayLoading
- Type:
number - Required: No
- Default:
undefined
Sets the delay time for displaying loading state in milliseconds. If the loading time is less than this time, the loading state will not be displayed.
fallback
- Type:
(({ error }: { error: ErrorInfo}) => React.ReactElement) - Required: Yes
- Default:
undefined
The error component rendered when component loading or rendering fails.
export
- Type:
string - Required: No
- Default:
'default'
If the remote component is a named export, you can specify the component name to export through this parameter. By default, it loads the default export.
dataFetchParams
- Type:
DataFetchParams - Required: No
- Default:
undefined
If the remote component has a data fetching function, this will be passed to the data fetching function when set.
noSSR
- Type:
boolean - Required: No
- Default:
false
When set to true, this component will not render in SSR scenarios.
injectScript
- Type:
boolean - Required: No
- Default:
false
In SSR environment, if set to true, the created component will inject the corresponding script resources.
For example, if remote/button has __federation_button.js, then in the HTML returned by SSR, the corresponding script will be injected before the component to accelerate interaction speed.
injectLink
- Type:
boolean - Required: No
- Default:
true
In SSR environment, if set to true, the created component will inject the corresponding style resource links.
For example, if remote/button has __federation_button.css, then in the HTML returned by SSR, the corresponding link will be injected before the component to avoid page flickering issues.
prefetch
This API requires registering the lazyLoadComponentPlugin plugin first before it can be called.
Type declaration
Preload component resource files and the component's data loader.
id
- Type:
string - Required: Yes
- Default:
undefined
The id of the component to preload.
preloadComponentResource
- Type:
boolean - Required: No
- Default:
false
Whether to preload the component's resource files.
dataFetchParams
- Type:
DataFetchParams - Required: No
- Default:
undefined
If the remote component has a data fetching function, this will be passed to the data fetching function when set.