@@ -13,13 +13,31 @@ const isMfComponent = component => mfAppNamesRegex.test(component);
13
13
* @return {string[] } chunk ids of the rendered components.
14
14
*/
15
15
export const getLoadableRequiredComponents = extractor => {
16
- const loadableElement = extractor
17
- . getScriptElements ( )
18
- . find ( el => el . key === '__LOADABLE_REQUIRED_CHUNKS___ext' ) ;
16
+ const scriptElements = extractor ?. getScriptElements ?.( ) ?? [ ] ;
19
17
20
- const { namedChunks } = JSON . parse ( loadableElement . props . dangerouslySetInnerHTML . __html ) ;
18
+ const loadableElement = scriptElements . find (
19
+ el => el ?. key === '__LOADABLE_REQUIRED_CHUNKS___ext' ,
20
+ ) ;
21
21
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
+ }
23
41
} ;
24
42
25
43
const getMfRenderedComponents = loadableRequiredComponents => {
@@ -31,21 +49,48 @@ const getMfRenderedComponents = loadableRequiredComponents => {
31
49
32
50
const getMFStats = async ( ) => {
33
51
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
+ }
35
61
} ;
36
62
37
63
export const getMfChunks = async extractor => {
38
64
const loadableRequiredComponents = getLoadableRequiredComponents ( extractor ) ;
39
65
66
+ if ( ! loadableRequiredComponents . length ) {
67
+ return [ [ ] , [ ] ] ;
68
+ }
69
+
40
70
const mfRenderedComponents = getMfRenderedComponents ( loadableRequiredComponents ) ;
41
71
72
+ if ( ! mfRenderedComponents . length ) {
73
+ return [ [ ] , [ ] ] ;
74
+ }
75
+
42
76
const mfChunks = await getMFStats ( ) ;
43
77
44
- const scriptsArr = [ ] ;
45
- const stylesArr = [ ] ;
78
+ if ( ! mfChunks . length ) {
79
+ return [ [ ] , [ ] ] ;
80
+ }
81
+
82
+ const scriptsArr : string [ ] = [ ] ;
83
+ const stylesArr : string [ ] = [ ] ;
84
+
46
85
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 => {
49
94
const url = 'http://localhost:3001/static/' + chunk ;
50
95
51
96
url . endsWith ( '.css' ) ? stylesArr . push ( url ) : scriptsArr . push ( url ) ;
0 commit comments