Skip to content

Commit 63c91bd

Browse files
committed
feat: add cacheData option
1 parent 38f664c commit 63c91bd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/bridge/bridge-react/src/lazy/createLazyComponent.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type CreateLazyComponentOptions<T, E extends keyof T> = {
3737
export?: E;
3838
dataFetchParams?: DataFetchParams;
3939
noSSR?: boolean;
40+
cacheData?: boolean;
4041
};
4142

4243
type ReactKey = { key?: React.Key | null };
@@ -192,10 +193,12 @@ function getServerNeedRemoteInfo(
192193
export function createLazyComponent<T, E extends keyof T>(
193194
options: CreateLazyComponentOptions<T, E>,
194195
) {
195-
const { instance } = options;
196+
const { instance, cacheData } = options;
196197
if (!instance) {
197198
throw new Error('instance is required for createLazyComponent!');
198199
}
200+
let dataCache: unknown = null;
201+
199202
type ComponentType = T[E] extends (...args: any) => any
200203
? Parameters<T[E]>[0] extends undefined
201204
? ReactKey
@@ -257,6 +260,7 @@ export function createLazyComponent<T, E extends keyof T>(
257260
);
258261
setDataFetchItemLoadedStatus(dataFetchMapKey);
259262
logger.debug('get data res: \n', data);
263+
dataCache = data;
260264
return data;
261265
} catch (err) {
262266
const errMsg = `${DATA_FETCH_ERROR_PREFIX}${wrapDataFetchId(dataFetchMapKey)}${err}`;
@@ -321,6 +325,10 @@ export function createLazyComponent<T, E extends keyof T>(
321325

322326
return (props: ComponentType) => {
323327
const { key, ...args } = props;
328+
if (cacheData && dataCache) {
329+
// @ts-expect-error ignore
330+
return <LazyComponent {...args} mfData={dataCache} />;
331+
}
324332
if (!options.noSSR) {
325333
return (
326334
<AwaitDataFetch

0 commit comments

Comments
 (0)