@module-federation/modern-js 0.14.3 → 0.15.0
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.
- package/dist/cjs/cli/configPlugin.js +17 -13
- package/dist/cjs/cli/configPlugin.spec.js +3 -1
- package/dist/cjs/cli/index.js +3 -1
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
- package/dist/cjs/cli/ssrPlugin.js +31 -12
- package/dist/cjs/cli/utils.js +5 -0
- package/dist/cjs/constant.js +49 -2
- package/dist/cjs/{ssr-runtime/index.js → interfaces/global.js} +2 -8
- package/dist/cjs/{cli/logger.js → logger.js} +1 -1
- package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
- package/dist/cjs/runtime/createRemoteComponent.js +327 -0
- package/dist/cjs/runtime/index.js +15 -3
- package/dist/cjs/{cli/constant.js → runtime/wrapNoSSR.js} +13 -6
- package/dist/cjs/ssr-runtime/{plugin.js → devPlugin.js} +9 -6
- package/dist/cjs/ssr-runtime/downgrade.js +114 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
- package/dist/cjs/utils/dataFetch.js +211 -0
- package/dist/cjs/utils/index.js +54 -0
- package/dist/esm/cli/configPlugin.js +18 -14
- package/dist/esm/cli/configPlugin.spec.js +3 -1
- package/dist/esm/cli/index.js +3 -1
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
- package/dist/esm/cli/ssrPlugin.js +28 -8
- package/dist/esm/cli/utils.js +4 -0
- package/dist/esm/constant.js +37 -1
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/{cli/logger.js → logger.js} +1 -1
- package/dist/esm/runtime/AwaitDataFetch.js +131 -0
- package/dist/esm/runtime/createRemoteComponent.js +417 -0
- package/dist/esm/runtime/index.js +13 -3
- package/dist/esm/runtime/wrapNoSSR.js +12 -0
- package/dist/esm/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
- package/dist/esm/ssr-runtime/downgrade.js +150 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
- package/dist/esm/utils/dataFetch.js +237 -0
- package/dist/esm/utils/index.js +28 -0
- package/dist/esm-node/cli/configPlugin.js +18 -14
- package/dist/esm-node/cli/configPlugin.spec.js +3 -1
- package/dist/esm-node/cli/index.js +3 -1
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
- package/dist/esm-node/cli/ssrPlugin.js +27 -8
- package/dist/esm-node/cli/utils.js +4 -0
- package/dist/esm-node/constant.js +37 -1
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/{cli/logger.js → logger.js} +1 -1
- package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
- package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
- package/dist/esm-node/runtime/index.js +13 -3
- package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
- package/dist/esm-node/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
- package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
- package/dist/esm-node/utils/dataFetch.js +166 -0
- package/dist/esm-node/utils/index.js +27 -0
- package/dist/types/cli/configPlugin.d.ts +1 -1
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
- package/dist/types/cli/utils.d.ts +1 -0
- package/dist/types/constant.d.ts +23 -0
- package/dist/types/interfaces/global.d.ts +27 -0
- package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
- package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
- package/dist/types/runtime/index.d.ts +7 -2
- package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
- package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
- package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/utils/dataFetch.d.ts +26 -0
- package/dist/types/utils/index.d.ts +15 -0
- package/package.json +37 -12
- package/dist/cjs/cli/manifest.js +0 -75
- package/dist/cjs/runtime/createRemoteSSRComponent.js +0 -201
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/manifest.js +0 -41
- package/dist/esm/runtime/createRemoteSSRComponent.js +0 -191
- package/dist/esm/ssr-runtime/index.js +0 -1
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/manifest.js +0 -41
- package/dist/esm-node/runtime/createRemoteSSRComponent.js +0 -166
- package/dist/esm-node/ssr-runtime/index.js +0 -1
- package/dist/types/cli/constant.d.ts +0 -1
- package/dist/types/cli/manifest.d.ts +0 -2
- package/dist/types/runtime/createRemoteSSRComponent.d.ts +0 -18
- package/dist/types/ssr-runtime/index.d.ts +0 -1
- package/dist/types/ssr-runtime/plugin.d.ts +0 -2
- /package/dist/types/{cli/logger.d.ts → logger.d.ts} +0 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { DATA_FETCH_QUERY, MF_DATA_FETCH_STATUS } from "../../constant";
|
|
2
|
+
import logger from "../../logger";
|
|
3
|
+
import { getDataFetchMap } from "../../utils";
|
|
4
|
+
import { fetchData, initDataFetchMap, loadDataFetchModule } from "../../utils/dataFetch";
|
|
5
|
+
import { SEPARATOR, MANIFEST_EXT } from "@module-federation/sdk";
|
|
6
|
+
function wrapSetTimeout(targetPromise, delay = 2e4, id) {
|
|
7
|
+
if (targetPromise && typeof targetPromise.then === "function") {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
const timeoutId = setTimeout(() => {
|
|
10
|
+
logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
|
|
11
|
+
reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
|
|
12
|
+
}, delay);
|
|
13
|
+
targetPromise.then((value) => {
|
|
14
|
+
clearTimeout(timeoutId);
|
|
15
|
+
resolve(value);
|
|
16
|
+
}).catch((err) => {
|
|
17
|
+
clearTimeout(timeoutId);
|
|
18
|
+
reject(err);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function addProtocol(url) {
|
|
24
|
+
if (url.startsWith("//")) {
|
|
25
|
+
return "https:" + url;
|
|
26
|
+
}
|
|
27
|
+
return url;
|
|
28
|
+
}
|
|
29
|
+
const getDecodeQuery = (url, name) => {
|
|
30
|
+
const res = url.searchParams.get(name);
|
|
31
|
+
if (!res) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return decodeURIComponent(res);
|
|
35
|
+
};
|
|
36
|
+
const middleware = async (ctx, next) => {
|
|
37
|
+
var _globalThis___FEDERATION__;
|
|
38
|
+
let url;
|
|
39
|
+
let dataFetchId;
|
|
40
|
+
let params;
|
|
41
|
+
let remoteInfo;
|
|
42
|
+
try {
|
|
43
|
+
url = new URL(ctx.req.url);
|
|
44
|
+
dataFetchId = getDecodeQuery(url, DATA_FETCH_QUERY);
|
|
45
|
+
params = JSON.parse(getDecodeQuery(url, "params") || "{}");
|
|
46
|
+
const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
|
|
47
|
+
remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
logger.error("fetch data from server, error: ", e);
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
if (!dataFetchId) {
|
|
53
|
+
return next();
|
|
54
|
+
}
|
|
55
|
+
logger.log("fetch data from server, dataFetchId: ", dataFetchId);
|
|
56
|
+
logger.debug("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
|
|
57
|
+
try {
|
|
58
|
+
var _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1;
|
|
59
|
+
const dataFetchMap = getDataFetchMap();
|
|
60
|
+
if (!dataFetchMap) {
|
|
61
|
+
initDataFetchMap();
|
|
62
|
+
}
|
|
63
|
+
const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
|
|
64
|
+
logger.debug("fetch data from server, fetchDataPromise: ", fetchDataPromise);
|
|
65
|
+
if (fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== MF_DATA_FETCH_STATUS.ERROR) {
|
|
66
|
+
const targetPromise = fetchDataPromise[0];
|
|
67
|
+
const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
|
|
68
|
+
if (wrappedPromise) {
|
|
69
|
+
const res = await wrappedPromise;
|
|
70
|
+
logger.log("fetch data from server, fetchDataPromise res: ", res);
|
|
71
|
+
return ctx.json(res);
|
|
72
|
+
}
|
|
73
|
+
logger.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
|
|
74
|
+
}
|
|
75
|
+
if (remoteInfo) {
|
|
76
|
+
try {
|
|
77
|
+
const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
78
|
+
const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
|
|
79
|
+
if (!hostInstance2) {
|
|
80
|
+
throw new Error("host instance not found!");
|
|
81
|
+
}
|
|
82
|
+
const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
|
|
83
|
+
logger.debug("find remote: ", JSON.stringify(remote));
|
|
84
|
+
if (!remote) {
|
|
85
|
+
hostInstance2.registerRemotes([
|
|
86
|
+
{
|
|
87
|
+
name: remoteInfo.name,
|
|
88
|
+
entry: remoteEntry,
|
|
89
|
+
entryGlobalName: remoteInfo.globalName
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
} else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
|
|
93
|
+
const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
|
|
94
|
+
logger.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
|
|
95
|
+
logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
|
|
96
|
+
if (!hostGlobalSnapshot || !remoteSnapshot) {
|
|
97
|
+
if ("version" in remote) {
|
|
98
|
+
delete remote.version;
|
|
99
|
+
}
|
|
100
|
+
remote.entry = remoteEntry;
|
|
101
|
+
remote.entryGlobalName = remoteInfo.globalName;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} catch (e) {
|
|
105
|
+
ctx.status(500);
|
|
106
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
107
|
+
${e}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const dataFetchItem = dataFetchMap[dataFetchId];
|
|
111
|
+
logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
|
|
112
|
+
if (dataFetchItem) {
|
|
113
|
+
const callFetchDataPromise = fetchData(dataFetchId, {
|
|
114
|
+
...params,
|
|
115
|
+
isDowngrade: !remoteInfo
|
|
116
|
+
});
|
|
117
|
+
const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
|
|
118
|
+
if (wrappedPromise) {
|
|
119
|
+
const res = await wrappedPromise;
|
|
120
|
+
logger.log("fetch data from server, dataFetchItem res: ", res);
|
|
121
|
+
return ctx.json(res);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const remoteId = dataFetchId.split(SEPARATOR)[0];
|
|
125
|
+
const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
126
|
+
if (!hostInstance) {
|
|
127
|
+
throw new Error("host instance not found!");
|
|
128
|
+
}
|
|
129
|
+
const dataFetchFn = await loadDataFetchModule(hostInstance, remoteId);
|
|
130
|
+
const data = await dataFetchFn({
|
|
131
|
+
...params,
|
|
132
|
+
isDowngrade: !remoteInfo
|
|
133
|
+
});
|
|
134
|
+
logger.log("fetch data from server, loadDataFetchModule res: ", data);
|
|
135
|
+
return ctx.json(data);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
logger.error("server plugin data fetch error: ", e);
|
|
138
|
+
ctx.status(500);
|
|
139
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
140
|
+
${e}`);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const dataFetchServePlugin = () => ({
|
|
144
|
+
name: "mf-data-fetch-server-plugin",
|
|
145
|
+
setup: (api) => {
|
|
146
|
+
api.onPrepare(() => {
|
|
147
|
+
const { middlewares } = api.getServerContext();
|
|
148
|
+
middlewares.push({
|
|
149
|
+
name: "module-federation-serve-manifest",
|
|
150
|
+
// @ts-ignore type error
|
|
151
|
+
handler: middleware
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
var data_fetch_server_plugin_default = dataFetchServePlugin;
|
|
157
|
+
export {
|
|
158
|
+
data_fetch_server_plugin_default as default
|
|
159
|
+
};
|
|
@@ -3,9 +3,9 @@ import fs from "fs-extra";
|
|
|
3
3
|
import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
|
|
4
4
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
5
5
|
import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
6
|
-
import
|
|
7
|
-
import { isDev } from "./
|
|
8
|
-
import
|
|
6
|
+
import logger from "../logger";
|
|
7
|
+
import { isDev } from "./utils";
|
|
8
|
+
import { updateStatsAndManifest } from "@module-federation/rsbuild-plugin/manifest";
|
|
9
9
|
import { isWebTarget, skipByTarget } from "./utils";
|
|
10
10
|
function setEnv() {
|
|
11
11
|
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
@@ -76,15 +76,23 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
76
76
|
}
|
|
77
77
|
setEnv();
|
|
78
78
|
api._internalRuntimePlugins(({ entrypoint, plugins }) => {
|
|
79
|
-
|
|
79
|
+
const { fetchServerQuery } = pluginOptions;
|
|
80
|
+
plugins.push({
|
|
81
|
+
name: "injectDataFetchFunction",
|
|
82
|
+
path: "@module-federation/modern-js/ssr-inject-data-fetch-function-plugin",
|
|
83
|
+
config: {
|
|
84
|
+
fetchServerQuery
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (!isDev()) {
|
|
80
88
|
return {
|
|
81
89
|
entrypoint,
|
|
82
90
|
plugins
|
|
83
91
|
};
|
|
84
92
|
}
|
|
85
93
|
plugins.push({
|
|
86
|
-
name: "
|
|
87
|
-
path: "@module-federation/modern-js/ssr-
|
|
94
|
+
name: "mfSSRDev",
|
|
95
|
+
path: "@module-federation/modern-js/ssr-dev-plugin",
|
|
88
96
|
config: {}
|
|
89
97
|
});
|
|
90
98
|
return {
|
|
@@ -92,6 +100,17 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
92
100
|
plugins
|
|
93
101
|
};
|
|
94
102
|
});
|
|
103
|
+
if (pluginOptions.ssrConfig.remotes) {
|
|
104
|
+
api._internalServerPlugins(({ plugins }) => {
|
|
105
|
+
plugins.push({
|
|
106
|
+
name: "@module-federation/modern-js/data-fetch-server-plugin",
|
|
107
|
+
options: {}
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
plugins
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
95
114
|
api.modifyBundlerChain((chain) => {
|
|
96
115
|
const target = chain.get("target");
|
|
97
116
|
if (skipByTarget(target)) {
|
|
@@ -112,7 +131,7 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
112
131
|
}
|
|
113
132
|
if (!isWeb) {
|
|
114
133
|
chain.target("async-node");
|
|
115
|
-
if (isDev) {
|
|
134
|
+
if (isDev()) {
|
|
116
135
|
chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
|
|
117
136
|
}
|
|
118
137
|
const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
|
|
@@ -121,7 +140,7 @@ const moduleFederationSSRPlugin = (pluginOptions) => ({
|
|
|
121
140
|
chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
|
|
122
141
|
}
|
|
123
142
|
}
|
|
124
|
-
if (isDev && isWeb) {
|
|
143
|
+
if (isDev() && isWeb) {
|
|
125
144
|
chain.externals({
|
|
126
145
|
"@module-federation/node/utils": "NOT_USED_IN_BROWSER"
|
|
127
146
|
});
|
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
const LOCALHOST = "localhost";
|
|
2
2
|
const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
|
|
3
|
+
const DATA_FETCH_QUERY = "x-mf-data-fetch";
|
|
4
|
+
const DATA_FETCH_ERROR_PREFIX = "caught the following error during dataFetch: ";
|
|
5
|
+
const LOAD_REMOTE_ERROR_PREFIX = "caught the following error during loadRemote: ";
|
|
6
|
+
const DOWNGRADE_KEY = "_mfSSRDowngrade";
|
|
7
|
+
const DATA_FETCH_MAP_KEY = "__MF_DATA_FETCH_MAP__";
|
|
8
|
+
const DATA_FETCH_FUNCTION = "_mfDataFetch";
|
|
9
|
+
const FS_HREF = "_mfFSHref";
|
|
10
|
+
const ERROR_TYPE = {
|
|
11
|
+
DATA_FETCH: 1,
|
|
12
|
+
LOAD_REMOTE: 2,
|
|
13
|
+
UNKNOWN: 3
|
|
14
|
+
};
|
|
15
|
+
const WRAP_DATA_FETCH_ID_IDENTIFIER = "wrap_dfip_identifier";
|
|
16
|
+
var MF_DATA_FETCH_TYPE;
|
|
17
|
+
(function(MF_DATA_FETCH_TYPE2) {
|
|
18
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_SERVER"] = 1] = "FETCH_SERVER";
|
|
19
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_CLIENT"] = 2] = "FETCH_CLIENT";
|
|
20
|
+
})(MF_DATA_FETCH_TYPE || (MF_DATA_FETCH_TYPE = {}));
|
|
21
|
+
var MF_DATA_FETCH_STATUS;
|
|
22
|
+
(function(MF_DATA_FETCH_STATUS2) {
|
|
23
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADED"] = 1] = "LOADED";
|
|
24
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADING"] = 2] = "LOADING";
|
|
25
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["AWAIT"] = 0] = "AWAIT";
|
|
26
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["ERROR"] = 3] = "ERROR";
|
|
27
|
+
})(MF_DATA_FETCH_STATUS || (MF_DATA_FETCH_STATUS = {}));
|
|
3
28
|
export {
|
|
29
|
+
DATA_FETCH_ERROR_PREFIX,
|
|
30
|
+
DATA_FETCH_FUNCTION,
|
|
31
|
+
DATA_FETCH_MAP_KEY,
|
|
32
|
+
DATA_FETCH_QUERY,
|
|
33
|
+
DOWNGRADE_KEY,
|
|
34
|
+
ERROR_TYPE,
|
|
35
|
+
FS_HREF,
|
|
36
|
+
LOAD_REMOTE_ERROR_PREFIX,
|
|
4
37
|
LOCALHOST,
|
|
5
|
-
|
|
38
|
+
MF_DATA_FETCH_STATUS,
|
|
39
|
+
MF_DATA_FETCH_TYPE,
|
|
40
|
+
PLUGIN_IDENTIFIER,
|
|
41
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER
|
|
6
42
|
};
|
|
File without changes
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Suspense, useRef } from "react";
|
|
3
|
+
import logger from "../logger";
|
|
4
|
+
import { DATA_FETCH_ERROR_PREFIX, LOAD_REMOTE_ERROR_PREFIX, ERROR_TYPE, DATA_FETCH_FUNCTION } from "../constant";
|
|
5
|
+
import { getDataFetchIdWithErrorMsgs, wrapDataFetchId } from "../utils";
|
|
6
|
+
function isPromise(obj) {
|
|
7
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
|
|
8
|
+
}
|
|
9
|
+
const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
|
|
10
|
+
const transformError = (err) => {
|
|
11
|
+
const errMsg = err instanceof Error ? err.message : err;
|
|
12
|
+
const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
|
|
13
|
+
const dataFetchMapKey = getDataFetchIdWithErrorMsgs(originalMsg);
|
|
14
|
+
if (originalMsg.indexOf(DATA_FETCH_ERROR_PREFIX) === 0) {
|
|
15
|
+
return {
|
|
16
|
+
error: new Error(originalMsg.replace(DATA_FETCH_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")),
|
|
17
|
+
errorType: ERROR_TYPE.DATA_FETCH,
|
|
18
|
+
dataFetchMapKey
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (originalMsg.indexOf(LOAD_REMOTE_ERROR_PREFIX) === 0) {
|
|
22
|
+
return {
|
|
23
|
+
error: new Error(originalMsg.replace(LOAD_REMOTE_ERROR_PREFIX, "").replace(wrapDataFetchId(dataFetchMapKey), "")),
|
|
24
|
+
errorType: ERROR_TYPE.LOAD_REMOTE,
|
|
25
|
+
dataFetchMapKey
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
error: new Error(originalMsg.replace(wrapDataFetchId(dataFetchMapKey), "")),
|
|
30
|
+
errorType: ERROR_TYPE.UNKNOWN,
|
|
31
|
+
dataFetchMapKey
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
const DefaultLoading = /* @__PURE__ */ _jsx(_Fragment, {});
|
|
35
|
+
const DefaultErrorElement = (_data) => /* @__PURE__ */ _jsx("div", {
|
|
36
|
+
children: "Error"
|
|
37
|
+
});
|
|
38
|
+
function AwaitDataFetch({ resolve, loading = DefaultLoading, errorElement = DefaultErrorElement, children, params }) {
|
|
39
|
+
const dataRef = useRef();
|
|
40
|
+
const data = dataRef.current || resolve;
|
|
41
|
+
const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
|
|
42
|
+
return /* @__PURE__ */ _jsx(AwaitSuspense, {
|
|
43
|
+
params,
|
|
44
|
+
loading,
|
|
45
|
+
errorElement,
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
resolve: getData,
|
|
48
|
+
children
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function AwaitSuspense({ resolve, children, loading = DefaultLoading, errorElement = DefaultErrorElement }) {
|
|
52
|
+
return /* @__PURE__ */ _jsx(Suspense, {
|
|
53
|
+
fallback: loading,
|
|
54
|
+
children: /* @__PURE__ */ _jsx(ResolveAwait, {
|
|
55
|
+
resolve,
|
|
56
|
+
errorElement,
|
|
57
|
+
children
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function ResolveAwait({ children, resolve, errorElement, params }) {
|
|
62
|
+
const data = resolve();
|
|
63
|
+
logger.debug("resolve data: ", data);
|
|
64
|
+
if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
|
|
65
|
+
const transformedError = transformError(data);
|
|
66
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
67
|
+
children: typeof errorElement === "function" ? /* @__PURE__ */ _jsxs(_Fragment, {
|
|
68
|
+
children: [
|
|
69
|
+
globalThis.FEDERATION_SSR && /* @__PURE__ */ _jsx("script", {
|
|
70
|
+
suppressHydrationWarning: true,
|
|
71
|
+
dangerouslySetInnerHTML: {
|
|
72
|
+
__html: String.raw`
|
|
73
|
+
globalThis['${DATA_FETCH_FUNCTION}'] = globalThis['${DATA_FETCH_FUNCTION}'] || []
|
|
74
|
+
globalThis['${DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
|
|
75
|
+
}
|
|
76
|
+
}),
|
|
77
|
+
errorElement(transformedError)
|
|
78
|
+
]
|
|
79
|
+
}) : errorElement
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const toRender = typeof children === "function" ? children(data) : children;
|
|
83
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
84
|
+
children: toRender
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const fetchData = (promise, ref) => {
|
|
88
|
+
let data;
|
|
89
|
+
let status = "pending";
|
|
90
|
+
const suspender = promise.then((res) => {
|
|
91
|
+
status = "success";
|
|
92
|
+
data = res;
|
|
93
|
+
ref.current = res;
|
|
94
|
+
}).catch((e) => {
|
|
95
|
+
status = "success";
|
|
96
|
+
console.warn(e);
|
|
97
|
+
data = AWAIT_ERROR_PREFIX + e;
|
|
98
|
+
});
|
|
99
|
+
return () => {
|
|
100
|
+
if (status === "pending") {
|
|
101
|
+
throw suspender;
|
|
102
|
+
}
|
|
103
|
+
return data;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export {
|
|
107
|
+
AwaitDataFetch,
|
|
108
|
+
transformError
|
|
109
|
+
};
|