question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Module Federation (MFE) supports dynamic module loading like Angular already does

See original GitHub issue

Description

Would like to be able load remote React application via JSON call just like it is supported with the Angular modules. See your Advanced Angular Modules Frontend with Dynamic Module Federation article.

As far as I can tell, this ability is only supported in Angular.

Motivation

Some our application are deployed on site to a customers data centers and we also have many multi-tenant sites of our own, so we do not know the remote site URL at build time.

Our work around for now is to use URL rewrite rules so that all the remote sites end up in the same domain but this is not optimal.

Also, all of our products use React on the frontend se we would rather not have to switch to Angular (no offense to Angular) for our new stuff.

Suggested Implementation

Have an equivalent namespace as angular import { setRemoteDefinitions } from '@nrwl/react/mf';

Have similar functions for React as the Angular functions that load the modules at runtime.

import { setRemoteDefinitions } from '@nrwl/angular/mf // React equivalent import

fetch('/assets/module-federation.manifest.json')
  .then((res) => res.json())
  .then((definitions) => setRemoteDefinitions(definitions)) // <--sets the React modules
  .then(() => import('./bootstrap').catch((err) => console.error(err)));

Then lazy loading the module would still work the same as does today.

const Academics = React.lazy(() => import('ui-academics/Module'));

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:9
  • Comments:10

github_iconTop GitHub Comments

3reactions
wangel13commented, Aug 22, 2022
2reactions
LongLiveCHIEFcommented, Oct 4, 2022

I’ve done a lot of digging into source and docs on this, and under the hood, the whole dynamic remotes thing is framework agnostic, as it’s the webpack container that provides the functionality. (see: https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers)

Originally, dynamic remotes were achieved using webpack external-remotes-plugin. Which would allow url resolution of the container at runtime.

Since then, webpack has created functionality to allow dynamic resolution of remote webpack container urls using a promise, or asynchronously, as long as the resolved object has get and init interface described in the docs.

This is what the current angular withFederatedModules function does, and is what @wangel13’s script does (in addition to ensuring shared scope access).

From there, content container retrieval is a webpack thing, and the loaded javascript is just built/packed javascript

All this is to say, I’m starting to think that the existing @nrwl/angular/mffunctions for loading components and setting remote definitions could be put into a package like @nrwl/mf and used by either framework.vs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Angular Micro Frontends with Dynamic Module ...
We need to generate two applications that support Module Federation. We'll start with the Admin Dashboard application which will act as a host...
Read more >
Module Federation
This object is used as a shared scope in the remote container and is filled with the provided modules from a host. It...
Read more >
Multi-Framework and -Version Micro Frontends with ...
Module Federation makes it easy to load parts of other applications into a host. In our case, the host is the Micro Frontend...
Read more >
Code structure when dynamically load a micro frontend via ...
1 and https://www.angulararchitects.io/en/aktuelles/dynamic-module-federation-with-angular/ further referenced as Ex. 2. Ex. 1 has multiple ...
Read more >
Angular contains React using Webpack 5 Module Federation ...
In Micro-frontend implementation [part 2] — Nested React apps, the implementation is straightforward with both host and container apps are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found