Skip to content
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

fix(router): Only retrieve stored route when reuse strategy indicates it should reattach #30263

Closed
wants to merge 1 commit into from

Conversation

dimakuba
Copy link
Contributor

@dimakuba dimakuba commented May 4, 2019

When creating the router state, the RouteReuseStrategy#retrieve should
only be called when RouteReuseStrategy#shouldAttach returns true.
That is, we should only retrieve a stored route when the reuse strategy
indicates that there is one stored and that it should be reattached.

This now matches the behavior in the route activation:

if (this.routeReuseStrategy.shouldAttach(future.snapshot)) {
const stored =
(<DetachedRouteHandleInternal>this.routeReuseStrategy.retrieve(future.snapshot));

Fixes #23162

@dimakuba dimakuba requested a review from a team as a code owner May 4, 2019 18:33
@googlebot

This comment has been minimized.

@ngbot ngbot bot added this to the needsTriage milestone May 6, 2019
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the commit message to describe what prompted this change. It doesn't appear to be just a "refactor" as it would change whether retrieve were called. Also, would this resolve #23162? If so, can you please add that as a note in the commit message as well?

packages/router/src/create_router_state.ts Outdated Show resolved Hide resolved
packages/router/test/create_router_state.spec.ts Outdated Show resolved Hide resolved
@atscott atscott changed the title refactor(router): determine if a route (and its subtree) should be reattached until detached route retrieving fix(router): Only retrieve stored route when reuse strategy indicates it should reattach Apr 13, 2021
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving but since I went ahead and applied fixes to this PR it should be reviewed by someone else as well.

@atscott
Copy link
Contributor

atscott commented Apr 13, 2021

presubmit

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good. I'm wondering if it's possible (and worthwhile) to add a test that would reproduce the original problem? It looks like the tests were updated to reflect the new behavior, should we also check that the retrieve function is never called in these scenarios?

… it should reattach

When creating the router state, the `RouteReuseStrategy#retrieve` should
only be called when `RouteReuseStrategy#shouldAttach` returns `true`.
That is, we should only retrieve a stored route when the reuse strategy
indicates that there is one stored and that it should be reattached.

This now matches the behavior in the route activation:
https://github.com/angular/angular/blob/1d12c50f63f90c91636185b2287e31e9c0291121/packages/router/src/operators/activate_routes.ts#L170-L172

Fixes angular#23162
@atscott
Copy link
Contributor

atscott commented Apr 13, 2021

@AndrewKushnir -- Oops! I had done that but didn't add it to the commit. PTAL

@sliekens
Copy link
Contributor

Regarding #23162

It seems like routeReuseStrategy.shouldAttach is still called for the initial navigation. I might just be misinterpreting the code, I haven't tested it. If so, is it possible/preferable to skip that test and go straight to the createActivatedRoute path for the initial navigation?

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atscott LGTM 👍

@atscott
Copy link
Contributor

atscott commented Apr 13, 2021

@StevenLiekens it may be possible but I'm wondering why it would be necessary. Shouldn't the RouteReuseStrategy implementation return false for shouldAttach on the initial navigation? I'm not completely familiar with this part of the router code and I'm wary of making changes beyond what I can determine to be safe. At least with the change as it is now, it can be supported by the existing implementation in the activate_routes code.

@sliekens
Copy link
Contributor

Shouldn't the RouteReuseStrategy implementation return false for shouldAttach on the initial navigation?

Yep, that's why I'm thinking the method shouldn't even be tested (you already know the answer). But it would only be a minor optimization so leaving it as-is seems completely reasonable.

@atscott atscott added the target: patch This PR is targeted for the next patch release label Apr 14, 2021
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Apr 14, 2021
@atscott atscott added the action: presubmit The PR is in need of a google3 presubmit label Apr 14, 2021
@atscott
Copy link
Contributor

atscott commented Apr 14, 2021

global presubmit
The initial presubmit brought up one test failure. The test assertion just needed to be updated and I don't believe it would affect the application functionality. Global presubmit should also run to verify there are no other failures.

edit: full global presubmit (green)

@atscott atscott removed the action: presubmit The PR is in need of a google3 presubmit label Apr 15, 2021
@atscott atscott added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note freq2: medium labels Apr 15, 2021
@atscott
Copy link
Contributor

atscott commented Apr 15, 2021

caretaker note: please merge and sync this one on its own. Global presubmit is green but it would still be good to do since this is a router change.

AndrewKushnir pushed a commit that referenced this pull request Apr 15, 2021
… it should reattach (#30263)

When creating the router state, the `RouteReuseStrategy#retrieve` should
only be called when `RouteReuseStrategy#shouldAttach` returns `true`.
That is, we should only retrieve a stored route when the reuse strategy
indicates that there is one stored and that it should be reattached.

This now matches the behavior in the route activation:
https://github.com/angular/angular/blob/1d12c50f63f90c91636185b2287e31e9c0291121/packages/router/src/operators/activate_routes.ts#L170-L172

Fixes #23162

PR Close #30263
AndrewKushnir pushed a commit that referenced this pull request Apr 15, 2021
… it should reattach (#30263)

When creating the router state, the `RouteReuseStrategy#retrieve` should
only be called when `RouteReuseStrategy#shouldAttach` returns `true`.
That is, we should only retrieve a stored route when the reuse strategy
indicates that there is one stored and that it should be reattached.

This now matches the behavior in the route activation:
https://github.com/angular/angular/blob/1d12c50f63f90c91636185b2287e31e9c0291121/packages/router/src/operators/activate_routes.ts#L170-L172

Fixes #23162

PR Close #30263
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: router cla: yes freq2: medium merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary calls to retrieve and shouldAttach in RouteReuseStrategy
6 participants