Skip to content

ActivatedRoute.title observable does not emit changes #51401

Closed

Description

@AleksandrSibiakov

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

The problem is that having RouteConfig.title configured as ResolveFn does not emit updates in the component via ActivatedRoute.title (which is of Observable type).
The correct/updated title value though is available in ActivatedRoute.snapshot.title.

I.e.
A route like

{
  path: ':id',
  component: SomeComponent,
  title: (snapshot) => snapshot.paramMap.get('id'),
}

and a component

export class SomeComponent implements OnInit {
 constructor(readonly route: ActivatedRoute) {
    route.params.pipe(map(() => route.snapshot.title)).subscribe(console.log); // logs every time params is updated
    route.title.subscribe(console.log); // only initial value is logged
}

I would expect both observable should log same values simultaneously, i.e. title Observable should be in sync with current snapshot.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-v2ndqs

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.2.9
Node: 18.15.0
Package Manager: npm 9.5.0
OS: win32 x64

Angular: 15.2.9
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.9
@angular-devkit/build-angular   15.2.9
@angular-devkit/core            15.2.9
@angular-devkit/schematics      15.2.9
@schematics/angular             15.2.9
rxjs                            7.8.1
typescript                      4.9.5

Anything else?

I believe the cause of the issue is next:

if (!shallowEqual(currentSnapshot.data, nextSnapshot.data)) {
route.dataSubject.next(nextSnapshot.data);
}

AFAIK the title is resolved as part of route's data and stored there under symbol Symbol(RouteTitle).
ActivatedRoute.title set as projection from ActivatedRoute.data[Symbol(RouteTitle)]

So in order for ActivatedRoute.title to emit a new value the ActivatedRoute.data should emit a new value. But, as we can see above, the ActivatedRoute.data only updated when shallowEqual(currentSnapshot.data, nextSnapshot.data) == false. The thing is that symbols are not enumerated as keys of data object thus not checked for equality.

The very naive solution might be to update check as following:

 if (!shallowEqual(currentSnapshot.data, nextSnapshot.data) || currentSnapshot.title !== nextSnapshot.title) {
      route.dataSubject.next(nextSnapshot.data);
 }

Activity

  1. added this to the needsTriage milestone on Aug 17, 2023
  2. atscott commented on Aug 21, 2023

    @atscott
    Contributor

    Guards and resolvers are only rerun on certain conditions (this includes the title property). You can update your route configuration to include runGuardsAndResolvers: 'paramsChange',.

    stackblitz example: https://stackblitz.com/edit/stackblitz-starters-8nj5dk

  3. AleksandrSibiakov commented on Aug 28, 2023

    @AleksandrSibiakov
    Author

    But the problem I mentioned is not about guards/resolvers - they run as expected (and I believe paramsChange is default value).

    The problem is with ActivatedRoute.title property. Please, check fork of your stackblitz example: https://stackblitz.com/edit/stackblitz-starters-v2ndqs

    I put console.log within title's resolver so it is clear it run correctly

    P.S. Sorry if I should not report this in closed issue.

  4. atscott commented on Aug 28, 2023

    @atscott
    Contributor

    @AleksandrSibiakov the tile functions run as resolvers.

  5. AleksandrSibiakov commented on Aug 29, 2023

    @AleksandrSibiakov
    Author

    @atscott I feel like we are not on the same page...
    Issue is not with running resolver function, but emitting new value from ActivatedRoute.title after resolver is actually ran. (reproducible in attached stackblitz by changing id param via input)

    Anyway, if you insist it is expected behavior I will not argue. It's pretty minor and rare case in the end.

  6. atscott commented on Aug 29, 2023

    @atscott
    Contributor

    @AleksandrSibiakov Sorry, you're right. I understood your report initially but forgot what I was looking at by the time I got to my desk. Should be a straightforward fix. Appreciate your patience here.

  7. AleksandrSibiakov commented on Aug 30, 2023

    @AleksandrSibiakov
    Author

    Oh, stop it 😳
    I'm so glad that my contribution is adopted.

    Thanks

  8. added a commit that references this issue on Sep 20, 2023
    24d7f17
  9. angular-automatic-lock-bot commented on Oct 1, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Oct 1, 2023
  11. added a commit that references this issue on Jan 23, 2024
    a484847
  12. added a commit that references this issue on Jan 31, 2024
    f464e39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions