score:7

Accepted answer

In order to make it work you need to change the way you are loading remote entry.

  1. Update your ModuleFederationPlugin config in webpack.config.js for app1 to this:
...

new ModuleFederationPlugin({
    name: "app1",
    remoteType: 'var',
    remotes: {
      app2: 'app2',
    },
    shared: {
      ...packageJsonDeps,
      react: { singleton: true, eager: true, requiredVersion: packageJsonDeps.react },
      "react-dom": { singleton: true, eager: true, requiredVersion: packageJsonDeps["react-dom"] }
    },
}),

...
  1. Add script tag to the head of your index.html in app1 :
<script src="http://localhost:2002/remoteEntry.js"></script>

Good look with further hacking!

UPDATE:

Just for the sake of it: I've create a PR to your sandbox repo with fixes described above: https://github.com/vovkvlad/webpack-module-fedaration-sandbox/pull/2

score:0

I also faced with problems when using NextJS app as a container app. You know, we had to give eager loaded again and again.

I followed a different way from current techniques on the web. I used dynamic load technique for my remote library and it seems shared modules are not being fetched again and again now. They are being loaded only once. Also, I implemented full system as framework agnostic so you can use any framework as a remote app (angular, vue, react, svelte...). Also, I moved SSR logic to remote app part so now it can fully support SSR for any framework, too. It seems worked and I wanted to share my solution in here to help community. I wrote a medium blog post with example Github repo link. I told my approach with details.

You can find detailed post in here: https://medium.com/@metinarslanturkk/how-i-implemented-dynamic-loaded-framework-agnostic-microfrontend-app-with-nextjs-and-react-which-620ff3df4298

And this is the example repo link: https://github.com/MetinArslanturk/microfrontend-nextjs-ssr

Thanks, Metin

score:4

You can set the dependency as eager inside the advanced API of Module Federation, which doesn’t put the modules in an async chunk, but provides them synchronously. This allows us to use these shared modules in the initial chunk. But be careful as all provided and fallback modules will always be downloaded. It’s recommended to provide it only at one point of your application, e.g. the shell.

Webpack's website strongly recommend using an asynchronous boundary. It will split out the initialization code of a larger chunk to avoid any additional round trips and improve performance in general.

For example, your entry looked like this:

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));

Let's create bootstrap.js file and move contents of the entry into it, and import that bootstrap into the entry:

index.js

+ import('./bootstrap');
- import React from 'react';
- import ReactDOM from 'react-dom';
- import App from './App';
- ReactDOM.render(<App />, document.getElementById('root'));

bootstrap.js

+ import React from 'react';
+ import ReactDOM from 'react-dom';
+ import App from './App';
+ ReactDOM.render(<App />, document.getElementById('root'));

This method works but can have limitations or drawbacks.

Setting eager: true for dependency via the ModuleFederationPlugin

webpack.config.js

// ...
new ModuleFederationPlugin({
  shared: {
    ...deps,
    react: {
      eager: true,
    },
  },
});

Source

score:14

Just to make it clear for those who might miss the comment to the initial answer:

It seems like the main reason of why it failed initially was that remoteEntry.js file was loaded after the code that actually runs the host app.

Both bootstrap.js approach and adding direct script <script src="http://localhost:2002/remoteEntry.js"></script> to the <head></head> tag has exactly the same outcome - they make remoteEntry.js be loaded and parsed BEFORE the main app's code.

In case of bootstrap the order is next:

  1. main_bundle is loaded
  2. as the main code is extracted into bootstrap.js file - remoteEntry.js is loaded
  3. bootstrap.js is loaded which actually runs the main app

enter image description here

with proposed variant by Oleg Vodolazsky events order is next:

  1. remoteEntry.js is loaded first as it is directly added to html file and webpack's main_bundle is being appended to <head></head> after remoteEntry link
  2. main_bundle is loaded and runs the application

enter image description here

and in case of just trying to run app without bootstrap and without hardcoded script in the <head></head> main_bundle is loaded before remoteEntry.js and as main_bundle tries to actually run the app, it fails with an error:

enter image description here


Related Query

More Query from same tag