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.

RouteReuseStrategy.shouldReuseRoute has arguments swapped

See original GitHub issue

Hey, I noticed the following inconsistency regarding the arguments passed to RouteReuseStrategy.shouldReuseRoute in create_router_state.ts:

In line 26, the function is called like this:

routeReuseStrategy.shouldReuseRoute(curr.value, prevState.value.snapshot)

So the first argument is the current state, and the second argument is the one before. This matches the documentation.

However, a few lines below, the following call is made:

const children = createOrReuseChildren(routeReuseStrategy, curr, prevState);

So here, curr and prevState are passed to createOrReuseChildren which in turn does the following:

function createOrReuseChildren(
    routeReuseStrategy: RouteReuseStrategy, curr: TreeNode<ActivatedRouteSnapshot>,
    prevState: TreeNode<ActivatedRoute>) {
  return curr.children.map(child => {
    for (const p of prevState.children) {
      if (routeReuseStrategy.shouldReuseRoute(p.value.snapshot, child.value)) {
        return createNode(routeReuseStrategy, child, p);
      }
    }
    return createNode(routeReuseStrategy, child);
  });
}

Here are two iterations happening, once of the children of curr and once for the children of prevState. Within the inner iteration, child is the child of curr, and p is the child of prevState. However, the call to shouldReuseRoute now is in reverse to the one before:

routeReuseStrategy.shouldReuseRoute(p.value.snapshot, child.value)

So first, the previous state child is passed and then the current state child, which is the reversed order to the function definition, where the second argument is the older state.

Now I’m not perfectly sure if this isn’t the desired behavior, since I don’t completely understand how the reuse strategy works, but this appears very odd to me. And it would explain the odd behavior I see when trying to implement a reuse strategy that dynamically decides based on the route. Right now, when switching from route A to B, I get two calls to shouldReuseRoute, once with A => B and once B => A making it impossible to create a strategy handle only one direction.

If someone confirms that this is a bug, I can gladly provide a pull request for the fix.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:28
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
sliekenscommented, Feb 7, 2018

I can confirm this is wrong. I’m trying to implement a route reuse strategy for a named router outlet and I’m getting swapped curr and future arguments for the named outlet.

Right now I’m doing something like this to work around the issue.

shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    if (future.outlet !== 'primary') {
        // do the switcheroo
        [future, curr] = [curr, future];
    }
    if (curr.routeConfig && curr.routeConfig.data && curr.routeConfig.data.useOnce) {
        return false;
    } else {
        return future.routeConfig === curr.routeConfig;
    }
}
3reactions
johnny-mhcommented, May 27, 2020

@atscott https://stackblitz.com/edit/angular-ivy-crrs-swapped You can check the future and curr swapped by clicking settings detail link. shouldReuseRoute invoke twice. 1st is swapped future and curr ambiguously.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrading from Angular 9 to 11 prevents router.navigate ...
If you use the Router's RouteReuseStrategy, the argument order has changed. When calling RouteReuseStrategy#shouldReuseRoute previously when ...
Read more >
RouteReuseStrategy
Provides a way to customize when activated routes get reused. abstract class RouteReuseStrategy { abstract shouldDetach(route: ActivatedRouteSnapshot): ...
Read more >
Angulars RouteReuseStrategy - Auroria
The strategy to give us the power to decide which route component is allowed to live beyond its routing, and which is doomed...
Read more >
Cache components with Angular RouteReuseStrategy
The shouldReuseRoute I think has a wrong parameters names since the future parameter seems to be the route we just left, so the...
Read more >
Angular 11 features and release date - devs-tools
... @angular/compiler: source span for microsyntax text attr should be key span; @angular/router: RouteReuseStrategy.shouldReuseRoute has arguments swapped.
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