Skip to content

Commit 568c48b

Browse files
atscottthePunderWoman
authored andcommitted
fix(router): avoid mutating Route.resolve when resolving dynamic route titles
`getResolve` assigns `route.resolve` directly onto `ActivatedRouteSnapshot._resolve`, and `runResolve` previously attached dynamic `Route.title` resolvers by mutating `resolve[RouteTitleKey] = config.title` in place on that object. Because `route.resolve` is user-provided configuration, mutating it in place modifies the caller's `ResolveData` object directly. When two routes share a `ResolveData` constant—one with a dynamic `title` resolver and another with a static string `title` or no `title`—navigating to the first route attaches `RouteTitleKey` onto the shared object, so subsequent navigations to the second route run the first route's title resolver and overwrite the static title in `futureARS.data`. Mutating `resolve` also throws a `TypeError` when `route.resolve` is frozen with `Object.freeze`. Shallow-copy `futureARS._resolve` in `runResolve` when attaching `RouteTitleKey` so `resolveNode` sees the dynamic title resolver without mutating `route.resolve`.
1 parent 37d8968 commit 568c48b

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎packages/router/src/operators/resolve_data.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ function runResolve(
8282
paramsInheritanceStrategy: 'emptyOnly' | 'always',
8383
) {
8484
const config = futureARS.routeConfig;
85-
const resolve = futureARS._resolve;
86-
if (config?.title !== undefined && !hasStaticTitle(config)) {
87-
resolve[RouteTitleKey] = config.title;
88-
}
85+
const resolve =
86+
config?.title !== undefined && !hasStaticTitle(config)
87+
? {...futureARS._resolve, [RouteTitleKey]: config.title}
88+
: futureARS._resolve;
8989
return defer(() => {
9090
futureARS.data = getInherited(futureARS, futureARS.parent, paramsInheritanceStrategy).resolve;
9191
return resolveNode(resolve, futureARS, futureRSS).pipe(

‎packages/router/test/operators/resolve_data.spec.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,68 @@ describe('resolveData operator', () => {
247247
const rootSnapshot = TestBed.inject(Router).routerState.root.firstChild!.snapshot;
248248
expect(rootSnapshot.firstChild!.firstChild!.data).toEqual({bResolve: 'b', aResolve: 'a'});
249249
});
250+
251+
it('should not mutate a shared resolve object when resolving a dynamic title', async () => {
252+
@Component({
253+
template: '',
254+
})
255+
class Empty {}
256+
257+
const sharedResolve = {item: () => 'resolved-item'};
258+
259+
TestBed.configureTestingModule({
260+
providers: [
261+
provideRouter([
262+
{
263+
path: 'a',
264+
component: Empty,
265+
resolve: sharedResolve,
266+
title: () => 'Dynamic A',
267+
},
268+
{
269+
path: 'b',
270+
component: Empty,
271+
resolve: sharedResolve,
272+
title: 'Static B',
273+
},
274+
]),
275+
],
276+
});
277+
278+
const router = TestBed.inject(Router);
279+
const harness = await RouterTestingHarness.create('/a');
280+
expect(router.routerState.root.firstChild!.snapshot.title).toBe('Dynamic A');
281+
expect(router.routerState.root.firstChild!.snapshot.data['item']).toBe('resolved-item');
282+
283+
await harness.navigateByUrl('/b');
284+
expect(router.routerState.root.firstChild!.snapshot.title).toBe('Static B');
285+
expect(router.routerState.root.firstChild!.snapshot.data['item']).toBe('resolved-item');
286+
});
287+
288+
it('should support a frozen resolve object when resolving a dynamic title', async () => {
289+
@Component({
290+
template: '',
291+
})
292+
class Empty {}
293+
294+
const frozenResolve = Object.freeze({item: () => 'resolved-item'});
295+
296+
TestBed.configureTestingModule({
297+
providers: [
298+
provideRouter([
299+
{
300+
path: 'a',
301+
component: Empty,
302+
resolve: frozenResolve,
303+
title: () => 'Dynamic A',
304+
},
305+
]),
306+
],
307+
});
308+
309+
const router = TestBed.inject(Router);
310+
await RouterTestingHarness.create('/a');
311+
expect(router.routerState.root.firstChild!.snapshot.title).toBe('Dynamic A');
312+
expect(router.routerState.root.firstChild!.snapshot.data['item']).toBe('resolved-item');
313+
});
250314
});

0 commit comments

Comments
 (0)