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

Route state gets stuck in Angular Universal #22

Open
tstackhouse opened this issue Aug 23, 2023 · 2 comments
Open

Route state gets stuck in Angular Universal #22

tstackhouse opened this issue Aug 23, 2023 · 2 comments

Comments

@tstackhouse
Copy link

tstackhouse commented Aug 23, 2023

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.

@stackblitz
Copy link

stackblitz bot commented Aug 23, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@Benny739
Copy link

Have you found a solution? I'm having the same problem.

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