1
1
import React from 'react' ;
2
- import helpers from '@module-federation/runtime/helpers' ;
3
- import {
4
- getInstance ,
5
- type FederationHost ,
6
- } from '@module-federation/enhanced/runtime' ;
7
- import { isBrowserEnv } from '@module-federation/sdk' ;
2
+ import logger from './logger' ;
3
+ import { getInstance } from '@module-federation/enhanced/runtime' ;
8
4
import {
9
5
ErrorBoundary ,
10
6
ErrorBoundaryPropsWithComponent ,
11
7
} from 'react-error-boundary' ;
12
- import { getDataFetchInfo } from './utils' ;
13
8
import { Await } from './Await' ;
9
+ import { fetchData , getDataFetchId } from './dataFetch' ;
10
+ import { getLoadedRemoteInfos } from './utils' ;
14
11
15
12
type IProps = {
16
13
id : string ;
@@ -20,81 +17,6 @@ type IProps = {
20
17
21
18
type ReactKey = { key ?: React . Key | null } ;
22
19
23
- async function fetchData (
24
- id : string ,
25
- uid : string ,
26
- ) : Promise < unknown | undefined > {
27
- if ( isBrowserEnv ( ) ) {
28
- if ( ! globalThis . _MF__DATA_FETCH_ID_MAP__ ) {
29
- globalThis . _MF__DATA_FETCH_ID_MAP__ = { } ;
30
- }
31
- if ( globalThis . _MF__DATA_FETCH_ID_MAP__ [ uid ] ) {
32
- return ;
33
- }
34
- let res ;
35
- let rej ;
36
- const p = new Promise ( ( resolve , reject ) => {
37
- res = resolve ;
38
- rej = reject ;
39
- } ) ;
40
- globalThis . _MF__DATA_FETCH_ID_MAP__ [ uid ] = [ p , res , rej ] ;
41
- return globalThis . _MF__DATA_FETCH_ID_MAP__ [ uid ] [ 0 ] ;
42
- }
43
- const instance = getInstance ( ) ;
44
- if ( ! instance ) {
45
- return ;
46
- }
47
-
48
- const { name } = instance . remoteHandler . idToRemoteMap [ id ] || { } ;
49
- if ( ! name ) {
50
- return ;
51
- }
52
- const module = instance . moduleCache . get ( name ) ;
53
- if ( ! module ) {
54
- return ;
55
- }
56
- const dataFetchInfo = getDataFetchInfo ( {
57
- id,
58
- name,
59
- alias : module . remoteInfo . alias ,
60
- } ) ;
61
- if ( ! dataFetchInfo ) {
62
- return ;
63
- }
64
- const key = `${ name } @${ module . remoteInfo . version } @${ dataFetchInfo . dataFetchName } ` ;
65
- const fetchDataPromise =
66
- helpers . global . nativeGlobal . __FEDERATION__ . __DATA_FETCH_MAP__ . get ( key ) ;
67
- if ( ! fetchDataPromise ) {
68
- return ;
69
- }
70
-
71
- const fetchDataFn = await fetchDataPromise ;
72
- if ( ! fetchDataFn ) {
73
- return ;
74
- }
75
- console . log ( 'fetchDataFn: ' , fetchDataFn . toString ( ) ) ;
76
- return fetchDataFn ( ) ;
77
- }
78
-
79
- function getLoadedRemoteInfos ( instance : FederationHost , id : string ) {
80
- const { name, expose } = instance . remoteHandler . idToRemoteMap [ id ] || { } ;
81
- if ( ! name ) {
82
- return ;
83
- }
84
- const module = instance . moduleCache . get ( name ) ;
85
- if ( ! module ) {
86
- return ;
87
- }
88
- const { remoteSnapshot } = instance . snapshotHandler . getGlobalRemoteInfo (
89
- module . remoteInfo ,
90
- ) ;
91
- return {
92
- ...module . remoteInfo ,
93
- snapshot : remoteSnapshot ,
94
- expose,
95
- } ;
96
- }
97
-
98
20
function getTargetModuleInfo ( id : string ) {
99
21
const instance = getInstance ( ) ;
100
22
if ( ! instance ) {
@@ -199,9 +121,6 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
199
121
fallback : ErrorBoundaryPropsWithComponent [ 'FallbackComponent' ] ;
200
122
export ?: E ;
201
123
} ) {
202
- // const [uid,setUid] = useState('')
203
- console . log ( 'createRemoteSSRComponent trigger' ) ;
204
-
205
124
type ComponentType = T [ E ] extends ( ...args : any ) => any
206
125
? Parameters < T [ E ] > [ 0 ] extends undefined
207
126
? ReactKey
@@ -219,18 +138,24 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
219
138
} ;
220
139
221
140
const getData = async ( ) => {
222
- // const nodeUid = isBrowserEnv () ? document.get : uid
223
141
const m = await callLoader ( ) ;
224
142
const moduleId = m && m [ Symbol . for ( 'mf_module_id' ) ] ;
225
- const data = await fetchData ( moduleId , moduleId ) ;
226
- console . log ( 'data: ' , data ) ;
143
+ const dataFetchId = getDataFetchId ( moduleId ) ;
144
+ logger . debug ( 'getData dataFetchId: ' , dataFetchId ) ;
145
+ if ( ! dataFetchId ) {
146
+ return ;
147
+ }
148
+ const data = await fetchData ( dataFetchId ) ;
149
+ logger . debug ( 'data: \n' , data ) ;
227
150
return data ;
228
151
} ;
229
152
230
153
const LazyComponent = React . lazy ( async ( ) => {
231
154
try {
232
155
const m = await callLoader ( ) ;
233
156
const moduleId = m && m [ Symbol . for ( 'mf_module_id' ) ] ;
157
+ const dataFetchId = getDataFetchId ( moduleId ) ;
158
+ logger . debug ( 'LazyComponent dataFetchId: ' , dataFetchId ) ;
234
159
235
160
const assets = collectSSRAssets ( {
236
161
id : moduleId ,
@@ -247,7 +172,7 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
247
172
suppressHydrationWarning
248
173
dangerouslySetInnerHTML = { {
249
174
__html : String . raw `
250
- globalThis._MF__DATA_FETCH_ID_MAP__['${ moduleId } '][1](${ JSON . stringify ( props . _mfData ) } )
175
+ globalThis._MF__DATA_FETCH_ID_MAP__['${ dataFetchId } '][1](${ JSON . stringify ( props . _mfData ) } )
251
176
` ,
252
177
} }
253
178
> </ script >
0 commit comments