angular: Destroying lazy loaded modules doesn't unload them
I’m submitting a…
[x] Feature request
Current behavior
Currently the lazy loaded module sets itself to the destroyed state. When we revisit the lazy route, it still loads the already-destroyed module. It doesn’t unset itself.
Destroying it for a second time results in:
'Error: The ng module LazyModule has already been destroyed.'
Expected behavior
I expect lazy modules to unload themselves once destroyed, re-initializing themselves when they’re needed again.
What is the motivation / use case for changing the behavior?
Our application has to manage multiple users. Our services build up a toxic cache of data that belongs to user Alice. When we switch to user Bob, we call destroy() on the lazy loaded module and reload the module for Bob.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 36
- Comments: 36 (9 by maintainers)
Commits related to this issue
- fix(router): lazy loaded route will not load a destroyed module instance, creates new one instead (#24962) — committed to kewde/angular by kewde 6 years ago
- fix(router): lazy loaded route will not load a destroyed module instance, creates new one instead (#24962) — committed to kewde/angular by kewde 6 years ago
- fix(router): lazy loaded route will not load a destroyed module instance, creates new one instead (#24962) — committed to kewde/angular by kewde 6 years ago
- fix: lazy loaded route will not load a destroyed module instance, creates new one instead (#24962) — committed to kewde/angular by kewde 6 years ago
- fix: lazy loaded route will not load a destroyed module instance, creates new one instead (#24962) — committed to kewde/angular by kewde 6 years ago
@BenDevelopment
Route to a location outside of the lazy module before destroying it.
Ok - so if the user is logging out, after the session is cleared, why can’t you force the window to reload? Doesn’t that solve the problem? setTimeout(()={window.location.href=restartLocation;},1)
On Thu, Jun 10, 2021 at 3:44 AM Eddy @.***> wrote:
@mlc-mlapis As @kewde explained here https://github.com/angular/angular/issues/24962#issuecomment-406380151, yes there is a destroy() method which destroys the Injector (and call the methods ngOnDestroy on all services inside this injector) + call the registered onDestroy callbacks on the NgModuleRef itself.
But the ngModule instance (or NgModuleRef) itself is not cleared as there is still a reference to it inside the router inside a LoadedRouterConfig as the module attribute => https://github.com/angular/angular/blob/dfb072a93c2b49a02f1c36bf3e5f9547f26af29d/packages/router/src/config.ts#L501
This attribute is never cleared and therefor the NgModuleRef is never gc’ed.
On the other hand, the NgComponentOutlet does clear the reference to the NgModule instance : https://github.com/angular/angular/blob/dfb072a93c2b49a02f1c36bf3e5f9547f26af29d/packages/common/src/directives/ng_component_outlet.ts#L97
@kewde can you explain me how to call destroy() on a lazy loaded module ? I cannot find any documentation about that.