|
1 |
| -import React, { ReactNode } from 'react'; |
| 1 | +import React, { ReactNode, useState, useEffect } from 'react'; |
2 | 2 | import logger from './logger';
|
3 |
| -import { AwaitDataFetch } from './AwaitDataFetch'; |
| 3 | +import { AwaitDataFetch, transformError } from './AwaitDataFetch'; |
4 | 4 | import {
|
5 | 5 | fetchData,
|
6 | 6 | getDataFetchItem,
|
@@ -186,7 +186,7 @@ function getServerNeedRemoteInfo(
|
186 | 186 | globalName: loadedRemoteInfo.entryGlobalName,
|
187 | 187 | };
|
188 | 188 | }
|
189 |
| - return; |
| 189 | + return undefined; |
190 | 190 | }
|
191 | 191 |
|
192 | 192 | export function createLazyComponent<T, E extends keyof T>(
|
@@ -241,7 +241,7 @@ export function createLazyComponent<T, E extends keyof T>(
|
241 | 241 | id: moduleId,
|
242 | 242 | remoteSnapshot: loadedRemoteInfo.snapshot,
|
243 | 243 | }),
|
244 |
| - { name: instance!.name, version: instance?.options.version }, |
| 244 | + { name: instance.name, version: instance.options.version }, |
245 | 245 | );
|
246 | 246 | logger.debug('getData dataFetchMapKey: ', dataFetchMapKey);
|
247 | 247 | if (!dataFetchMapKey) {
|
@@ -319,17 +319,67 @@ export function createLazyComponent<T, E extends keyof T>(
|
319 | 319 | });
|
320 | 320 |
|
321 | 321 | return (props: ComponentType) => {
|
| 322 | + // eslint-disable-next-line max-lines |
322 | 323 | const { key, ...args } = props;
|
| 324 | + if (globalThis.FEDERATION_SSR && !options.noSSR) { |
| 325 | + return ( |
| 326 | + <AwaitDataFetch |
| 327 | + resolve={getData(options.noSSR)} |
| 328 | + loading={options.loading} |
| 329 | + errorElement={options.fallback} |
| 330 | + > |
| 331 | + {/* @ts-expect-error ignore */} |
| 332 | + {(data) => <LazyComponent {...args} mfData={data} />} |
| 333 | + </AwaitDataFetch> |
| 334 | + ); |
| 335 | + } else { |
| 336 | + // Client-side rendering logic |
| 337 | + const [data, setData] = useState<unknown>(null); |
| 338 | + const [loading, setLoading] = useState<boolean>(true); |
| 339 | + const [error, setError] = useState<ErrorInfo | null>(null); |
323 | 340 |
|
324 |
| - return ( |
325 |
| - <AwaitDataFetch |
326 |
| - resolve={getData(options.noSSR)} |
327 |
| - loading={options.loading} |
328 |
| - errorElement={options.fallback} |
329 |
| - > |
330 |
| - {/* @ts-ignore */} |
331 |
| - {(data) => <LazyComponent {...args} mfData={data} />} |
332 |
| - </AwaitDataFetch> |
333 |
| - ); |
| 341 | + useEffect(() => { |
| 342 | + let isMounted = true; |
| 343 | + const fetchDataAsync = async () => { |
| 344 | + try { |
| 345 | + setLoading(true); |
| 346 | + const result = await getData(options.noSSR); |
| 347 | + if (isMounted) { |
| 348 | + setData(result); |
| 349 | + } |
| 350 | + } catch (e) { |
| 351 | + if (isMounted) { |
| 352 | + setError(transformError(e as Error)); |
| 353 | + } |
| 354 | + } finally { |
| 355 | + if (isMounted) { |
| 356 | + setLoading(false); |
| 357 | + } |
| 358 | + } |
| 359 | + }; |
| 360 | + |
| 361 | + fetchDataAsync(); |
| 362 | + |
| 363 | + return () => { |
| 364 | + isMounted = false; |
| 365 | + }; |
| 366 | + }, []); |
| 367 | + |
| 368 | + if (loading) { |
| 369 | + return <>{options.loading}</>; |
| 370 | + } |
| 371 | + |
| 372 | + if (error) { |
| 373 | + return ( |
| 374 | + <> |
| 375 | + {typeof options.fallback === 'function' |
| 376 | + ? options.fallback(error) |
| 377 | + : options.fallback} |
| 378 | + </> |
| 379 | + ); |
| 380 | + } |
| 381 | + // @ts-expect-error ignore |
| 382 | + return <LazyComponent {...args} mfData={data} />; |
| 383 | + } |
334 | 384 | };
|
335 | 385 | }
|
0 commit comments