Repository navigation
Description
Bug Report
Ionic Info
Ionic:
ionic (Ionic CLI) : 4.5.0 (/usr/local/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.0.0-beta.19
@angular-devkit/build-angular : 0.10.7
@angular-devkit/schematics : 7.0.7
@angular/cli : 7.0.7
@ionic/angular-toolkit : 1.2.0
System:
NodeJS : v10.13.0 (/usr/local/bin/node)
npm : 6.4.1
OS : macOS Mojave
Describe the Bug
(There are arguably two interrelated but separate issues here. If I should create separate issues then please let me know! 🙂)
Forward navigation to a Page with the same URL (excluding query parameters) will modify the navigation stack history (not the browser history stack but the StackController stack, which are different). It will then re-use a previous Page instance that does not contain the latest navigation data from the forward navigation to the same URL.
Steps to Reproduce
Steps to reproduce the behavior:
- Clone this repo on your machine: https://github.com/KevinKelchen/ionic4-stack-page-reuse . It is a slightly modified version of the
sidemenuAngular stater template. - In the terminal, path into the repo root and run
npm i. - Run
ionic serve. - Click the Menu icon and then
List. - Click
Item 1to forward navigate to the detail screen forItem 1.- You will notice there's a timestamp that displays. I chose a timestamp in this example so that there's an easy way to see on-screen what navigation data is being used.
- This timestamp data was stored in a shared service prior to navigating from the source
Page. It was then retrieved from the targetPagefrom the same service. This pattern was outlined by Josh Morony here. The data in the service is keyed in a dictionary using thenavIdUUID URL query parameter. - A shared service is being used because we are migrating from Ionic 3 and the use of
NavParamswhich allowed passing complex data like callback functions and objects which cannot be serialized in a lossless way in the URL or@ionic/storage. - A UUID was chosen so that navigating to what's otherwise the same URL will use the navigation parameters specific to the current navigation. This will also preserve previous navigation parameters so they can be used when revisiting a
Pagein the browser history.
- Click the pencil/create button in the header. This will navigate you to a different Page type called
List Detail More. - From this Page, click
Item 1to forward navigate to the detail screen forItem 1. Note that the timestamp is the old value that was displayed when you initially navigated toItem 1. This is because theStackControllerrolled back the stack and re-used the old instance of thePageforItem 1. The newnavIdURL query parameter was not used to retrieve the data from the shared service and thus is not using fresh data on this forward navigation. - To observe the
StackControllernavigation stack modification, click theion-back-buttonand it will take you to the initialListPageinstead ofList Detail Morewhich is the screen you were previously on before revisitingItem 1.
Related Code
The steps to reproduce above link to this repository which is set up to reproduce the issue.
Expected Behavior
I expected forward navigation to a Page with the same URL to use the latest, current navigation data from the forward navigation to the same URL and not modify the StackController navigation stack history.
Additional Context
Here is some additional context from a message I posted in #16367:
Note: My definition of "reusing" a Page is when "forward" navigating to a URL and instead of pushing a new Page onto the StackController's "stack" it re-uses an instance of the Page that is already present in the stack.
I've been having issues with StackController as well but on Beta 15. I am using the Angular router imperatively in TypeScript but I believe I was experiencing them as well using the new NavController (which uses the Angular router).
Here's a related behavior I've observed:
When you use the router to "forward" navigate to a Page, if the URL (excluding query parameters) is the same as a Page that is already in the "stack," the StackController will remove all of the previous Pages in the stack back to the first occurrence of the matching URL/Page. It then re-uses that first occurrence of the Page in the stack. And so then when you use ion-back-button, which seems to ignore browser history and only uses the stack, the Page you go back to is not what you would expect--it's the Page prior to the first occurrence of the Page in the stack.
- Note: Since the Android and browser back button seems to ignore the
StackController's "stack," I've tried working around this issue to some success by creating my own custom back button component that just uses Angular'sLocation.back()when you click it. This tends to result in a lot of destroyed/re-initializing of Pages whose state you can attempt to preserve by storing off data in a shared service that is keyed with a unique ID (such as a UUID) present in the URL.
How StackController re-uses Pages has actually been tripping me up in another way:
If I use the Angular router to "forward" navigate to a Page with the same URL as one in the StackController's stack and I have navigation parameters that I store in a shared service (as opposed to the URL) and key the parameter storage in the service off of a URL query parameter such as a UUID (a query parameter--not using the matrix URL notation as noted at the end of this section), because the Page is being re-used due to a matching URL instead of being considered as different, the new query parameter value is not being used and the Page pulls the previous parameters out of the shared service due to using the old query parameter value.
Perhaps the solve is to work around it by just putting the UUID in as a matrix URL notation parameter and effectively opt-out of any StackController reuse of Pages.
- Upsides
- "Forward" navigating to the same Page/URL would use the correct parameters.
- With all of the Pages in the stack remaining wired up (never destroyed or reused), when you navigate back to them they are exactly the way you left them when you navigated away, which is how Ionic 3 worked.
- Downsides
- It's non-default behavior that differs from how Ionic seems to think it should work in Ionic 4.
- Although it's like Ionic 3, you lose an optimization in which all Pages stay wired-up in the DOM as you continue to forward navigate through the app which may potentially increase the amount of memory that's used by the app; I would have concerns of how this might scale as users keep forward navigating through the app.
Thank you for all that you do and for making a great framework! 🙂
Activity
KevinKelchen commentedon Nov 30, 2018
Behavior is likewise the same with Beta 17.
Ionic Team: I can update the repo for reproducing the issue to Beta 17 if you wish. Please just let me know. 🙂
Thank you!
bryce13950 commentedon Dec 2, 2018
If you could update it for ionic beta 17 then that would be very helpful.
KevinKelchen commentedon Dec 3, 2018
@bryce13950, the repository for reproducing the issue has now been updated to Beta 17. Please let me know if there's anything else that you would like.
Thank you! 😃
bryce13950 commentedon Dec 7, 2018
I spent sometime playing around with this, and I think the issue stems from the reuse strategy in the app.module.ts. I simply removed the following...
Then the pages seemed to update properly. Maybe the ionic route strategy will just need to be removed for your project?
bgpedersen commentedon Dec 11, 2018
I'm having the same page caching issue for several weeks now, which is starting to affect critical areas of my applictation for different situations. I am refreshing this thread everyday in hope a solution comes soon. I think it's a fine solution that pages pulled from cached if the user is pressing back/going back in history, but I don't think pages should ever be pulled from cache if navigating to a url (going forward) - even if the page has previously been visited.
Example 1:
User A is logged into the application and starts on the 'Home' page with info relevant to user A. User A logs out and navigates to 'Log In'.
User B logs in from the same application and navigates to 'Home' page. Ionic see's that Home was previously visited (by user A) and instead of instantiating 'Home', the view is pulled from cache and displays everything that User A was seing.
Example 2) A user clicks a recieved notification about a new post was created. This navigates to the post wall which will load the new post and show it in the wall. But if the user is already on the post wall or the post wall have been visited already, the component/page is pulled from cache and always display the old data without being able to hook into normal Angular behavior for ngOnInit.
--
Note:
I tried to remove the following as suggestedby @bryce13950 without any change:
I tried to use navigateRoot() as suggested by @manucorporat without success:
navController.navigateRoot()13 remaining items
daem0ndev commentedon Mar 29, 2019
@KevinKelchen could you try the latest nightly and see if you're still having issues here?
liamdebeasi commentedon Mar 29, 2019
Hi @KevinKelchen,
I tried out the repository you provided, and it looks like this issue has been resolved by #17888. Would you be able to try it out using the latest Ionic dev build to ensure that everything is working as you would expect? You can install the nightly using
npm i @ionic/core@dev @ionic/angular@dev.Thanks!
KevinKelchen commentedon Apr 2, 2019
@daem0ndev and @liamdebeasi,
I will give it a shot!
Thank you! 😀
KevinKelchen commentedon Apr 2, 2019
I am experiencing the same behavior as before.
Installing the nightly changed my
package.jsonto use the following versions:@liamdebeasi, is running that command to install the nightly and following the steps to reproduce what you did to check if it resolved the issue? Did you make any other changes?
Thanks!
KevinKelchen commentedon Apr 3, 2019
@daem0ndev reached out to me in effort to reproduce the issue. 😀
I'm not sure why it wasn't appearing during my initial attempts, but after blowing away
node_modulesand trying again I began seeingTypeError: this.router.getCurrentNavigation is not a functionin the console. It looks like that method was introduced in Angular 7.2.0-beta.1. My sample app repo's lockfile was using an older version of Angular 7.After updating to Angular 7.2.x, it works as expected! 🎉 Thank you, @daem0ndev!
KevinKelchen commentedon Apr 3, 2019
As I've learned more, I think there are two related "issues" in this single GitHub issue:
ActivatedRouteObservables are not firing when using a cached Page.ion-tabsI was expecting a linear navigation stack.ion-back-buttonto go back doesn't take you to the screen you were previously looking at but rather the screen that's before the cached Page instance. Using a custom back button that uses Angular'sLocation.back()or the browser back button uses browser history and thus works differently and works as I would have expected. Downsides though include wondering if I'm going against Ionic's intended non-linear navigation design as well as Page instances having to be re-initialized (not a huge deal; works closer to vanilla Angular). Perhaps a separate feature request "issue" could be used for that discussion.I'm going to close this issue since the primary issue is resolved. 🙂
Thank you!
Topiya commentedon Apr 24, 2019
@KevinKelchen I have tried
npm i @ionic/core@dev @ionic/angular@devand my package.json changed as below:and the Angular version is
"@angular/common": "^7.2.2",But still I am facing the problem like, when I navigate back to last page of stack, page reuses the old data.
Can you please suggest me on this?
karvannan-thi commentedon May 8, 2019
my package.json
"@ionic/angular": "4.1.0",
me too facing the same problem
KevinKelchen commentedon May 8, 2019
Hi @Topiya and @karvannan-thi, 👋
This was fixed in Ionic Angular 4.2.0.
https://github.com/ionic-team/ionic/releases/tag/v4.2.0
@Topiya,
Are all of your Angular packages upgraded? Not just
@angular/common?sunrisetechdoc commentedon May 11, 2019
My package.json
"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"@angular/forms": "^7.2.2",
"@angular/http": "^7.2.2",
"@angular/router": "^7.2.2",
"@ionic-native/core": "^5.0.0",
"@ionic/angular": "^4.2.0",
"cordova-android": "8.0.0",
me too facing the same problem
ionitron-bot commentedon Jun 10, 2019
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.