Skip to content

Route state gets stuck in Angular Universal #22

@tstackhouse

Description

@tstackhouse

Is this a regression?

Not sure

Description

When using this library in a project with Angular Universal, the first page load in the SSR response loads the requested route, however, subsequent navigations always respond with the initial route from the server site, before CSR boots and navigated to the actual route.

I've traced it to navigateIfNeeded, here https://github.com/ngneat/elf-ng-router-store/blob/master/libs/ng-router-store/src/lib/router.service.ts#L115, it seems like something is happening with potential stale emissions from the router repository, where the update to the route that is being requested isn't in the store before the call to setUpStoreListener happens, resulting in navigateIfNeeded issuing a navigateByUrl to the previous route on the server side.

Please provide a link to a minimal reproduction of the bug

I'm attempting to create a reproduction of this issue on this stackblitz: https://stackblitz.com/edit/stackblitz-starters-5q2tm5?file=src%2Findex.html

That said, I've ran into issues with this where it seems to just get stuck in some kind of infinite loop on the server side that I've yet to be able to track down.

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in

My application is an MFE based angular app with multiple static remotes federated into a host app, some using names router outlets, and using the router store helps to ensure that all the MFEs see the same router state, as named router outlets result in things like ActivatedRoute not having the "real" URL represented in their state(s).

Anything else?

I've identified a workaround by adding the following code in the constructor of my AppModule:

if (isPlatformServer(this.platformId)) {
  this.routerRepository.update({ state: null, navigationId: null });
}

Additionally, moving the import of ElfNgRouterStoreModule from my host application, into each MFE's shell seems to at least sidestep the issue, as the above code was not reliably producing a fix. Not having the state clearing resulted in infinite navigation loops causing the SSR server to hang. That said, moving that import seems to produce multiple subscriptions to the router store and I saw multiple calls to navigateIfNeeded, that however all resulted in no navigation being performed. However I would imagine that all of this is less than ideal.

Do you want to create a pull request?

If I can figure out a fix to propose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions