#70965·angular

canDeactivate and resolvers are skipped when a RouteReuseStrategy recreates the component

Author: aminesbdevCreated Sep 26, 2026Updated Sep 26, 2026
Labelsarea: routergemini-triaged

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

router

Is this a regression?

No

Description

With a custom RouteReuseStrategy that doesn't reuse a route, the component gets destroyed and recreated, but its canDeactivate guard never runs. Neither do canActivate or the resolvers for the new instance.

A lot of apps force components to be recreated this way:

typescript
class NeverReuseStrategy extends BaseRouteReuseStrategy {
  override shouldReuseRoute() {
    return false;
  }
}

Take a form route with an "unsaved changes" guard:

typescript
{
  path: 'form',
  component: FormPage,
  canDeactivate: [unsavedChangesGuard],
  resolve: {page: pageResolver},
}
  1. Go to /form?page=1 and type something in the form.
  2. Navigate to /other: the guard runs and blocks the navigation. That's fine.
  3. Navigate to /form?page=2 instead: the form is destroyed and a new one is created, so what the user typed is gone. The guard is never called, and the resolver doesn't run for the new component either.

The cause seems to be that two parts of the router decide on their own whether a route is reused:

  • createRouterState asks routeReuseStrategy.shouldReuseRoute(). When it returns false, the component is destroyed.
  • getRouteGuards in utils/preactivation.ts only checks future.routeConfig === curr.routeConfig. For the same config, it treats the route as reused and only reruns guards and resolvers according to runGuardsAndResolvers, which with the default paramsChange means nothing on a query param change.

As far as I can tell, this goes back to when RouteReuseStrategy was introduced in 42cf06fa12. Before that, both places compared routeConfig, and the strategy was only plugged into create_router_state.

I'm not sure whether this is intended, which is why I'm opening an issue rather than a PR. The docs point both ways:

  • RunGuardsAndResolvers says "Guards and/or resolvers will always run when a route is activated or deactivated", and here the component is deactivated.
  • The "React to same-URL navigations" section of the customizing route behavior guide says guard and resolver reruns are governed by runGuardsAndResolvers, while component reuse is determined by the route reuse strategy, which reads as if the two are meant to be independent.

runGuardsAndResolvers: 'always' works around it (that's how #49035 ended up), but it also runs canDeactivate when the component is kept, which isn't the same thing.

I have a change that passes the strategy to getAllRouteGuards and only treats a route as reused when it has the same config and shouldReuseRoute() returns true. With DefaultRouteReuseStrategy that's the same check as today, so nothing changes for apps that don't customize it. Apps with a strategy like the one above would start running their guards and resolvers whenever the component is recreated. That's arguably what they expect, but it's a visible change: a canDeactivate with a confirm() would now prompt on those navigations, and #70784 shows people are already sensitive to extra canDeactivate calls.

So the question: should a component that the RouteReuseStrategy destroys go through canDeactivate, and should the new one go through canActivate and the resolvers? If yes, I'm happy to open the PR. If not, I think it's worth a line in the RouteReuseStrategy docs, since losing a form without the guard being asked is pretty surprising.

Please provide a link to a minimal reproduction of the bug

https://gist.github.com/aminesbdev/b3c96c0ea491a04adb949b0211e88202

It's a single index.html on Angular 22.2.0 with no build step: download it and open it in a browser (it loads Angular from esm.sh and uses hash routing).

Please provide the exception or error you saw

No error. The form component is destroyed and recreated without its canDeactivate guard being called, and the resolver doesn't run for the new instance.

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

Angular 22.2.0. Also reproduced on main (319a3c4f32).

Anything else?

No response