@@ -37,6 +37,7 @@ export type CreateLazyComponentOptions<T, E extends keyof T> = {
37
37
export ?: E ;
38
38
dataFetchParams ?: DataFetchParams ;
39
39
noSSR ?: boolean ;
40
+ cacheData ?: boolean ;
40
41
} ;
41
42
42
43
type ReactKey = { key ?: React . Key | null } ;
@@ -192,10 +193,12 @@ function getServerNeedRemoteInfo(
192
193
export function createLazyComponent < T , E extends keyof T > (
193
194
options : CreateLazyComponentOptions < T , E > ,
194
195
) {
195
- const { instance } = options ;
196
+ const { instance, cacheData } = options ;
196
197
if ( ! instance ) {
197
198
throw new Error ( 'instance is required for createLazyComponent!' ) ;
198
199
}
200
+ let dataCache : unknown = null ;
201
+
199
202
type ComponentType = T [ E ] extends ( ...args : any ) => any
200
203
? Parameters < T [ E ] > [ 0 ] extends undefined
201
204
? ReactKey
@@ -257,6 +260,7 @@ export function createLazyComponent<T, E extends keyof T>(
257
260
) ;
258
261
setDataFetchItemLoadedStatus ( dataFetchMapKey ) ;
259
262
logger . debug ( 'get data res: \n' , data ) ;
263
+ dataCache = data ;
260
264
return data ;
261
265
} catch ( err ) {
262
266
const errMsg = `${ DATA_FETCH_ERROR_PREFIX } ${ wrapDataFetchId ( dataFetchMapKey ) } ${ err } ` ;
@@ -321,6 +325,10 @@ export function createLazyComponent<T, E extends keyof T>(
321
325
322
326
return ( props : ComponentType ) => {
323
327
const { key, ...args } = props ;
328
+ if ( cacheData && dataCache ) {
329
+ // @ts -expect-error ignore
330
+ return < LazyComponent { ...args } mfData = { dataCache } /> ;
331
+ }
324
332
if ( ! options . noSSR ) {
325
333
return (
326
334
< AwaitDataFetch
0 commit comments