Repository navigation
Description
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
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:
angular/packages/router/src/router_state.ts
Lines 476 to 478 in 77504f1
| 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
atscott commentedon Aug 21, 2023
Guards and resolvers are only rerun on certain conditions (this includes the
titleproperty). You can update your route configuration to includerunGuardsAndResolvers: 'paramsChange',.stackblitz example: https://stackblitz.com/edit/stackblitz-starters-8nj5dk
AleksandrSibiakov commentedon Aug 28, 2023
But the problem I mentioned is not about guards/resolvers - they run as expected (and I believe
paramsChangeis default value).The problem is with
ActivatedRoute.titleproperty. Please, check fork of your stackblitz example: https://stackblitz.com/edit/stackblitz-starters-v2ndqsI put
console.logwithin title's resolver so it is clear it run correctlyP.S. Sorry if I should not report this in closed issue.
atscott commentedon Aug 28, 2023
@AleksandrSibiakov the tile functions run as resolvers.
AleksandrSibiakov commentedon Aug 29, 2023
@atscott I feel like we are not on the same page...
Issue is not with running resolver function, but emitting new value from
ActivatedRoute.titleafter resolver is actually ran. (reproducible in attached stackblitz by changingidparam via input)Anyway, if you insist it is expected behavior I will not argue. It's pretty minor and rare case in the end.
atscott commentedon Aug 29, 2023
@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.
AleksandrSibiakov commentedon Aug 30, 2023
Oh, stop it 😳
I'm so glad that my contribution is adopted.
Thanks
fix(router): Ensure title observable gets latest values (angular#51561)
angular-automatic-lock-bot commentedon Oct 1, 2023
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.
fix(router): Ensure title observable gets latest values (angular#51561)
fix(router): Ensure title observable gets latest values (angular#51561)