Closed oytuncoban closed 3 weeks ago
You can use a runtime plugin to handle such cases, since the v1 way or catching errors was unreliable and did not work for import from ''
The runtime plugin doesn't catch this kind of error, I'm stuck on version 0.1.6 until it get fixed 😢
You can use a runtime plugin to handle such cases, since the v1 way or catching errors was unreliable and did not work for import from ''
Couldn't try it yet due to the recent workload of my own. Gonna update as soon as I can.
The runtime plugin doesn't catch this kind of error, I'm stuck on version 0.1.6 until it get fixed 😢
should be fixed now, this was a CORS error
@ScriptedAlchemy, the error persists, I created a repo example: https://github.com/danieloprado/mf-offline
When a shared is set, an uncaughtable error is thrown.
also interested in fix for this issue.
@2heal1 this seems like a legitimate issue in the runtime.
https://github.com/danieloprado/mf-offline
Any ideas on how we can patch it so it doesn't fail
@2heal1 this seems like a legitimate issue in the runtime.
https://github.com/danieloprado/mf-offline
Any ideas on how we can patch it so it doesn't fail
The error happens because the default shared strategy is version first which means it will fetch remote entry first and then sync the sharedScope . But in this case , the remote is offline , so the request failed .
It can be solved by change the default shared strategy without any source code change:
shared-strategy.ts
file in the project root
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
const sharedStrategy: () => FederationRuntimePlugin = () => ({ name: 'shared-strategy-plugin', beforeInit(args) { const { userOptions } = args; const shared = userOptions.shared; if (shared) { Object.keys(shared).forEach((sharedKey) => { const sharedConfigs = shared[sharedKey]; const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [sharedConfigs]; arraySharedConfigs.forEach((s) => { s.strategy = 'loaded-first'; }); }); } return args; }, }); export default sharedStrategy;
2. apply the runtime plugin
```diff
// rsbuild.config.ts
import path from 'path';
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
export default defineConfig({
server: { port: 3000 },
dev: { assetPrefix: 'http://localhost:3000' },
plugins: [pluginReact()],
html: {
title: 'MFE Offline Test'
},
tools: {
rspack: {
plugins: [
new ModuleFederationPlugin({
name: 'test_host',
remotes: {
'app_offline': 'app_offline@http://localhost:3001/manifest.json'
},
+ runtimePlugins:[path.resolve(__dirname,'shared-strategy.ts')],
shared: ['react'] // comment and it will work
})
]
}
}
});
And to prevent this issue , i think we should add sharedStrategy
config in build plugin , and also make it as default strategy.
@2heal1 this seems like a legitimate issue in the runtime. https://github.com/danieloprado/mf-offline Any ideas on how we can patch it so it doesn't fail
The error happens because the default shared strategy is version first which means it will fetch remote entry first and then sync the sharedScope . But in this case , the remote is offline , so the request failed .
It can be solved by change the default shared strategy without any source code change:
- add
shared-strategy.ts
file in the project rootimport type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; const sharedStrategy: () => FederationRuntimePlugin = () => ({ name: 'shared-strategy-plugin', beforeInit(args) { const { userOptions } = args; const shared = userOptions.shared; if (shared) { Object.keys(shared).forEach((sharedKey) => { const sharedConfigs = shared[sharedKey]; const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [sharedConfigs]; arraySharedConfigs.forEach((s) => { s.strategy = 'loaded-first'; }); }); } return args; }, }); export default sharedStrategy;
- apply the runtime plugin
// rsbuild.config.ts import path from 'path'; import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack'; import { defineConfig } from '@rsbuild/core'; import { pluginReact } from '@rsbuild/plugin-react'; export default defineConfig({ server: { port: 3000 }, dev: { assetPrefix: 'http://localhost:3000' }, plugins: [pluginReact()], html: { title: 'MFE Offline Test' }, tools: { rspack: { plugins: [ new ModuleFederationPlugin({ name: 'test_host', remotes: { 'app_offline': 'app_offline@http://localhost:3001/manifest.json' }, + runtimePlugins:[path.resolve(__dirname,'shared-strategy.ts')], shared: ['react'] // comment and it will work }) ] } } });
And to prevent this issue , i think we should add
sharedStrategy
config in build plugin , and also make it as default strategy.
yes with this one issue solved .
And to prevent this issue , i think we should add sharedStrategy config in build plugin , and also make it as default strategy.
i personally think that its a good idea .
Setting this a default strategy would be a nice DX boost
Glad to stumble upon this solution as we were just noticing similar problems with our own Error Fallbacks not working.
Question for @2heal1 about the implications of this strategy: does loaded-first
mean that the shared package versions requested by the top level host are guaranteed to be the chosen versions because the host is always going to be loaded first?
Or is the loaded-first
resolution strategy when dealing with shared packages requested by the host less deterministic than even that?
For those who are using rspack v1, and getting this warning on the console
[ Federation Runtime ]: "shared.strategy is deprecated, please set in initOptions.shareStrategy instead!"
To solve this, you can just use this code
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
const sharedStrategy: () => FederationRuntimePlugin = () => ({
name: 'shared-strategy-plugin',
beforeInit(args) {
args.userOptions.shareStrategy = 'loaded-first';
return args;
},
});
export default sharedStrategy;
As the shared-strategy is mentioned as solution in several Issues, I will move the solution from @2heal1 to the Error catalog section of the docs for more transpareny
Amazing, thank you!
Docs updated, issue can be closed
Describe the bug
Hey, I was using the old Module Federation ('webpack/lib/container/ModuleFederationPlugin'), and decided to migrate to the new Module Federation 2.0.
However, in the previous MF version, I had to implement some ErrorBoundary components:
ErrorBoundary:
DynamicImport:
Usage of DynamicImport:
With this method, I was able to show Error UI when one of the remote is failing and has errors, or unavailable.
With Module Federation 2.0 I now get this error thrown by @module-federation/enhanced:
The versions of used packages:
webpack
:^5.57.1
,@module-federation/enhanced
:^0.2.1
,Example remotes object that I use:
Issue is that, webpack throws error and I cannot use my website as expected. Main goal is to keep Host(Shell) app to be continue working even if a remote fails. Users should be able to use other remotes and shell app layout.
I tried to implement the Dynamic System Host example. Here again, if I don't start one of the remotes, when user tries to load the failing remote, it immediately throws error and breaks the shell.
Wouldn't it be convenient that we can catch the Remote load errors and provide a fallback UI to the user?
Reproduction
https://github.com/module-federation/module-federation-examples/tree/master/dynamic-system-host
Used Package Manager
npm
System Info
Validations