@@ -13,13 +13,31 @@ const isMfComponent = component => mfAppNamesRegex.test(component);
1313 * @return {string[] } chunk ids of the rendered components.
1414 */
1515export const getLoadableRequiredComponents = extractor => {
16- const loadableElement = extractor
17- . getScriptElements ( )
18- . find ( el => el . key === '__LOADABLE_REQUIRED_CHUNKS___ext' ) ;
16+ const scriptElements = extractor ?. getScriptElements ?.( ) ?? [ ] ;
1917
20- const { namedChunks } = JSON . parse ( loadableElement . props . dangerouslySetInnerHTML . __html ) ;
18+ const loadableElement = scriptElements . find (
19+ el => el ?. key === '__LOADABLE_REQUIRED_CHUNKS___ext' ,
20+ ) ;
2121
22- return namedChunks ;
22+ if ( ! loadableElement ) {
23+ return [ ] ;
24+ }
25+
26+ try {
27+ const rawHtml = loadableElement . props ?. dangerouslySetInnerHTML ?. __html ;
28+
29+ if ( ! rawHtml ) {
30+ return [ ] ;
31+ }
32+
33+ const parsedData = JSON . parse ( rawHtml ) ;
34+ const { namedChunks } = parsedData ?? { } ;
35+
36+ return Array . isArray ( namedChunks ) ? namedChunks : [ ] ;
37+ } catch ( error ) {
38+ console . error ( '[getLoadableRequiredComponents] Failed to parse required chunks' , error ) ;
39+ return [ ] ;
40+ }
2341} ;
2442
2543const getMfRenderedComponents = loadableRequiredComponents => {
@@ -31,21 +49,48 @@ const getMfRenderedComponents = loadableRequiredComponents => {
3149
3250const getMFStats = async ( ) => {
3351 const promises = Object . values ( mfStatsUrlMap ) . map ( url => axios . get ( url ) ) ;
34- return Promise . all ( promises ) . then ( responses => responses . map ( response => response . data ) ) ;
52+
53+ try {
54+ const responses = await Promise . all ( promises ) ;
55+
56+ return responses . map ( response => response . data ) ;
57+ } catch ( error ) {
58+ console . error ( '[getMFStats] Failed to fetch remote federation stats' , error ) ;
59+ return [ ] ;
60+ }
3561} ;
3662
3763export const getMfChunks = async extractor => {
3864 const loadableRequiredComponents = getLoadableRequiredComponents ( extractor ) ;
3965
66+ if ( ! loadableRequiredComponents . length ) {
67+ return [ [ ] , [ ] ] ;
68+ }
69+
4070 const mfRenderedComponents = getMfRenderedComponents ( loadableRequiredComponents ) ;
4171
72+ if ( ! mfRenderedComponents . length ) {
73+ return [ [ ] , [ ] ] ;
74+ }
75+
4276 const mfChunks = await getMFStats ( ) ;
4377
44- const scriptsArr = [ ] ;
45- const stylesArr = [ ] ;
78+ if ( ! mfChunks . length ) {
79+ return [ [ ] , [ ] ] ;
80+ }
81+
82+ const scriptsArr : string [ ] = [ ] ;
83+ const stylesArr : string [ ] = [ ] ;
84+
4685 mfRenderedComponents . forEach ( ( [ appName , component ] ) => {
47- const remoteStats = mfChunks . find ( remote => remote . name === appName ) ;
48- remoteStats . exposes [ component ] . forEach ( chunk => {
86+ const remoteStats = mfChunks . find ( remote => remote ?. name === appName ) ;
87+ const exposeChunks = remoteStats ?. exposes ?. [ component ] ;
88+
89+ if ( ! Array . isArray ( exposeChunks ) ) {
90+ return ;
91+ }
92+
93+ exposeChunks . forEach ( chunk => {
4994 const url = 'http://localhost:3001/static/' + chunk ;
5095
5196 url . endsWith ( '.css' ) ? stylesArr . push ( url ) : scriptsArr . push ( url ) ;
0 commit comments