@@ -42,7 +42,7 @@ export type CreateLazyComponentOptions<T, E extends keyof T> = {
42
42
fallback : ReactNode | ( ( errorInfo : ErrorInfo ) => ReactNode ) ;
43
43
export ?: E ;
44
44
dataFetchParams ?: DataFetchParams ;
45
- // noSSR?: boolean;
45
+ noSSR ?: boolean ;
46
46
cacheData ?: boolean ;
47
47
} ;
48
48
@@ -157,12 +157,11 @@ export function collectSSRAssets(options: IProps) {
157
157
function getServerNeedRemoteInfo (
158
158
loadedRemoteInfo : ReturnType < typeof getLoadedRemoteInfos > ,
159
159
id : string ,
160
- // noSSR?: boolean,
160
+ noSSR ?: boolean ,
161
161
) : NoSSRRemoteInfo | undefined {
162
162
if (
163
- // noSSR ||
164
- typeof window !== 'undefined' &&
165
- window . location . href !== window [ FS_HREF ]
163
+ noSSR ||
164
+ ( typeof window !== 'undefined' && window . location . href !== window [ FS_HREF ] )
166
165
) {
167
166
if ( ! loadedRemoteInfo ?. version ) {
168
167
throw new Error ( `${ loadedRemoteInfo ?. name } version is empty` ) ;
@@ -233,8 +232,7 @@ export function createLazyComponent<T, E extends keyof T>(
233
232
return m ;
234
233
} ;
235
234
236
- // const getData = async (noSSR?: boolean) => {
237
- const getData = async ( ) => {
235
+ const getData = async ( noSSR ?: boolean ) => {
238
236
let loadedRemoteInfo : ReturnType < typeof getLoadedRemoteInfos > ;
239
237
let moduleId : string ;
240
238
try {
@@ -273,8 +271,7 @@ export function createLazyComponent<T, E extends keyof T>(
273
271
...options . dataFetchParams ,
274
272
isDowngrade : false ,
275
273
} ,
276
- // getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR),
277
- getServerNeedRemoteInfo ( loadedRemoteInfo , dataFetchMapKey ) ,
274
+ getServerNeedRemoteInfo ( loadedRemoteInfo , dataFetchMapKey , noSSR ) ,
278
275
) ;
279
276
setDataFetchItemLoadedStatus ( dataFetchMapKey ) ;
280
277
logger . debug ( 'get data res: \n' , data ) ;
@@ -347,72 +344,71 @@ export function createLazyComponent<T, E extends keyof T>(
347
344
// @ts -expect-error ignore
348
345
return < LazyComponent { ...args } mfData = { dataCache } /> ;
349
346
}
350
- return (
351
- < AwaitDataFetch
352
- // resolve={getData(options.noSSR)}
353
- resolve = { getData ( ) }
354
- loading = { options . loading }
355
- delayLoading = { options . delayLoading }
356
- errorElement = { options . fallback }
357
- >
358
- { /* @ts -expect-error ignore */ }
359
- { ( data ) => < LazyComponent { ...args } mfData = { data } /> }
360
- </ AwaitDataFetch >
361
- ) ;
362
- // if (!options.noSSR) {
363
347
364
- // } else {
365
- // // Client-side rendering logic
366
- // const [data, setData] = useState<unknown>(null);
367
- // const [loading, setLoading] = useState<boolean>(true);
368
- // const [error, setError] = useState<ErrorInfo | null>(null);
348
+ if ( ! options . noSSR ) {
349
+ return (
350
+ < AwaitDataFetch
351
+ resolve = { getData ( options . noSSR ) }
352
+ loading = { options . loading }
353
+ delayLoading = { options . delayLoading }
354
+ errorElement = { options . fallback }
355
+ >
356
+ { /* @ts -expect-error ignore */ }
357
+ { ( data ) => < LazyComponent { ...args } mfData = { data } /> }
358
+ </ AwaitDataFetch >
359
+ ) ;
360
+ } else {
361
+ // Client-side rendering logic
362
+ const [ data , setData ] = useState < unknown > ( null ) ;
363
+ const [ loading , setLoading ] = useState < boolean > ( true ) ;
364
+ const [ error , setError ] = useState < ErrorInfo | null > ( null ) ;
369
365
370
- // useEffect(() => {
371
- // let isMounted = true;
372
- // const fetchDataAsync = async () => {
373
- // try {
374
- // setLoading(true);
375
- // const result = await getData(options.noSSR);
376
- // if (isMounted) {
377
- // setData(result);
378
- // }
379
- // } catch (e) {
380
- // if (isMounted) {
381
- // setError(transformError(e as Error));
382
- // }
383
- // } finally {
384
- // if (isMounted) {
385
- // setLoading(false);
386
- // }
387
- // }
388
- // };
366
+ useEffect ( ( ) => {
367
+ let isMounted = true ;
368
+ const fetchDataAsync = async ( ) => {
369
+ try {
370
+ setLoading ( true ) ;
371
+ const result = await getData ( options . noSSR ) ;
372
+ if ( isMounted ) {
373
+ setData ( result ) ;
374
+ }
375
+ } catch ( e ) {
376
+ if ( isMounted ) {
377
+ setError ( transformError ( e as Error ) ) ;
378
+ }
379
+ } finally {
380
+ if ( isMounted ) {
381
+ setLoading ( false ) ;
382
+ }
383
+ }
384
+ } ;
389
385
390
- // fetchDataAsync();
386
+ fetchDataAsync ( ) ;
391
387
392
- // return () => {
393
- // isMounted = false;
394
- // };
395
- // }, []);
388
+ return ( ) => {
389
+ isMounted = false ;
390
+ } ;
391
+ } , [ ] ) ;
396
392
397
- // if (loading) {
398
- // return (
399
- // <DelayedLoading delayLoading={options.delayLoading}>
400
- // {options.loading}
401
- // </DelayedLoading>
402
- // );
403
- // }
393
+ if ( loading ) {
394
+ return (
395
+ < DelayedLoading delayLoading = { options . delayLoading } >
396
+ { options . loading }
397
+ </ DelayedLoading >
398
+ ) ;
399
+ }
404
400
405
- // if (error) {
406
- // return (
407
- // <>
408
- // {typeof options.fallback === 'function'
409
- // ? options.fallback(error)
410
- // : options.fallback }
411
- // </>
412
- // );
413
- // }
414
- // // @ts -expect-error ignore
415
- // return <LazyComponent {...args} mfData={data} />;
416
- // }
401
+ if ( error ) {
402
+ return (
403
+ < >
404
+ { typeof options . fallback === 'function'
405
+ ? options . fallback ( error )
406
+ : options . fallback }
407
+ </ >
408
+ ) ;
409
+ }
410
+ // @ts -expect-error ignore
411
+ return < LazyComponent { ...args } mfData = { data } /> ;
412
+ }
417
413
} ;
418
414
}
0 commit comments