Skip to content

Commit a05cfcb

Browse files
docs: document errorLoadRemote lifecycle calls (#2417)
Co-authored-by: ScriptedAlchemy <[email protected]> Co-authored-by: Zhou xiao <[email protected]>
1 parent 5a34b5e commit a05cfcb

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

apps/website-new/docs/en/plugin/dev/index.mdx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ type HandlePreloadModuleOptions = {
270270

271271
`AsyncHook`
272272

273-
Called if loading a federated module fails, allowing for custom error handling.
273+
This hook is invoked when the loading of a federated module fails, enabling customized error handling strategies.
274+
275+
It is designed to be triggered during various lifecycle stages of module loading in the event that any of the stages fail.
276+
277+
Utilize `args.lifecycle` to identify the specific lifecycle stage that has initiated the call to `errorLoadRemote`, allowing for appropriate error handling or fallback mechanisms.
278+
274279

275280
- Type
276281

@@ -282,7 +287,9 @@ async function errorLoadRemote(
282287
type ErrorLoadRemoteOptions = {
283288
id: string;
284289
error: unknown;
290+
options?: any;
285291
from: 'build' | 'runtime';
292+
lifecycle: 'onLoad' | 'beforeRequest';
286293
origin: FederationHost;
287294
};
288295
```
@@ -298,9 +305,13 @@ const fallbackPlugin: () => FederationRuntimePlugin = function () {
298305
return {
299306
name: 'fallback-plugin',
300307
errorLoadRemote(args) {
301-
const fallback = 'fallback';
302-
return fallback;
303-
},
308+
if(args.lifecycle === 'onLoad') {
309+
const fallback = 'fallback';
310+
return fallback;
311+
} else if (args.lifecycle === 'beforeRequest') {
312+
return args
313+
}
314+
}
304315
};
305316
};
306317

@@ -310,10 +321,10 @@ init({
310321
{
311322
name: '@demo/app2',
312323
entry: 'http://localhost:3006/remoteEntry.js',
313-
alias: 'app2',
314-
},
324+
alias: 'app2'
325+
}
315326
],
316-
plugins: [fallbackPlugin()],
327+
plugins: [fallbackPlugin()]
317328
});
318329

319330
loadRemote('app2/un-existed-module').then((mod) => {

apps/website-new/docs/zh/plugin/dev/index.mdx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,33 @@ type HandlePreloadModuleOptions = {
271271

272272
`AsyncHook`
273273

274-
如果加载联合模块失败,则调用,允许自定义错误处理。
274+
当联合模块加载失败时,此钩子将被触发,允许自定义错误处理策略。
275+
276+
它被设计为在模块加载的各个生命周期阶段失败时触发。
277+
278+
利用 `args.lifecycle` 来识别调用 `errorLoadRemote` 的具体生命周期阶段,从而实现适当的错误处理或回退机制。
279+
275280

276281
- 类型
277282

278-
```ts
283+
```typescript
279284
async function errorLoadRemote(
280285
args: ErrorLoadRemoteOptions,
281286
): Promise<void | unknown>;
282287

283288
type ErrorLoadRemoteOptions = {
284289
id: string;
285290
error: unknown;
291+
options?: any;
286292
from: 'build' | 'runtime';
293+
lifecycle: 'onLoad' | 'beforeRequest';
287294
origin: FederationHost;
288295
};
289296
```
290297

291298
- 示例
292299

293-
```ts
300+
```typescript
294301
import { init, loadRemote } from '@module-federation/enhanced/runtime';
295302

296303
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
@@ -299,9 +306,13 @@ const fallbackPlugin: () => FederationRuntimePlugin = function () {
299306
return {
300307
name: 'fallback-plugin',
301308
errorLoadRemote(args) {
302-
const fallback = 'fallback';
303-
return fallback;
304-
},
309+
if(args.lifecycle === 'onLoad') {
310+
const fallback = 'fallback';
311+
return fallback;
312+
} else if (args.lifecycle === 'beforeRequest') {
313+
return args
314+
}
315+
}
305316
};
306317
};
307318

@@ -311,17 +322,16 @@ init({
311322
{
312323
name: '@demo/app2',
313324
entry: 'http://localhost:3006/remoteEntry.js',
314-
alias: 'app2',
315-
},
325+
alias: 'app2'
326+
}
316327
],
317-
plugins: [fallbackPlugin()],
328+
plugins: [fallbackPlugin()]
318329
});
319330

320331
loadRemote('app2/un-existed-module').then((mod) => {
321332
expect(mod).toEqual('fallback');
322333
});
323334
```
324-
325335
### beforeLoadShare
326336

327337
`AsyncWaterfallHook`

0 commit comments

Comments
 (0)