Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The remote module is missing on the Window when running in the sandbox of the micro-frontend framework micro-app. #2663

Closed
5 tasks done
zhangxilong-43 opened this issue Jun 25, 2024 · 6 comments

Comments

@zhangxilong-43
Copy link

zhangxilong-43 commented Jun 25, 2024

Describe the bug

I load sub-application app_1 in a container based on the micro-frontend framework micro-app, and app_1 uses module federation to obtain the remote module of sub-application app_2.
Both sub-applications app_1 and app_2 can run independently, and module federation is effective;
When app_2 is loaded in the container, it can also run normally;
But after loading app_1 in the container, app_1 will report an error where it uses the remote module.

from react.development.js:1409's error:
Uncaught TypeError: Cannot read properties of undefined (reading 'get')
while loading "./Page" from webpack/container/reference/app_2

This error from app_2's webpack.config.js;
I don't understand why window.app_2 doesn't exist
The following is its module federation configuration:

new ModuleFederationPlugin({ name: 'app_1', filename: 'remoteEntry.js', remotes: { // 为什么要这样写?详见: https://github.com/module-federation/module-federation-examples/issues/1142 app_2:promise new Promise(resolve => {
const remoteUrl = 'http://127.0.0.1:3002/remoteEntry.js'
const script = document.createElement('script')
script.src = remoteUrl
script.onload = () => {
console.log(window, window.rawWindow, window.app_2, 'app_2')
const proxy = {
get: (request) => window.app_2.get(request),
init: (arg) => {
try {
return window.app_2.init(arg)
} catch(e) {
console.log('remote container already initialized')
}
}
}
resolve(proxy)
}
document.head.appendChild(script);
})
}, shared: { ...deps, 'react-router-dom': { singleton: true, }, 'react-dom': { singleton: true, }, react: { singleton: true, }, }, }),

Reproduction

https://github.com/zhangxilong-43/module_federation_case/tree/master

Used Package Manager

pnpm

System Info

macOS

Validations

@ScriptedAlchemy
Copy link
Member

use a runtime plugin instead of promise new promise.

    app_2: `app_2@http://127.0.0.1:3002/remoteEntry.js`

https://module-federation.io/plugin/dev/index.html

You can also set the library type if the default var is unreliable.

library: {
type: 'window',
name: 'app_2'
},

@ScriptedAlchemy
Copy link
Member

use the beforeRequest hook, you can look in module federation examples repo at runtime-plugin folder

@zhangxilong-43
Copy link
Author

zhangxilong-43 commented Jun 29, 2024

use the beforeRequest hook, you can look in module federation examples repo at runtime-plugin folder

THX.
However, the following error occurred:
`
Error: [ Federation Runtime ]:
Unable to use the app_2's 'http://127.0.0.1:3002/remoteEntry.js' URL with app_2's globalName to get remoteEntry exports.
Possible reasons could be:

  1. 'http://127.0.0.1:3002/remoteEntry.js' is not the correct URL, or the remoteEntry resource or name is incorrect.

  2. app_2 cannot be used to get remoteEntry exports in the window object.

`

So, how should I modify the configuration when the module federation throws such an error?

@ScriptedAlchemy
Copy link
Member

Update the repo please so i can see

@zhangxilong-43
Copy link
Author

Update the repo please so i can see

Sorry,I have updated the code, you can see it now。

Copy link
Contributor

Stale issue message

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants