ngxs-labs / select-snapshot

🌊 Flexibile decorator that allows to get a snapshot of the state
MIT License
42 stars 4 forks source link

You've forgotten to import "NgxsSelectSnapshotModule" // the NGXS module #103

Open elegon32 opened 3 years ago

elegon32 commented 3 years ago

Description

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)

🔬 Minimal Reproduction

Ok. We found out how to reproduce:

  1. open a page in browser
  2. refresh
  3. quickly abort loading
  4. quickly refresh again
  5. --> many many FATAL Errors

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.

🔥 Exception or Error


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)

or....

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)

Environment


Libs:
    "@angular/core": "^11.0.6",
    "@ngxs/store": "^3.7.1",
    "@ngxs-labs/select-snapshot": "^2.0.1",

Browser: only on SSR
elegon32 commented 3 years ago

Ok. We found out how to reproduce:

  1. open a page in browser
  2. refresh
  3. quickly abort loading
  4. quickly refresh again
  5. --> many many FATAL Errors

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.

elegon32 commented 3 years ago

This is the same issue as further explained here: https://github.com/ngxs/store/issues/1723