Skip to content

Commit

Permalink
chore: add errorLoadRemote example in plugin hook
Browse files Browse the repository at this point in the history
  • Loading branch information
danpeen committed Feb 2, 2025
1 parent 6e9c447 commit 2cfbc8c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
14 changes: 7 additions & 7 deletions apps/router-demo/router-host-2000/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ export default defineConfig({
'react-dom': {
singleton: true,
},
// 'react-router-dom': {
// // singleton: true,
// requiredVersion: '6.x',
// },
'react-router-dom': {
// singleton: true,
requiredVersion: '6.x',
},
},
runtimePlugins: [
path.join(__dirname, './src/runtime-plugin/shared-strategy.ts'),
path.join(__dirname, './src/runtime-plugin/retry.ts'),
path.join(__dirname, './src/runtime-plugin/fallback.ts'),
],
// bridge: {
// disableAlias: true,
// },
bridge: {
disableAlias: true,
},
}),
],
});
35 changes: 23 additions & 12 deletions apps/router-demo/router-host-2000/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@ import Home from './pages/Home';
import './App.css';
import BridgeReactPlugin from '@module-federation/bridge-react/plugin';
import { ErrorBoundary } from 'react-error-boundary';
import Remote1AppNew from 'remote1/app';

const fallbackPlugin: () => FederationRuntimePlugin = function () {
return {
name: 'fallback-plugin',
errorLoadRemote(args) {
return { default: () => <div> fallback component </div> };
},
};
};

init({
name: 'federation_consumer',
remotes: [],
remotes: [
{
name: 'remote1',
alias: 'remote1',
entry: 'http://localhost:2001/mf-manifest.json',
},
],
plugins: [
BridgeReactPlugin(),
// RetryPlugin({
Expand All @@ -36,6 +52,7 @@ init({
// },
// },
// }),
fallbackPlugin(),
],
});

Expand Down Expand Up @@ -63,9 +80,7 @@ const Remote1App = createRemoteComponent({
const Remote1AppWithLoadRemote = React.lazy(() => loadRemote('remote1/app'));
const Remote1AppWithErrorBoundary = React.forwardRef<any, any>((props, ref) => (
<ErrorBoundary fallback={<div>Error loading Remote1App...</div>}>
<Suspense
fallback={<div style={{ color: 'red' }}> Loading Remote1App...</div>}
>
<Suspense fallback={<div> Loading Remote1App...</div>}>
<Remote1AppWithLoadRemote {...props} ref={ref} />
</Suspense>
</ErrorBoundary>
Expand Down Expand Up @@ -102,12 +117,12 @@ function Wraper3() {
<div className="flex flex-row">
<div className="grow">
<h2>Remote1</h2>
<Remote1App name={'Ming'} age={12} memoryRoute={{ entryPath: '/' }} />
{/* <Remote1AppWithErrorBoundary
{/* <Remote1App name={'Ming'} age={12} memoryRoute={{ entryPath: '/' }} /> */}
<Remote1AppWithErrorBoundary
name={'Ming'}
age={12}
memoryRoute={{ entryPath: '/' }}
/> */}
/>
</div>
<div className="grow">
<h2>Remote2</h2>
Expand Down Expand Up @@ -156,11 +171,7 @@ const App = () => {
// ref={ref}
// basename="/remote1"
// />
<Remote1App
name={'Ming'}
age={12}
memoryRoute={{ entryPath: '/' }}
/>
<Remote1AppNew name={'Ming'} age={12} />
)}
/>
<Route
Expand Down
4 changes: 2 additions & 2 deletions apps/router-demo/router-host-2000/src/pages/Detail.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadRemote } from '@module-federation/enhanced/runtime';
import Button from 'remote1/button';
// import Button from 'remote1/button';

// console.log('loadRemote', loadRemote);
// loadRemote("remote1/button").then((m) => {
Expand All @@ -11,7 +11,7 @@ function Detail() {
<div>
Detail
<h5>Remote Button</h5>
<Button />
{/* <Button /> */}
</div>
);
}
Expand Down
19 changes: 15 additions & 4 deletions apps/router-demo/router-host-2000/src/runtime-plugin/fallback.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
import type {
FederationRuntimePlugin,
Manifest,
} from '@module-federation/runtime';
// you can alse import fallback constant remote entry fallback data
import { manifestJson } from './remote-manifest-data';

const fallbackPlugin: () => FederationRuntimePlugin = function () {
const fallbackPlugin = (): FederationRuntimePlugin => {
return {
name: 'fallback-plugin',
async errorLoadRemote(args) {
console.log('============ errorLoadRemote ============', args);
if (args.lifecycle === 'onLoad') {
const fallback = 'fallback';
return fallback;
const React = await import('react');
const FallbackComponent = React.memo(() =>
React.createElement('div', null, 'fallback component'),
);
FallbackComponent.displayName = 'FallbackComponent';
return () => ({
__esModule: true,
default: FallbackComponent,
});
} else if (args.lifecycle === 'afterResolve') {
// you can try another fall back remote entry
// const response = await fetch('http://localhost:2002/mf-manifest.json');
// const manifestJson = (await response.json()) as Manifest;
return manifestJson;
}
return args;
},
};
};
Expand Down
14 changes: 7 additions & 7 deletions apps/router-demo/router-remote1-2001/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ export default defineConfig({
'react-dom': {
singleton: true,
},
// 'react-router-dom': {
// // singleton: true,
// requiredVersion: '5.x',
// },
'react-router-dom': {
// singleton: true,
requiredVersion: '5.x',
},
antd: {
singleton: true,
},
},
// bridge: {
// disableAlias: true,
// },
bridge: {
disableAlias: true,
},
}),
],
});
4 changes: 0 additions & 4 deletions apps/website-new/docs/en/practice/bridge/vue-bridge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,7 @@ export default createBridgeComponent({
appOptions: ({app}) => {
// Optional: adding a plugin to the new Vue instance on the host application side
app.use(customPlugin)
<<<<<<< HEAD
return {router}
=======
return { router };
>>>>>>> main
},
});
```

0 comments on commit 2cfbc8c

Please sign in to comment.