-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): allow errorLoadRemote hook catch remote entry resource…
… loading error (#3474) Co-authored-by: kyli <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,519 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@module-federation/runtime-core': patch | ||
'website-new': patch | ||
--- | ||
|
||
feat: allow errorLoadRemote hook catch remote entry resource loading error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/router-demo/router-host-2000/src/runtime-plugin/error-handling/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Error Handling Strategies for Module Federation | ||
* | ||
* This module provides different strategies for handling remote module loading errors. | ||
* Choose the strategy that best fits your needs: | ||
* | ||
* 1. Lifecycle-based Strategy: | ||
* - Handles errors differently based on the lifecycle stage | ||
* - Provides backup service support for entry file errors | ||
* - More granular control over error handling | ||
* | ||
* 2. Simple Strategy: | ||
* - Single fallback component for all error types | ||
* - Consistent error presentation | ||
* - Minimal configuration required | ||
* | ||
* Example usage: | ||
* ```typescript | ||
* import { createLifecycleBasedPlugin, createSimplePlugin } from './error-handling'; | ||
* | ||
* // Use lifecycle-based strategy | ||
* const plugin1 = createLifecycleBasedPlugin({ | ||
* backupEntryUrl: 'http://backup-server/manifest.json', | ||
* errorMessage: 'Custom error message' | ||
* }); | ||
* | ||
* // Use simple strategy | ||
* const plugin2 = createSimplePlugin({ | ||
* errorMessage: 'Module failed to load' | ||
* }); | ||
* ``` | ||
*/ | ||
|
||
export * from './lifecycle-based'; | ||
export * from './simple'; |
82 changes: 82 additions & 0 deletions
82
apps/router-demo/router-host-2000/src/runtime-plugin/error-handling/lifecycle-based.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Lifecycle-based Error Handling Strategy | ||
* | ||
* This implementation demonstrates a more granular approach to error handling | ||
* by responding differently based on the lifecycle stage where the error occurred. | ||
* | ||
* Two main stages are handled: | ||
* 1. Component Loading (onLoad): Provides a UI fallback for component rendering failures | ||
* 2. Entry File Loading (afterResolve): Attempts to load from a backup service | ||
*/ | ||
|
||
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; | ||
|
||
interface LifecycleBasedConfig { | ||
backupEntryUrl?: string; | ||
errorMessage?: string; | ||
} | ||
|
||
export const createLifecycleBasedPlugin = ( | ||
config: LifecycleBasedConfig = {}, | ||
): FederationRuntimePlugin => { | ||
const { | ||
backupEntryUrl = 'http://localhost:2002/mf-manifest.json', | ||
errorMessage = 'Module loading failed, please try again later', | ||
} = config; | ||
|
||
return { | ||
name: 'lifecycle-based-fallback-plugin', | ||
async errorLoadRemote(args) { | ||
// Handle component loading errors | ||
if (args.lifecycle === 'onLoad') { | ||
const React = await import('react'); | ||
|
||
// Create a fallback component with error message | ||
const FallbackComponent = React.memo(() => { | ||
return React.createElement( | ||
'div', | ||
{ | ||
style: { | ||
padding: '16px', | ||
border: '1px solid #ffa39e', | ||
borderRadius: '4px', | ||
backgroundColor: '#fff1f0', | ||
color: '#cf1322', | ||
}, | ||
}, | ||
errorMessage, | ||
); | ||
}); | ||
|
||
FallbackComponent.displayName = 'ErrorFallbackComponent'; | ||
|
||
return () => ({ | ||
__esModule: true, | ||
default: FallbackComponent, | ||
}); | ||
} | ||
|
||
// Handle entry file loading errors | ||
if (args.lifecycle === 'afterResolve') { | ||
try { | ||
// Try to load backup service | ||
const response = await fetch(backupEntryUrl); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to fetch backup entry: ${response.statusText}`, | ||
); | ||
} | ||
const backupManifest = await response.json(); | ||
console.info('Successfully loaded backup manifest'); | ||
return backupManifest; | ||
} catch (error) { | ||
console.error('Failed to load backup manifest:', error); | ||
// If backup service also fails, return original error | ||
return args; | ||
} | ||
} | ||
|
||
return args; | ||
}, | ||
}; | ||
}; |
57 changes: 57 additions & 0 deletions
57
apps/router-demo/router-host-2000/src/runtime-plugin/error-handling/simple.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Simple Error Handling Strategy | ||
* | ||
* This implementation provides a straightforward approach to error handling | ||
* by using a single fallback component for all types of errors. | ||
* | ||
* Benefits: | ||
* - Simple to understand and implement | ||
* - Consistent error presentation | ||
* - Requires minimal configuration | ||
* | ||
* Use this when you don't need different handling strategies for different error types. | ||
*/ | ||
|
||
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; | ||
|
||
interface SimpleConfig { | ||
errorMessage?: string; | ||
} | ||
|
||
export const createSimplePlugin = ( | ||
config: SimpleConfig = {}, | ||
): FederationRuntimePlugin => { | ||
const { errorMessage = 'Module loading failed, please try again later' } = | ||
config; | ||
|
||
return { | ||
name: 'simple-fallback-plugin', | ||
async errorLoadRemote() { | ||
const React = await import('react'); | ||
|
||
// Create a fallback component with error message | ||
const FallbackComponent = React.memo(() => { | ||
return React.createElement( | ||
'div', | ||
{ | ||
style: { | ||
padding: '16px', | ||
border: '1px solid #ffa39e', | ||
borderRadius: '4px', | ||
backgroundColor: '#fff1f0', | ||
color: '#cf1322', | ||
}, | ||
}, | ||
errorMessage, | ||
); | ||
}); | ||
|
||
FallbackComponent.displayName = 'ErrorFallbackComponent'; | ||
|
||
return () => ({ | ||
__esModule: true, | ||
default: FallbackComponent, | ||
}); | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.