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

Lazyload forRoot #97

Open
raphael22 opened this issue Nov 8, 2024 · 12 comments
Open

Lazyload forRoot #97

raphael22 opened this issue Nov 8, 2024 · 12 comments

Comments

@raphael22
Copy link

Hello,
How a child module could initialize matomo?

I've got a module-federation architecture in which I want one of my module to use matomo.
But, as forbidden by guidelines, I can't use forRoot in my child module nor provide a root configuration.

I could inject matomo in my shell app, but that's mean my child module would be dependent on my shell deployment.

Bon weekend :)

@EmmanuelRoux
Copy link
Owner

Hi @raphael22

You typically should import the module using .forRoot in your root module, and import the Matomo module without .forRoot in child (lazy) modules.

If you really want to call .forRoot in your child module, it should still work though... :) (but Matomo will not be accessible in other modules)

If what you want to achieve is lazy-initialization of Matomo (but still use it in the whole app), take a look at how to defer initialization

@raphael22
Copy link
Author

Well, I tried few settings with defer but always result in a No Matomo configuration found! error :/

@EmmanuelRoux
Copy link
Owner

Could you please provide a repro to investigate ? Or share your config ?

@raphael22
Copy link
Author

raphael22 commented Nov 8, 2024

Will reproduce a basic module-federation later.
Config is either provideMatomo({ mode: 'deferred' }) or forRoot equivalent.
Maybe loadRemoteModule from "@angular-architects/module-federation-runtime" is not compatible with library as is.
PS: it was okay with ngx-matomo lib.

@EmmanuelRoux
Copy link
Owner

EmmanuelRoux commented Nov 9, 2024

If use Matomo only in one micro-front end (child) module, when you use .forRoot/provideMatomo, what is the error you are getting?

At least, share your config, otherwise it's difficult to understand the problem

@raphael22
Copy link
Author

No config found is the error I get if forRoot is coming from a lazyloaded MF module.
Angular 17.3.0
@angular-architects/module-federation 17.0.8
ngx-matomo-client 6.0.2
node 20

@EmmanuelRoux
Copy link
Owner

if forRoot is coming from a lazyloaded MF module

What do you precisely mean?

Can you share your config please? So we can investigate

@raphael22
Copy link
Author

repro incoming
I've got my master/shell module which lazyload a child/federation module.
My master module doesn't know matomo, only my child module does and try to provide it in root.
What config do you need?
It's really just a basic provideMatomo with siteId/url or with mode: deferred, I tried a bunch of settings always resulting in the no config error.

@raphael22
Copy link
Author

raphael22 commented Nov 12, 2024

Here a basic reproduction
You should be stuck on "loading..." after install/serve with error in browser console.
A module federation test component with it's ngModule and matomo is in angular-app/

@EmmanuelRoux
Copy link
Owner

@raphael22 Thank you for the repro, I understand the use case

It was not initially designed this way, but it seems a valid use case. I will take a look and probably publish a solution soon (PR welcome if you have some time !)

@raphael22
Copy link
Author

Not familiar with matomo.js but I could take a look if you have a branch to lead me a bit.

@EmmanuelRoux
Copy link
Owner

You can fork this project then create a PR.

In my mind, the required changes are to remove all {providedIn: ‘root’} on @Injectable annotations, and replace them by providers in existing provideXXX functions instead.
You can sure start working on that, and I will take a look soon, when I have some time. Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants