[4.0.0-beta.12] Ionic 4 - Angular 6: How to control Ionic router history to stop cache a view component?


#1

Maybe I am missing something, but I’m experiencing a problem with Ionic caching views previously visited, preventing the components from being re-initialized by Angular. The components are pulled from some cache and rendered as whatever data existed previously.

Example :

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.

I noticed ion-router-outlet adds page transitioning when navigation, and this transition swaps to the left, when the navigation goes “forward” and swipe right when the navigation goes “backwards”. This data seems to read from the same history-source that stores cached views.

TL;DR How to control Ionic router history (url tree) from caching the views preventing angular to re-initialize the components?

Github question
StackOverflow question


#2

If you don’t get any better answers, my opinion is “you should design your application so you don’t care about this”.

Instead of doing timing-critical things in lifecycle event handlers, what I do is to subscribe to Observables that are exposed in service providers at component creation time, and unsubscribe in destruction time. I don’t care how many times that pairing happens, and I don’t care how many times that component goes in and out of the foreground in the meantime. As long as that subscription is active, the freshest data will always be received and available in the component controller. If these updates happen when the component is obscured, so be it.


#3

Thank you for the answer. It is an option I didn’t consider, and can possibly fix my problem, by redesigning my app architecture.

I see that I am not the only one having issues with Ionic 4 router stack history handling according to this GitHub thread, so I will keep an eye on this process before deciding what to do.