@module-federation/bridge-react 0.0.0-next-20250708121428 → 0.0.0-next-20250709032753

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +4 -8
  2. package/__tests__/bridge.spec.tsx +7 -7
  3. package/dist/{bridge-base-UGCwcMnG.js → bridge-base-BBH982Tz.cjs} +1 -1
  4. package/dist/{bridge-base-BoshEggF.mjs → bridge-base-P6pEjY1q.js} +1 -1
  5. package/dist/index-Cv3p6r66.cjs +235 -0
  6. package/dist/index-D4yt7Udv.js +236 -0
  7. package/dist/index.cjs.js +9 -35
  8. package/dist/index.d.ts +0 -143
  9. package/dist/index.es.js +12 -38
  10. package/dist/plugin.d.ts +0 -9
  11. package/dist/router-v5.cjs.js +1 -1
  12. package/dist/router-v5.d.ts +0 -9
  13. package/dist/router-v5.es.js +1 -1
  14. package/dist/router-v6.cjs.js +1 -1
  15. package/dist/router-v6.d.ts +0 -9
  16. package/dist/router-v6.es.js +1 -1
  17. package/dist/router.cjs.js +1 -1
  18. package/dist/router.d.ts +0 -9
  19. package/dist/router.es.js +1 -1
  20. package/dist/v18.cjs.js +1 -1
  21. package/dist/v18.d.ts +0 -9
  22. package/dist/v18.es.js +1 -1
  23. package/dist/v19.cjs.js +1 -1
  24. package/dist/v19.d.ts +0 -9
  25. package/dist/v19.es.js +1 -1
  26. package/package.json +5 -46
  27. package/src/.eslintrc.js +9 -0
  28. package/src/index.ts +1 -32
  29. package/src/remote/component.tsx +3 -3
  30. package/src/remote/create.tsx +4 -17
  31. package/tsconfig.json +1 -1
  32. package/vite.config.ts +0 -13
  33. package/dist/data-fetch-server-middleware.cjs.js +0 -163
  34. package/dist/data-fetch-server-middleware.d.ts +0 -15
  35. package/dist/data-fetch-server-middleware.es.js +0 -164
  36. package/dist/data-fetch-utils.cjs.js +0 -24
  37. package/dist/data-fetch-utils.d.ts +0 -86
  38. package/dist/data-fetch-utils.es.js +0 -26
  39. package/dist/index-C0fDZB5b.js +0 -45
  40. package/dist/index-CqxytsLY.mjs +0 -46
  41. package/dist/index.esm-BCeUd-x9.mjs +0 -418
  42. package/dist/index.esm-j_1sIRzg.js +0 -417
  43. package/dist/lazy-load-component-plugin-BbqmMGKu.mjs +0 -492
  44. package/dist/lazy-load-component-plugin-DgYHamxE.js +0 -491
  45. package/dist/lazy-load-component-plugin.cjs.js +0 -6
  46. package/dist/lazy-load-component-plugin.d.ts +0 -16
  47. package/dist/lazy-load-component-plugin.es.js +0 -6
  48. package/dist/lazy-utils.cjs.js +0 -24
  49. package/dist/lazy-utils.d.ts +0 -149
  50. package/dist/lazy-utils.es.js +0 -24
  51. package/dist/prefetch-BWabXlKU.js +0 -1332
  52. package/dist/prefetch-CXJhiNUD.mjs +0 -1333
  53. package/dist/utils-BTYYwZcb.mjs +0 -2016
  54. package/dist/utils-iEVlDmyk.js +0 -2015
  55. package/src/lazy/AwaitDataFetch.tsx +0 -215
  56. package/src/lazy/constant.ts +0 -30
  57. package/src/lazy/createLazyComponent.tsx +0 -418
  58. package/src/lazy/data-fetch/cache.ts +0 -296
  59. package/src/lazy/data-fetch/call-data-fetch.ts +0 -13
  60. package/src/lazy/data-fetch/data-fetch-server-middleware.ts +0 -196
  61. package/src/lazy/data-fetch/index.ts +0 -16
  62. package/src/lazy/data-fetch/inject-data-fetch.ts +0 -109
  63. package/src/lazy/data-fetch/prefetch.ts +0 -106
  64. package/src/lazy/data-fetch/runtime-plugin.ts +0 -115
  65. package/src/lazy/index.ts +0 -35
  66. package/src/lazy/logger.ts +0 -6
  67. package/src/lazy/types.ts +0 -75
  68. package/src/lazy/utils.ts +0 -372
  69. package/src/lazy/wrapNoSSR.tsx +0 -10
  70. package/src/plugins/lazy-load-component-plugin.ts +0 -62
