@module-federation/retry-plugin 0.6.14 → 0.6.15

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @module-federation/retry-plugin
2
2
 
3
+ ## 0.6.15
4
+
5
+ ### Patch Changes
6
+
7
+ - ec31539: fix(retry-plugin): fix script retry logic to make error-boundary should render until script retry finished.
8
+ - 283574b: feat(retry-plugin): allow fallback function to receive failed URL
9
+ - @module-federation/sdk@0.6.15
10
+
3
11
  ## 0.6.14
4
12
 
5
13
  ### Patch Changes
package/dist/esm/index.js CHANGED
@@ -4,10 +4,6 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // packages/retry-plugin/src/constant.ts
5
5
  var defaultRetries = 3;
6
6
  var defaultRetryDelay = 1e3;
7
- var loadStatus = {
8
- success: "success",
9
- error: "error"
10
- };
11
7
  var PLUGIN_IDENTIFIER = "[ Module Federation RetryPlugin ]";
12
8
 
13
9
  // packages/retry-plugin/src/logger.ts
@@ -32,7 +28,7 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
32
28
  logger_default.log(`>>>>>>>>> retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
33
29
  if (fallback && typeof fallback === "function") {
34
30
  return fetchWithRetry({
35
- url: fallback(),
31
+ url: fallback(url),
36
32
  options,
37
33
  retryTimes: 0,
38
34
  retryDelay: 0
@@ -57,81 +53,6 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
57
53
  }
58
54
  __name(fetchWithRetry, "fetchWithRetry");
59
55
 
60
- // packages/retry-plugin/src/script-retry.ts
61
- var defaultCreateScript = /* @__PURE__ */ __name((url, attrs) => {
62
- let script = document.createElement("script");
63
- script.src = url;
64
- Object.keys(attrs).forEach((key) => {
65
- if (key === "async" || key === "defer") {
66
- script[key] = attrs[key];
67
- } else if (!script.getAttribute(key)) {
68
- script.setAttribute(key, attrs[key]);
69
- }
70
- });
71
- return script;
72
- }, "defaultCreateScript");
73
- var getScript = /* @__PURE__ */ __name((url, attrs, customCreateScript) => {
74
- let script = null;
75
- if (customCreateScript && typeof customCreateScript === "function") {
76
- script = customCreateScript(url, attrs);
77
- }
78
- if (!script) {
79
- script = defaultCreateScript(url, attrs);
80
- }
81
- return script;
82
- }, "getScript");
83
- async function loadScript(url, attrs, maxRetries = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript) {
84
- let retries = 0;
85
- function attemptLoad() {
86
- return new Promise((resolve, reject) => {
87
- const script = getScript(url, attrs, customCreateScript);
88
- script.onload = (event) => {
89
- resolve({
90
- status: loadStatus.success,
91
- event
92
- });
93
- };
94
- script.onerror = (event) => {
95
- if (retries < maxRetries) {
96
- retries++;
97
- logger_default.warn(`Failed to load script. Retrying... (${retries}/${maxRetries})`);
98
- retryDelay > 0 && setTimeout(() => {
99
- resolve(attemptLoad());
100
- }, retryDelay);
101
- } else {
102
- logger_default.error(`Failed to load script after maximum retries. the url is: ${url}`);
103
- resolve({
104
- status: loadStatus.error,
105
- event
106
- });
107
- }
108
- };
109
- document.head.appendChild(script);
110
- });
111
- }
112
- __name(attemptLoad, "attemptLoad");
113
- return attemptLoad();
114
- }
115
- __name(loadScript, "loadScript");
116
- function scriptWithRetry({ url, attrs = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript }) {
117
- const script = getScript(url, attrs, customCreateScript);
118
- const originOnerror = script.onerror;
119
- const originOnLoad = script.onload;
120
- script.onerror = async (event) => {
121
- logger_default.warn(`Script load failed, retrying (${retryTimes + 1}/${defaultRetries}): ${url}`);
122
- const scriptLoader = await loadScript(url, attrs, retryTimes, retryDelay, customCreateScript);
123
- if (scriptLoader.status === loadStatus.success) {
124
- originOnLoad?.call(script, scriptLoader?.event);
125
- return;
126
- } else {
127
- originOnerror?.call(script, scriptLoader?.event);
128
- }
129
- return;
130
- };
131
- return script;
132
- }
133
- __name(scriptWithRetry, "scriptWithRetry");
134
-
135
56
  // packages/retry-plugin/src/index.ts
136
57
  var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
137
58
  name: "retry-plugin",
@@ -161,31 +82,26 @@ var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOp
161
82
  }
162
83
  return fetch(url, options);
163
84
  },
164
- createScript({ url, attrs }) {
165
- const scriptAttrs = scriptOption?.attrs ? {
166
- ...attrs,
167
- ...scriptOption.attrs
168
- } : attrs;
169
- if (scriptOption) {
170
- if (scriptOption?.url) {
171
- if (url === scriptOption?.url) {
172
- return scriptWithRetry({
173
- url: scriptOption?.url,
174
- attrs: scriptAttrs,
175
- retryTimes: scriptOption?.retryTimes,
176
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
177
- });
85
+ async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) {
86
+ let moduleFactory;
87
+ const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay } = scriptOption || {};
88
+ if (scriptOption?.moduleName && scriptOption?.moduleName.some((m) => moduleInfo.name === m || moduleInfo?.alias === m) || scriptOption?.moduleName === void 0) {
89
+ let attempts = 0;
90
+ while (attempts - 1 < retryTimes) {
91
+ try {
92
+ moduleFactory = await remoteEntryExports.get(expose);
93
+ break;
94
+ } catch (error) {
95
+ attempts++;
96
+ if (attempts - 1 >= retryTimes) {
97
+ scriptOption?.cb && await new Promise((resolve) => scriptOption?.cb && scriptOption?.cb(resolve, error));
98
+ throw error;
99
+ }
100
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
178
101
  }
179
- } else {
180
- return scriptWithRetry({
181
- url,
182
- attrs: scriptAttrs,
183
- retryTimes: scriptOption?.retryTimes,
184
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
185
- });
186
102
  }
187
103
  }
188
- return {};
104
+ return moduleFactory;
189
105
  }
190
106
  }), "RetryPlugin");
191
107
  export {
package/dist/index.d.mts CHANGED
@@ -1,31 +1,27 @@
1
1
  import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2
2
 
3
3
  interface FetchWithRetryOptions {
4
- url?: string;
4
+ url: string;
5
5
  options?: RequestInit;
6
6
  retryTimes?: number;
7
7
  retryDelay?: number;
8
- fallback?: () => string;
8
+ fallback?:
9
+ | (() => string)
10
+ | ((url: string | URL | globalThis.Request) => string);
9
11
  }
10
12
 
11
13
  interface ScriptWithRetryOptions {
12
- url?: string;
13
- attrs?: Record<string, string>;
14
14
  retryTimes?: number;
15
15
  retryDelay?: number;
16
- customCreateScript?: CreateScriptFunc;
16
+ moduleName?: Array<string>;
17
+ cb?: (resolve: (value: unknown) => void, error: any) => void;
17
18
  }
18
19
 
19
20
  type RetryPluginParams = {
20
- fetch?: FetchWithRetryOptions; // fetch retry options
21
- script?: ScriptWithRetryOptions; // script retry options
21
+ fetch?: FetchWithRetryOptions;
22
+ script?: ScriptWithRetryOptions;
22
23
  };
23
24
 
24
- type CreateScriptFunc = (
25
- url: string,
26
- attrs: Record<string, any>,
27
- ) => HTMLScriptElement;
28
-
29
25
  declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
30
26
 
31
27
  export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
package/dist/index.d.ts CHANGED
@@ -1,31 +1,27 @@
1
1
  import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2
2
 
3
3
  interface FetchWithRetryOptions {
4
- url?: string;
4
+ url: string;
5
5
  options?: RequestInit;
6
6
  retryTimes?: number;
7
7
  retryDelay?: number;
8
- fallback?: () => string;
8
+ fallback?:
9
+ | (() => string)
10
+ | ((url: string | URL | globalThis.Request) => string);
9
11
  }
10
12
 
11
13
  interface ScriptWithRetryOptions {
12
- url?: string;
13
- attrs?: Record<string, string>;
14
14
  retryTimes?: number;
15
15
  retryDelay?: number;
16
- customCreateScript?: CreateScriptFunc;
16
+ moduleName?: Array<string>;
17
+ cb?: (resolve: (value: unknown) => void, error: any) => void;
17
18
  }
18
19
 
19
20
  type RetryPluginParams = {
20
- fetch?: FetchWithRetryOptions; // fetch retry options
21
- script?: ScriptWithRetryOptions; // script retry options
21
+ fetch?: FetchWithRetryOptions;
22
+ script?: ScriptWithRetryOptions;
22
23
  };
23
24
 
24
- type CreateScriptFunc = (
25
- url: string,
26
- attrs: Record<string, any>,
27
- ) => HTMLScriptElement;
28
-
29
25
  declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
30
26
 
31
27
  export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
package/dist/index.js CHANGED
@@ -28,10 +28,6 @@ module.exports = __toCommonJS(src_exports);
28
28
  // packages/retry-plugin/src/constant.ts
29
29
  var defaultRetries = 3;
30
30
  var defaultRetryDelay = 1e3;
31
- var loadStatus = {
32
- success: "success",
33
- error: "error"
34
- };
35
31
  var PLUGIN_IDENTIFIER = "[ Module Federation RetryPlugin ]";
36
32
 
37
33
  // packages/retry-plugin/src/logger.ts
@@ -56,7 +52,7 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
56
52
  logger_default.log(`>>>>>>>>> retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
57
53
  if (fallback && typeof fallback === "function") {
58
54
  return fetchWithRetry({
59
- url: fallback(),
55
+ url: fallback(url),
60
56
  options,
61
57
  retryTimes: 0,
62
58
  retryDelay: 0
@@ -81,81 +77,6 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
81
77
  }
82
78
  __name(fetchWithRetry, "fetchWithRetry");
83
79
 
84
- // packages/retry-plugin/src/script-retry.ts
85
- var defaultCreateScript = /* @__PURE__ */ __name((url, attrs) => {
86
- let script = document.createElement("script");
87
- script.src = url;
88
- Object.keys(attrs).forEach((key) => {
89
- if (key === "async" || key === "defer") {
90
- script[key] = attrs[key];
91
- } else if (!script.getAttribute(key)) {
92
- script.setAttribute(key, attrs[key]);
93
- }
94
- });
95
- return script;
96
- }, "defaultCreateScript");
97
- var getScript = /* @__PURE__ */ __name((url, attrs, customCreateScript) => {
98
- let script = null;
99
- if (customCreateScript && typeof customCreateScript === "function") {
100
- script = customCreateScript(url, attrs);
101
- }
102
- if (!script) {
103
- script = defaultCreateScript(url, attrs);
104
- }
105
- return script;
106
- }, "getScript");
107
- async function loadScript(url, attrs, maxRetries = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript) {
108
- let retries = 0;
109
- function attemptLoad() {
110
- return new Promise((resolve, reject) => {
111
- const script = getScript(url, attrs, customCreateScript);
112
- script.onload = (event) => {
113
- resolve({
114
- status: loadStatus.success,
115
- event
116
- });
117
- };
118
- script.onerror = (event) => {
119
- if (retries < maxRetries) {
120
- retries++;
121
- logger_default.warn(`Failed to load script. Retrying... (${retries}/${maxRetries})`);
122
- retryDelay > 0 && setTimeout(() => {
123
- resolve(attemptLoad());
124
- }, retryDelay);
125
- } else {
126
- logger_default.error(`Failed to load script after maximum retries. the url is: ${url}`);
127
- resolve({
128
- status: loadStatus.error,
129
- event
130
- });
131
- }
132
- };
133
- document.head.appendChild(script);
134
- });
135
- }
136
- __name(attemptLoad, "attemptLoad");
137
- return attemptLoad();
138
- }
139
- __name(loadScript, "loadScript");
140
- function scriptWithRetry({ url, attrs = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript }) {
141
- const script = getScript(url, attrs, customCreateScript);
142
- const originOnerror = script.onerror;
143
- const originOnLoad = script.onload;
144
- script.onerror = async (event) => {
145
- logger_default.warn(`Script load failed, retrying (${retryTimes + 1}/${defaultRetries}): ${url}`);
146
- const scriptLoader = await loadScript(url, attrs, retryTimes, retryDelay, customCreateScript);
147
- if (scriptLoader.status === loadStatus.success) {
148
- originOnLoad?.call(script, scriptLoader?.event);
149
- return;
150
- } else {
151
- originOnerror?.call(script, scriptLoader?.event);
152
- }
153
- return;
154
- };
155
- return script;
156
- }
157
- __name(scriptWithRetry, "scriptWithRetry");
158
-
159
80
  // packages/retry-plugin/src/index.ts
160
81
  var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
161
82
  name: "retry-plugin",
@@ -185,31 +106,26 @@ var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOp
185
106
  }
186
107
  return fetch(url, options);
187
108
  },
188
- createScript({ url, attrs }) {
189
- const scriptAttrs = scriptOption?.attrs ? {
190
- ...attrs,
191
- ...scriptOption.attrs
192
- } : attrs;
193
- if (scriptOption) {
194
- if (scriptOption?.url) {
195
- if (url === scriptOption?.url) {
196
- return scriptWithRetry({
197
- url: scriptOption?.url,
198
- attrs: scriptAttrs,
199
- retryTimes: scriptOption?.retryTimes,
200
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
201
- });
109
+ async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) {
110
+ let moduleFactory;
111
+ const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay } = scriptOption || {};
112
+ if (scriptOption?.moduleName && scriptOption?.moduleName.some((m) => moduleInfo.name === m || moduleInfo?.alias === m) || scriptOption?.moduleName === void 0) {
113
+ let attempts = 0;
114
+ while (attempts - 1 < retryTimes) {
115
+ try {
116
+ moduleFactory = await remoteEntryExports.get(expose);
117
+ break;
118
+ } catch (error) {
119
+ attempts++;
120
+ if (attempts - 1 >= retryTimes) {
121
+ scriptOption?.cb && await new Promise((resolve) => scriptOption?.cb && scriptOption?.cb(resolve, error));
122
+ throw error;
123
+ }
124
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
202
125
  }
203
- } else {
204
- return scriptWithRetry({
205
- url,
206
- attrs: scriptAttrs,
207
- retryTimes: scriptOption?.retryTimes,
208
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
209
- });
210
126
  }
211
127
  }
212
- return {};
128
+ return moduleFactory;
213
129
  }
214
130
  }), "RetryPlugin");
215
131
  // Annotate the CommonJS export names for ESM import in node:
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/retry-plugin",
3
- "version": "0.6.14",
3
+ "version": "0.6.15",
4
4
  "author": "danpeen <dapeen.feng@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/retry-plugin",
3
- "version": "0.6.14",
3
+ "version": "0.6.15",
4
4
  "author": "danpeen <dapeen.feng@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -28,9 +28,9 @@
28
28
  }
29
29
  },
30
30
  "devDependencies": {
31
- "@module-federation/runtime": "0.6.14"
31
+ "@module-federation/runtime": "0.6.15"
32
32
  },
33
33
  "dependencies": {
34
- "@module-federation/sdk": "0.6.14"
34
+ "@module-federation/sdk": "0.6.15"
35
35
  }
36
36
  }