question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Zoneless async router guards break change detection

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/router

Is this a regression?

Probably not

Description

Async router guards that run outside of zone break change detection.

πŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-zoneless-guard-issue?file=src%2Fapp%2Fapp.module.ts

Change detection is triggered when clicking the button on the eagerly loaded component but it is not triggered when clicking the button on the lazy loaded component as it’s running outside of zone.

Even if async guards that run outside of zone are a rare case, it can happen and produce issues like with @angular/fire here https://github.com/angular/angularfire/issues/2453 or https://github.com/angular/angularfire/issues/2355.

Maybe the router should get back in the zone once the guard emits the value somewhere after the routing guard management here: https://github.com/angular/angular/blob/f930e75a809a1e21369050884c20f42a350c411f/packages/router/src/router.ts#L590-L631

Should I try to shoot a PR?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Feb 25, 2021

I played with it a bit more and it appears that it can be simplified using an operator that wraps all notifications into a call to NgZone.run:

const rezone = (zone: NgZone) => <T>(source: Observable<T>) =>
  new Observable<T>(observer => source.subscribe({
    next: (v) => zone.run(() => observer.next(v)),
    error: (e) => zone.run(() => observer.error(e)),
    complete: () => zone.run(() => observer.complete()),
  }));

This can then be used together with dezone, see StackBlitz.

1reaction
JiaLiPassioncommented, Jul 31, 2020

I will try to check this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 routing breaks (click) handler change detection
Looks like this fix was relatively simple! For some reason when we router.navigate we lose change detection. The following is a way to...
Read more >
Navigation Guards | Vue Router
As the name suggests, the navigation guards provided by Vue router are primarily used to guard navigations either by redirecting it or canceling...
Read more >
angular/angular - Gitter
it's asynchronous, in that if you change any of the variables (this._hover), it takes angular a tick to update the change. Fabian Wiles....
Read more >
CRAN Packages By Name - The Comprehensive R Archive Network
ACA, Abrupt Change-Point or Aberration Detection in Point Series ... AsynchLong, Regression Analysis of Sparse Asynchronous Longitudinal Data.
Read more >
@ngrx/component-store | Yarn - Package Manager
The change detection is triggered via Ι΅markDirty in zone-less mode. ... RouterStore: Fix cancelled navigation with async guard (fixes #354 (#355 (920c0ba),Β ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found