[UPDATE: Ok. We found out how to reproduce, see minimal reproduction]
We see in our server logs many of these 2 FATAL Errors on production and on non-production environments:
We clearly have imported both ngxs modules in AppModule and in the Feature Modules. In App module:
NgxsModule.forRoot(
[
xxx....
],
{
developmentMode: !environment.production,
},
),
NgxsRouterPluginModule.forRoot(),
NgxsFormPluginModule.forRoot(),
NgxsSelectSnapshotModule.forRoot(),
NgxsStoragePluginModule.forRoot({
...xxx
},
}),
NgxsReduxDevtoolsPluginModule.forRoot({
name: 'xxxx',
disabled: environment.production,
}),
in Feature Module:
NgxsModule.forFeature([xxx, yyy]),
NgxsSelectSnapshotModule,
It is a very huge project with many modules and components... And the errors are only happening via SSR, and there it happens rarely on our dev machines... When it does, though, we see a LOT of Error statements successively while no other component or module is complaing with an error. It is just ngxs. It seems, that SSR still deliveres the html, and the Angular app loads still well on the client (including all ngxs stuff)
Ok. We found out how to reproduce:
Guess: This might be caused by angular destroying all stuff and both ngxs modules will clear the Injector onDestroy. As the errors happens ONLY when we reload quickly after aborting (just aborting never creates the errors), it must be sth about angular universal still (re-?) using some components while ngxs already got rid of its injector reference.
FATAL [main.js:2897] Error: You have forgotten to import the NGXS module!
at createSelectObservable (/opt/app/dist/server/main.js:1:xx)
at AppComponent.get [as progress] (/opt/app/dist/server/main.js:1:xx)
at AppComponent_Template (/opt/app/dist/server/main.js:2897:1625353)
at executeTemplate (/opt/app/dist/server/main.js:2897:xx)
at refreshView (/opt/app/dist/server/main.js:2897:xx)
at refreshComponent (/opt/app/dist/server/main.js:2897:xx)
at refreshChildComponents (/opt/app/dist/server/main.js:2897:xx)
at refreshView (/opt/app/dist/server/main.js:2897:xx)
at renderComponentOrTemplate (/opt/app/dist/server/main.js:2897:xx)
at tickRootContext (/opt/app/dist/server/main.js:2897:xx)
or....
FATAL [main.js:2897] NgxsSelectSnapshotModuleIsNotImported [Error]: You've forgotten to import "NgxsSelectSnapshotModule"!
at assertDefined (/opt/app/dist/server/main.js:2897:xx)
at getStore (/opt/app/dist/server/main.js:2897:xx)
at XXXComponent.get [as isXXX] (/opt/app/dist/server/main.js:2897:xx)
at XXXComponent_Template (/opt/app/dist/server/main.js:2897:xx)
at executeTemplate (/opt/app/dist/server/main.js:2897:xx)
at refreshView (/opt/app/dist/server/main.js:2897:xx)
at refreshComponent (/opt/app/dist/server/main.js:2897:xx)
at refreshChildComponents (/opt/app/dist/server/main.js:2897:xx)
at refreshView (/opt/app/dist/server/main.js:2897:xx)
at refreshComponent (/opt/app/dist/server/main.js:2897:xx)
Libs:
"@angular/core": "^11.0.6",
"@ngxs/store": "^3.7.1",
"@ngxs-labs/select-snapshot": "^2.0.1",
Browser: only on SSR
The root cause of this issue is this file: https://github.com/ngxs/store/blob/master/packages/store/src/decorators/select/select-factory.ts
The store key that is present in the SelectFactory class is a singleton for the entire server ( it is not an Angular specific singleton ). You can see that from the way it is used inside the decorator: https://github.com/ngxs/store/blob/8b52ae654c0a707612a8d23bde4737e69cbb8ee9/packages/store/src/decorators/select/symbols.ts#L11
Because of this if for one user the NGXS module is destroyed at some point and at that point for another user the server-side code is still running the error appears
I would suggest refactoring this decorator to depend on the Store in every component that is used rather than on the store which is a singleton for the entire server.
Also, this issue is present inside the select-snapshot repository as well and is the same underlying problem with a server-wide singletone ( https://github.com/ngxs-labs/select-snapshot )