-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RouteReuseStrategy.shouldReuseRoute has arguments swapped #16192
Comments
I concur with poke. it is not yet fixed at least in v 4.1.1 |
I just noticed this on 4.3.6 as well and then found this issue to confirm I'm not the only one. |
Still not fixed in
|
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 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;
}
} |
I have a trivial fix, but I'm not sure what tests need to be added so the PR is acceptable: I want to note that inverting the two arguments is not enough: Basically, any asymmetric routeReuseStrategy is impossible because of this issue. |
@lemoinem my workaround for that is to swap the arguments only if the first snapshot targets the 'primary' outlet. This guarantees that the call did not originate from Of course that workaround will break when |
@StevenLiekens This doesn't work for me. |
Weird I thought only child routes were affected by the swapped arguments bug |
Still not fixed in 7.2.7 |
versione 7.2.10 still effected. It would be great to have some news about it... |
Still not fixed in 8.2.3 |
There is a one-line fix PR waiting to be reviewed/merged, but I had no feedback. I'm open to any suggestion as to what to do to get it merged or attract some kind of attention. |
I just ran into this as well. I find it rather sad that this bug is open so long, when someone just has to change a single line to fix a major inconsistency. @jasonaden @mhevery, could someone please have a look at the PR for this? |
Any news on this? I seem to be losing my |
Hi all, I agree that the code looks suspect and is likely incorrect. That said, it's difficult to justify changing anything without a proper demonstration of incorrect behavior. Can someone create a minimal repro on StackBlitz that demonstrates unexpected behavior due to this bug? |
@atscott |
The `createOrReuseChildren` function calls shouldReuseRoute with the previous child values use as the future and the future child value used as the current argument. This is incosistent with the argument order in `createNode`. This inconsistent order can make it difficult/impossible to correctly implement the `shouldReuseRoute` function. Usually this order doesn't matter because simple equality checks are made on the args and it doesn't matter which is which. More detail can be found in the bug report: angular#16192. Fix angular#16192
The `createOrReuseChildren` function calls shouldReuseRoute with the previous child values use as the future and the future child value used as the current argument. This is incosistent with the argument order in `createNode`. This inconsistent order can make it difficult/impossible to correctly implement the `shouldReuseRoute` function. Usually this order doesn't matter because simple equality checks are made on the args and it doesn't matter which is which. More detail can be found in the bug report: angular#16192. Fix angular#16192
The `createOrReuseChildren` function calls shouldReuseRoute with the previous child values use as the future and the future child value used as the current argument. This is incosistent with the argument order in `createNode`. This inconsistent order can make it difficult/impossible to correctly implement the `shouldReuseRoute` function. Usually this order doesn't matter because simple equality checks are made on the args and it doesn't matter which is which. More detail can be found in the bug report: angular#16192. Fix angular#16192
The `createOrReuseChildren` function calls shouldReuseRoute with the previous child values use as the future and the future child value used as the current argument. This is incosistent with the argument order in `createNode`. This inconsistent order can make it difficult/impossible to correctly implement the `shouldReuseRoute` function. Usually this order doesn't matter because simple equality checks are made on the args and it doesn't matter which is which. More detail can be found in the bug report: angular#16192. Fix angular#16192 BREAKING CHANGE: This change corrects the argument order when calling RouteReuseStrategy#shouldReuseRoute. Previously, when evaluating child routes, they would be called with the future and current arguments would be swapped. If your RouteReuseStrategy relies specifically on only the future or current snapshot state, you may need to update the shouldReuseRoute implementation's use of "future" and "current" ActivateRouteSnapshots.
…r#26949) The `createOrReuseChildren` function calls shouldReuseRoute with the previous child values use as the future and the future child value used as the current argument. This is incosistent with the argument order in `createNode`. This inconsistent order can make it difficult/impossible to correctly implement the `shouldReuseRoute` function. Usually this order doesn't matter because simple equality checks are made on the args and it doesn't matter which is which. More detail can be found in the bug report: angular#16192. Fix angular#16192 BREAKING CHANGE: This change corrects the argument order when calling RouteReuseStrategy#shouldReuseRoute. Previously, when evaluating child routes, they would be called with the future and current arguments would be swapped. If your RouteReuseStrategy relies specifically on only the future or current snapshot state, you may need to update the shouldReuseRoute implementation's use of "future" and "current" ActivateRouteSnapshots. PR Close angular#26949
I know this has been fixed, but since that fix hasn't been released yet I had to come up with a workaround to get a consistent order of arguments. Now I haven't used this for a long time, but so far it seems like it's doing its job pretty … consistently? So I thought I'd share it with you, maybe it'll be helpful for someone or you have a different idea. In the leaderboard of worst code I've written, this is ranked pretty high: private createNodeStack: string;
public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
const caller = new Error().stack.split('\n')[1];
/*
* See comment below for why we do this.
* When called for the first time, store caller from stack trace.
* The first call will be from 'createNode'.
*/
if (!this.createNodeStack) {
this.createNodeStack = caller;
}
/*
* Workaround for Angular bug #16192 (https://github.com/angular/angular/issues/16192)
* Argument order for calls of this function is currently not consistent so
* we need to swap them if called from 'createOrReuseChildren'.
* This will break in a future release when the order is fixed.
*/
if (caller !== this.createNodeStack) {
[future, curr] = [curr, future];
}
[…] // do your thing here
} |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Hey, I noticed the following inconsistency regarding the arguments passed to
RouteReuseStrategy.shouldReuseRoute
increate_router_state.ts
:In line 26, the function is called like this:
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:
So here,
curr
andprevState
are passed tocreateOrReuseChildren
which in turn does the following:Here are two iterations happening, once of the children of
curr
and once for the children ofprevState
. Within the inner iteration,child
is the child ofcurr
, andp
is the child ofprevState
. However, the call toshouldReuseRoute
now is in reverse to the one before: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 withA => B
and onceB => 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.
The text was updated successfully, but these errors were encountered: