Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular module federation singleton service initiated multiple times

I was following this tutorial , and tried to share a library between the shell and the mfe1 app. I created the lib outside the workspace directory with one service injected in root, then added it to the tutorial package.json, and imported it in both shell and mfe1. Inside both projects webpack config, i added the following configuration

new ModuleFederationPlugin({
      .....
        },        
        shared: {
          .....
          "my-lib": { singleton: true, strictVersion: true, requiredVersion: '1.0.0'},
          ...sharedMappings.getDescriptors()
        }
        
    }),

When testing the shell, i noticed that the singleton service is being initiated twice, first time when loading the shell, second time when moving to the mfe1, maybe i'm missing something, but isn't the purpose of singleton: true config is to insure that only one instance of that service is existing?

like image 876
Hekek Avatar asked Jul 01 '21 07:07

Hekek


People also ask

How to make a service a singleton in angular?

There are two ways to make a service a singleton in Angular: Set the providedIn property of the @ Injectable () to "root". Include the service in the AppModule or in a module that is only imported by the AppModule

How to create a singleton service using ngmodule providers array?

In the case of NgModule providers array, a singleton service is created by passing the service as a value to the providers array and if the NgModule is root app module then the service will be available throughout the application as a singleton service. Below is the syntax of how to pass angular service to providers array.

Why is module Federation loading angular twice for my Micro frontend?

If we load a Micro Frontend that uses a different Angular version, Module Federation falls back to loading Angular twice, once the version for the shell and once the version for the Micro Frontend. You can try this out by updating the shell’s app.routes.ts as follows:

What is module Federation in angular?

The Angular Architects package @angular-architects/module-federation creates a simple API to request modules and pull them into your application. This will install the necessary dependency, with the schematics needed to add remote apps to be consumed by module federation. Shell is your consuming application.


Video Answer


1 Answers

With the federation module plugin when the singleton property is set to true on a shared library it means that the lib is only loaded once even if it is required by multiple mfes. The strictVersion: true will cause an error if your mfes or shell require different version of the library.

This is not related to the angular sevices, even if the lib is only loaded once, it doesn't mean that its services will be singletons.

If you want a service to be a singleton accross multiple modules you should make sure that you provide the service in the root injector by adding :

@Injectable({
  providedIn: 'root'
})

Or by making sure that the service is declared as a provider of the shell app module, and is not declared as a provider in any other modules.

like image 179
J.Loscos Avatar answered Oct 27 '22 23:10

J.Loscos