score:7
In order to make it work you need to change the way you are loading remote entry.
- Update your
ModuleFederationPlugin
config inwebpack.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"] }
},
}),
...
- Add
script
tag to thehead
of yourindex.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,
},
},
});
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:
main_bundle
is loaded- as the main code is extracted into
bootstrap.js
file -remoteEntry.js
is loaded bootstrap.js
is loaded which actually runs the main app
with proposed variant by Oleg Vodolazsky events order is next:
remoteEntry.js
is loaded first as it is directly added tohtml
file and webpack'smain_bundle
is being appended to<head></head>
after remoteEntry linkmain_bundle
is loaded and runs the application
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:
Source: stackoverflow.com
Related Query
- Webpack module federation is not working with eager shared libs
- Webpack 5 module federation - hooks in remote module - not working
- Webpack Hot Module Reloader not working with React Stateless Component
- Webpack 5 Module federation micro-frontend and react-redux is not working
- Uncaught Error: Shared module is not available for eager consumption in React module federation
- React Module Federation - Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react
- Tree shaking of shared dependencies in webpack 5 module federation
- import syntax not working with webpack
- How to use Webpack Module Federation plugin with create-react-app without ejecting
- Webpack Dev Server (webpack-dev-server) Hot Module Replacement (HMR) Not Working
- Bootstrap datepicker not working with Webpack
- webpack and react jsx - hot loading not working with docker container
- plugin is not working in Webpack with React JS
- React hot reload not working with webpack 4
- MUI5 not working with jest - SyntaxError: Cannot use import statement outside a module
- Importing React as default module not working inside Symfony Webpack Encore project
- Webpack 4 code splitting not working with React.lazy
- React built with Webpack 5 typescript compiled with problems in browser. @tailwind directive not working postcss-loader postcss.config.js Emit skipped
- Adding style to active link with ActiveClassName and CSS Module in Gatsby is not working
- HTML-Loader is not working with Webpack 4 (appropriate loader)
- Resolving relative paths in React with Webpack not working
- why proxy not working when used with webpack
- Nested classes not working in CSS Modules with webpack
- React router is not working with webpack server and redux?
- react router not working with webpack dora?
- Webpack 4 hot reload not working with React TypeScript
- The Babel-core module not working with Lingui
- React not working when bundled with Webpack
- Webpack 2 with historyApiFallback Not Working
- nginx sub_filter module not working with REACT websites
More Query from same tag
- useSelector hook for functions?
- how can i make a get request in axios to get the info of a user by clicking on his name using react.js
- How to style from an array of objects with using material-ui
- React parent property function not firing when called from child function
- Enzyme and Google map CDN
- I need to access a new window which pops after a button is clicked
- How to get focus on Material UI Link(without using component="button") using keyboard tab key
- Transformed content makes html stutter reaching the end of page
- How to add live camera scan with "react-qr-reader" in React App?
- JSS - How to style a nested child?
- Using props in React with styled components across components
- Converting react from es5 to es6
- Updating state from recursively rendered component in React.js
- How to recall getServerSideProps from same component with updated api url?
- Using React.lazy with TypeScript
- react-router inconsistent `exact` behaviour in `matchPath()` function
- Submit form(ReactJS) to Google Spreadsheet - change the message validation
- Material UI Autocomplete custom renderInput
- NextJS render Link with createElement
- How to use Highcharts in typescript and react
- Use typescrip iterfaces in react js application
- TypeError: Cannot read property 'map' of undefined, How do i rectify this?
- React: Understanding import statement
- How to do this Redux Thunk action creator in my special case with a React.createContext wrapper
- How to map a relay (graphql) connection to an edge's nodes using generics with Typescript
- How to import context from a file which is 2-steps up
- Next.js Image component Tailwind-css not working
- Concatenate strings within array of objects
- Autocomplete html tags in jsx (sublime text)
- Update node in react-sortable-tree