@@ -1,106 +0,0 @@
1
- import type { getInstance } from '@module-federation/runtime';
2
- import {
3
- getDataFetchInfo,
4
- getDataFetchMap,
5
- getDataFetchMapKey,
6
- } from '../utils';
7
- import logger from '../logger';
8
- import helpers from '@module-federation/runtime/helpers';
9
- import { DataFetchParams } from '../types';
10
-
11
- export type PrefetchOptions = {
12
- id: string;
13
- instance: ReturnType<typeof getInstance>;
14
- dataFetchParams?: DataFetchParams;
15
- preloadComponentResource?: boolean;
16
- };
17
-
18
- export async function prefetch(options: PrefetchOptions) {
19
- const { instance, id, dataFetchParams, preloadComponentResource } = options;
20
- if (!id) {
21
- logger.error('id is required for prefetch!');
22
- return;
23
- }
24
- if (!instance) {
25
- logger.error('instance is required for prefetch!');
26
- return;
27
- }
28
- const matchedRemoteInfo = helpers.utils.matchRemoteWithNameAndExpose(
29
- instance.options.remotes,
30
- id,
31
- );
32
- if (!matchedRemoteInfo) {
33
- logger.error(`Can not found '${id}' in instance.options.remotes!`);
34
- return;
35
- }
36
- const { remote, expose } = matchedRemoteInfo;
37
- const { remoteSnapshot, globalSnapshot } =
38
- await instance.snapshotHandler.loadRemoteSnapshotInfo({
39
- moduleInfo: remote,
40
- id,
41
- expose,
42
- });
43
-
44
- if (preloadComponentResource) {
45
- const remoteInfo = helpers.utils.getRemoteInfo(remote);
46
-
47
- instance.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
48
- origin: instance,
49
- preloadOptions: {
50
- remote,
51
- preloadConfig: {
52
- nameOrAlias: remote.name,
53
- exposes: [expose],
54
- },
55
- },
56
- remote,
57
- remoteInfo,
58
- globalSnapshot,
59
- remoteSnapshot,
60
- });
61
- }
62
-
63
- const dataFetchMap = getDataFetchMap();
64
- if (!dataFetchMap) {
65
- return;
66
- }
67
- const dataFetchInfo = getDataFetchInfo({
68
- name: remote.name,
69
- alias: remote.alias,
70
- id,
71
- remoteSnapshot,
72
- });
73
-
74
- const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
75
- name: instance.name,
76
- version: instance.options.version,
77
- });
78
-
79
- if (!dataFetchMapKey) {
80
- return;
81
- }
82
-
83
- const dataFetchItem = dataFetchMap[dataFetchMapKey];
84
- if (!dataFetchItem) {
85
- return;
86
- }
87
-
88
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
89
- const [getDataFetchGetter, _type, getDataFetchPromise] = dataFetchItem[0];
90
-
91
- let _getDataFetchPromise = getDataFetchPromise;
92
- if (!getDataFetchPromise) {
93
- if (!getDataFetchGetter) {
94
- return;
95
- }
96
- _getDataFetchPromise = getDataFetchGetter();
97
- }
98
-
99
- _getDataFetchPromise!.then((dataFetchFn) => {
100
- return dataFetchFn({
101
- ...dataFetchParams,
102
- _id: dataFetchMapKey,
103
- isDowngrade: false,
104
- });
105
- });
106
- }
@@ -1,115 +0,0 @@
1
- import { injectDataFetch } from './inject-data-fetch';
2
- import {
3
- getDataFetchInfo,
4
- initDataFetchMap,
5
- getDataFetchItem,
6
- getDataFetchMap,
7
- isCSROnly,
8
- getDataFetchMapKey,
9
- isDataLoaderExpose,
10
- loadDataFetchModule,
11
- isServerEnv,
12
- } from '../utils';
13
- import logger from '../logger';
14
- import {
15
- MF_DATA_FETCH_TYPE,
16
- MF_DATA_FETCH_STATUS,
17
- DATA_FETCH_CLIENT_SUFFIX,
18
- } from '../constant';
19
-
20
- import type { MF_DATA_FETCH_MAP_VALUE } from '../types';
21
- import type { FederationRuntimePlugin } from '@module-federation/runtime';
22
-
23
- const autoFetchData: () => FederationRuntimePlugin = () => {
24
- initDataFetchMap();
25
- injectDataFetch();
26
- return {
27
- name: 'auto-fetch-data-plugin',
28
- afterLoadSnapshot(args) {
29
- const { id, moduleInfo, remoteSnapshot, host } = args;
30
- if (typeof id === 'string' && isDataLoaderExpose(id)) {
31
- return args;
32
- }
33
- if (!remoteSnapshot || !id || !('modules' in remoteSnapshot)) {
34
- return args;
35
- }
36
-
37
- const { name, alias } = moduleInfo;
38
- const dataFetchInfo = getDataFetchInfo({
39
- name,
40
- alias,
41
- id,
42
- remoteSnapshot,
43
- });
44
- if (!dataFetchInfo) {
45
- return args;
46
- }
47
- const { dataFetchId, dataFetchName } = dataFetchInfo;
48
-
49
- const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
50
- name: host.name,
51
- version: host.options.version,
52
- });
53
- logger.debug(
54
- '======= auto fetch plugin dataFetchMapKey: ',
55
- dataFetchMapKey,
56
- );
57
-
58
- if (!dataFetchMapKey) {
59
- return args;
60
- }
61
-
62
- const dataFetchItem = getDataFetchItem(dataFetchMapKey);
63
- if (dataFetchItem) {
64
- return args;
65
- }
66
-
67
- const dataFetchMap = getDataFetchMap();
68
- const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
69
- const hasDataFetchClient = Boolean(
70
- remoteSnapshot.modules.find(
71
- (module) =>
72
- module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`,
73
- ),
74
- );
75
- const downgradeType = hasDataFetchClient
76
- ? MF_DATA_FETCH_TYPE.FETCH_CLIENT
77
- : hasSSRAsset
78
- ? MF_DATA_FETCH_TYPE.FETCH_SERVER
79
- : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
80
- let finalDataFetchId = dataFetchId;
81
-
82
- if (!isServerEnv()) {
83
- finalDataFetchId =
84
- downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT
85
- ? hasDataFetchClient
86
- ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}`
87
- : dataFetchId
88
- : dataFetchId;
89
- }
90
-
91
- const getDataFetchGetter = () =>
92
- loadDataFetchModule(host, finalDataFetchId);
93
-
94
- const dataFetchFnItem: MF_DATA_FETCH_MAP_VALUE[0] = [
95
- getDataFetchGetter,
96
- downgradeType,
97
- ];
98
-
99
- // server client must execute
100
- if (typeof window === 'undefined' || isCSROnly()) {
101
- dataFetchFnItem.push(getDataFetchGetter());
102
- }
103
-
104
- dataFetchMap[dataFetchMapKey] = [
105
- dataFetchFnItem,
106
- undefined,
107
- MF_DATA_FETCH_STATUS.AWAIT,
108
- ];
109
-
110
- return args;
111
- },
112
- };
113
- };
114
-
115
- export default autoFetchData;
package/src/lazy/index.ts DELETED
@@ -1,35 +0,0 @@
1
- import autoFetchDataPlugin from './data-fetch/runtime-plugin';
2
-
3
- export { ERROR_TYPE } from './constant';
4
- export type { DataFetchParams, NoSSRRemoteInfo } from './types';
5
- export type {
6
- CreateLazyComponentOptions,
7
- IProps as CollectSSRAssetsOptions,
8
- } from './createLazyComponent';
9
-
10
- export { createLazyComponent, collectSSRAssets } from './createLazyComponent';
11
-
12
- // export { wrapNoSSR } from './wrapNoSSR';
13
-
14
- export {
15
- injectDataFetch,
16
- callDataFetch,
17
- CacheSize,
18
- CacheTime,
19
- configureCache,
20
- generateKey,
21
- cache,
22
- revalidateTag,
23
- clearStore,
24
- prefetch,
25
- } from './data-fetch';
26
-
27
- export { setSSREnv } from './utils';
28
-
29
- export { autoFetchDataPlugin };
30
-
31
- export type {
32
- CacheStatus,
33
- CacheStatsInfo,
34
- PrefetchOptions,
35
- } from './data-fetch';
@@ -1,6 +0,0 @@
1
- import { createLogger } from '@module-federation/sdk';
2
- import { PLUGIN_IDENTIFIER } from './constant';
3
-
4
- const logger = createLogger(PLUGIN_IDENTIFIER);
5
-
6
- export default logger;
package/src/lazy/types.ts DELETED
@@ -1,75 +0,0 @@
1
- import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from './constant';
2
- import type { LRUCache } from 'lru-cache';
3
-
4
- export type dataFetchFunctionOptions = [
5
- id?: string,
6
- data?: unknown,
7
- downgrade?: boolean,
8
- ];
9
-
10
- declare global {
11
- var _mfSSRDowngrade: string[] | true | undefined;
12
- var __MF_DATA_FETCH_MAP__: MF_DATA_FETCH_MAP | undefined;
13
- var FEDERATION_SERVER_QUERY: Record<string, unknown> | undefined;
14
- var FEDERATION_SSR: boolean | undefined;
15
- var _mfFSHref: string | undefined;
16
- var _mfDataFetch: Array<dataFetchFunctionOptions> | undefined;
17
- var __MF_DATA_FETCH_CACHE__: MF_DATA_FETCH_CACHE | undefined;
18
- }
19
-
20
- export interface CacheItem<T> {
21
- data: T;
22
- timestamp: number;
23
- isRevalidating?: boolean;
24
- }
25
-
26
- export type DataFetchParams = {
27
- isDowngrade: boolean;
28
- _id?: string;
29
- } & Record<string, unknown>;
30
- export type DataFetch<T> = (params: DataFetchParams) => Promise<T>;
31
-
32
- // loading, resolve, reject
33
- export type MF_DATA_FETCH_MAP_VALUE_PROMISE_SET = [
34
- Promise<unknown>,
35
- ((data: unknown) => void)?,
36
- ((err: unknown) => void)?,
37
- ];
38
- export type MF_DATA_FETCH_MAP_VALUE = [
39
- [
40
- // getDataFetchGetter
41
- () => Promise<DataFetch<unknown>>,
42
- // data fetch type
43
- MF_DATA_FETCH_TYPE,
44
- // getDataFetchPromise
45
- Promise<DataFetch<unknown>>?,
46
- ],
47
- MF_DATA_FETCH_MAP_VALUE_PROMISE_SET?,
48
- MF_DATA_FETCH_STATUS?,
49
- ];
50
- export type MF_DATA_FETCH_MAP = Record<string, MF_DATA_FETCH_MAP_VALUE>;
51
- export type MF_SSR_DOWNGRADE = string[] | true | undefined;
52
-
53
- export type NoSSRRemoteInfo = {
54
- name: string;
55
- version: string;
56
- ssrPublicPath: string;
57
- ssrRemoteEntry: string;
58
- globalName: string;
59
- };
60
-
61
- export interface CacheConfig {
62
- maxSize?: number;
63
- unstable_shouldDisable?: ({
64
- request,
65
- }: {
66
- request: Request;
67
- }) => boolean | Promise<boolean>;
68
- }
69
-
70
- // key is the id, default is the same with dataFetchMapId, but it can be custom by passing dataFetchParams.id
71
- export type MF_DATA_FETCH_CACHE = {
72
- cacheStore?: LRUCache<string, Map<string, CacheItem<any>>>;
73
- tagKeyMap?: Map<string, Set<string>>;
74
- cacheConfig?: CacheConfig;
75
- };
package/src/lazy/utils.ts DELETED
@@ -1,372 +0,0 @@
1
- import { isBrowserEnv, composeKeyWithSeparator } from '@module-federation/sdk';
2
- import logger from './logger';
3
- import {
4
- DOWNGRADE_KEY,
5
- MF_DATA_FETCH_STATUS,
6
- WRAP_DATA_FETCH_ID_IDENTIFIER,
7
- DATA_FETCH_QUERY,
8
- MF_DATA_FETCH_TYPE,
9
- DATA_FETCH_IDENTIFIER,
10
- DATA_FETCH_CLIENT_SUFFIX,
11
- } from './constant';
12
-
13
- import type { GlobalModuleInfo } from '@module-federation/sdk';
14
- import type {
15
- DataFetchParams,
16
- MF_DATA_FETCH_MAP,
17
- NoSSRRemoteInfo,
18
- MF_SSR_DOWNGRADE,
19
- MF_DATA_FETCH_MAP_VALUE_PROMISE_SET,
20
- MF_DATA_FETCH_CACHE,
21
- } from './types';
22
- import type { FederationHost } from '@module-federation/runtime';
23
- import { clearStore } from './data-fetch/cache';
24
-
25
- export const getDataFetchInfo = ({
26
- name,
27
- alias,
28
- id,
29
- remoteSnapshot,
30
- }: {
31
- id: string;
32
- name: string;
33
- remoteSnapshot: GlobalModuleInfo[string];
34
- alias?: string;
35
- }) => {
36
- if (!remoteSnapshot) {
37
- return;
38
- }
39
- if (!('modules' in remoteSnapshot)) {
40
- return;
41
- }
42
- const regex = new RegExp(`^${name}(/[^/].*|)$`);
43
- const nameOrAlias = regex.test(id) ? name : alias || name;
44
-
45
- const expose = id.replace(nameOrAlias, '');
46
- let dataFetchName = '';
47
- let dataFetchId = '';
48
- let dataFetchKey = '';
49
- if (expose.startsWith('/')) {
50
- dataFetchName = `${expose.slice(1)}.${DATA_FETCH_IDENTIFIER}`;
51
- dataFetchId = `${id}.${DATA_FETCH_IDENTIFIER}`;
52
- dataFetchKey = `${name}${expose}.${DATA_FETCH_IDENTIFIER}`;
53
- } else if (expose === '') {
54
- dataFetchName = DATA_FETCH_IDENTIFIER;
55
- dataFetchId = `${id}/${DATA_FETCH_IDENTIFIER}`;
56
- dataFetchKey = `${name}/${DATA_FETCH_IDENTIFIER}`;
57
- } else {
58
- return;
59
- }
60
-
61
- if (!dataFetchName || !dataFetchId || !dataFetchKey) {
62
- return;
63
- }
64
-
65
- if (
66
- !remoteSnapshot.modules.find(
67
- (module) => module.moduleName === dataFetchName,
68
- )
69
- ) {
70
- return;
71
- }
72
-
73
- return {
74
- dataFetchName,
75
- dataFetchId,
76
- dataFetchKey,
77
- };
78
- };
79
-
80
- export function initDataFetchMap() {
81
- globalThis.__MF_DATA_FETCH_MAP__ ||= {};
82
- }
83
-
84
- export function getDataFetchItem(id: string) {
85
- return (globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP)?.[id];
86
- }
87
-
88
- export function getDataFetchMap() {
89
- return globalThis.__MF_DATA_FETCH_MAP__ as MF_DATA_FETCH_MAP;
90
- }
91
-
92
- export function getDataFetchCache() {
93
- return globalThis.__MF_DATA_FETCH_CACHE__ as MF_DATA_FETCH_CACHE;
94
- }
95
-
96
- export const flushDataFetch = () => {
97
- globalThis.__MF_DATA_FETCH_MAP__ = {};
98
- globalThis[DOWNGRADE_KEY] = undefined;
99
- clearStore();
100
- };
101
-
102
- export function setDataFetchItemLoadedStatus(id: string) {
103
- const dataFetchItem = getDataFetchItem(id);
104
- if (!dataFetchItem) {
105
- return;
106
- }
107
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
108
- }
109
-
110
- export const wrapDataFetchId = (id?: string) => {
111
- return `${WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${WRAP_DATA_FETCH_ID_IDENTIFIER}`;
112
- };
113
-
114
- export const getDataFetchIdWithErrorMsgs = (errMsgs: string) => {
115
- const firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
116
- if (firstIdentifierIndex === -1) {
117
- return undefined;
118
- }
119
-
120
- const secondIdentifierIndex = errMsgs.indexOf(
121
- WRAP_DATA_FETCH_ID_IDENTIFIER,
122
- firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
123
- );
124
-
125
- if (secondIdentifierIndex === -1) {
126
- return undefined;
127
- }
128
-
129
- return errMsgs.substring(
130
- firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
131
- secondIdentifierIndex,
132
- );
133
- };
134
-
135
- export async function fetchData(
136
- id: string,
137
- params: DataFetchParams,
138
- remoteInfo?: NoSSRRemoteInfo,
139
- ): Promise<unknown | undefined> {
140
- const callFetchData = async () => {
141
- const item = getDataFetchItem(id);
142
- if (!item) {
143
- return;
144
- }
145
- const [fetchDataFnArr, ..._rest] = item;
146
-
147
- const fetchDataFn = await fetchDataFnArr[2];
148
- if (!fetchDataFn) {
149
- return;
150
- }
151
- return fetchDataFn({
152
- ...params,
153
- _id: id,
154
- });
155
- };
156
- if (isBrowserEnv()) {
157
- const dataFetchItem = getDataFetchItem(id);
158
- if (!dataFetchItem) {
159
- throw new Error(`dataFetchItem not found, id: ${id}`);
160
- }
161
- if (dataFetchItem[1]?.[0]) {
162
- return dataFetchItem[1][0];
163
- }
164
-
165
- if (isCSROnly()) {
166
- logger.debug('==========csr only!');
167
- return callFetchData();
168
- }
169
-
170
- if (remoteInfo) {
171
- return callDowngrade(id, params, remoteInfo);
172
- }
173
-
174
- const mfDowngrade = getDowngradeTag();
175
- if (
176
- mfDowngrade &&
177
- (typeof mfDowngrade === 'boolean' || mfDowngrade.includes(id))
178
- ) {
179
- return callDowngrade(id, { ...params, isDowngrade: true });
180
- }
181
-
182
- let res;
183
- let rej;
184
- const p = new Promise((resolve, reject) => {
185
- res = resolve;
186
- rej = reject;
187
- });
188
- dataFetchItem[1] = [p, res, rej];
189
- dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
190
- return dataFetchItem[1][0];
191
- }
192
-
193
- return callFetchData();
194
- }
195
-
196
- export function getDataFetchMapKey(
197
- dataFetchInfo?: ReturnType<typeof getDataFetchInfo>,
198
- hostInfo?: { name: string; version?: string },
199
- ) {
200
- if (!dataFetchInfo || !hostInfo) {
201
- return;
202
- }
203
-
204
- const { dataFetchKey } = dataFetchInfo;
205
-
206
- return composeKeyWithSeparator(dataFetchKey, hostInfo.name, hostInfo.version);
207
- }
208
-
209
- export async function loadDataFetchModule(
210
- instance: FederationHost,
211
- id: string,
212
- ) {
213
- return instance.loadRemote(id).then((m) => {
214
- if (
215
- m &&
216
- typeof m === 'object' &&
217
- 'fetchData' in m &&
218
- typeof m.fetchData === 'function'
219
- ) {
220
- return m.fetchData as (params: DataFetchParams) => Promise<unknown>;
221
- }
222
- throw new Error(
223
- `fetchData not found in remote ${id}, ${JSON.stringify(m)}`,
224
- );
225
- });
226
- }
227
-
228
- export function isDataLoaderExpose(exposeKey: string) {
229
- return (
230
- exposeKey.endsWith(DATA_FETCH_IDENTIFIER) ||
231
- exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX)
232
- );
233
- }
234
-
235
- export function getDowngradeTag() {
236
- return globalThis[DOWNGRADE_KEY] as MF_SSR_DOWNGRADE;
237
- }
238
-
239
- export function callAllDowngrade() {
240
- const dataFetchMap = getDataFetchMap();
241
- if (!dataFetchMap) {
242
- return;
243
- }
244
- Object.keys(dataFetchMap).forEach((key) => {
245
- callDowngrade(key);
246
- });
247
- }
248
-
249
- export async function callDowngrade(
250
- id: string,
251
- params?: DataFetchParams,
252
- remoteInfo?: NoSSRRemoteInfo,
253
- ) {
254
- const dataFetchMap = getDataFetchMap();
255
- if (!dataFetchMap) {
256
- return;
257
- }
258
- const mfDataFetch = dataFetchMap[id];
259
- if (mfDataFetch?.[2] === MF_DATA_FETCH_STATUS.AWAIT) {
260
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
261
- let promise: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[0];
262
- let res: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[1];
263
- let rej: MF_DATA_FETCH_MAP_VALUE_PROMISE_SET[2];
264
- if (mfDataFetch[1]) {
265
- promise = mfDataFetch[1][0];
266
- res = mfDataFetch[1][1];
267
- rej = mfDataFetch[1][2];
268
- } else {
269
- promise = new Promise((resolve, reject) => {
270
- res = resolve;
271
- rej = reject;
272
- });
273
- mfDataFetch[1] = [promise, res, rej];
274
- }
275
- const dataFetchType = mfDataFetch[0][1];
276
- if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT) {
277
- try {
278
- mfDataFetch[0][0]().then(async (getDataFetchFn) => {
279
- return getDataFetchFn({
280
- ...params,
281
- isDowngrade: true,
282
- _id: id,
283
- }).then((data) => {
284
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
285
- res && res(data);
286
- });
287
- });
288
- } catch (e) {
289
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
290
- rej && rej(e);
291
- }
292
- } else if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER) {
293
- try {
294
- const currentUrl = new URL(window.location.href);
295
- currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
296
- if (params) {
297
- currentUrl.searchParams.set(
298
- 'params',
299
- encodeURIComponent(JSON.stringify(params)),
300
- );
301
- }
302
- if (remoteInfo) {
303
- currentUrl.searchParams.set(
304
- 'remoteInfo',
305
- encodeURIComponent(JSON.stringify(remoteInfo)),
306
- );
307
- }
308
- const fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
309
- if (fetchServerQuery && typeof fetchServerQuery === 'object') {
310
- Object.keys(fetchServerQuery).forEach((key) => {
311
- currentUrl.searchParams.set(
312
- key,
313
- JSON.stringify(fetchServerQuery[key]),
314
- );
315
- });
316
- }
317
- const fetchUrl = currentUrl.toString();
318
- const data = await fetch(fetchUrl).then((res) => res.json());
319
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
320
- res && res(data);
321
- } catch (e) {
322
- mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
323
- rej && rej(e);
324
- }
325
- }
326
-
327
- return promise;
328
- }
329
- }
330
-
331
- export function isCSROnly() {
332
- // @ts-ignore modern.js will inject window._SSR_DATA if enable ssr
333
- return window._SSR_DATA === undefined;
334
- }
335
-
336
- export function isServerEnv() {
337
- return typeof window === 'undefined';
338
- }
339
-
340
- export function setSSREnv({
341
- fetchServerQuery,
342
- }: {
343
- fetchServerQuery?: Record<string, unknown>;
344
- }) {
345
- globalThis.FEDERATION_SSR = true;
346
- globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
347
- }
348
-
349
- export function getLoadedRemoteInfos(
350
- id: string,
351
- instance: FederationHost | null,
352
- ) {
353
- if (!instance) {
354
- return;
355
- }
356
- const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
357
- if (!name) {
358
- return;
359
- }
360
- const module = instance.moduleCache.get(name);
361
- if (!module) {
362
- return;
363
- }
364
- const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(
365
- module.remoteInfo,
366
- );
367
- return {
368
- ...module.remoteInfo,
369
- snapshot: remoteSnapshot,
370
- expose,
371
- };
372
- }