Ionic 4 routing caching / history preservation


I have been having an ongoing problem which I haven’t found any clean solutions to, so I decided to repost this with a video demo and github code to better demonstrate how this actually appears to be a problem with routing or caching? Not sure.

To keep it very short, here are the steps:

  1. Login screen, click “Login”
  2. On successful login you see a number of users
  3. Click “Person A”
  4. “Person A” is found in “Case X”
  5. Click “Case X”
  6. Inside “Case X” obviously we see linked “Person A”
  7. Click “Person A” again
  8. Now click “Back” button.

Here is a problem. I expect the back button to take me to Case step #6.

Instead, it takes me all the way back to step #2.

YouTube demo:

Github repo:

The cleanest workaround I found was to pass a url param from “Case X” to “Person A”. If param exists, back button would take user from person to case; otherwise, if no location history exists, fall back on default.

1 Like

Hey @f1v,

I studied your github repo, and found one issue on routing back stages.

      <ion-back-button defaultHref="home"></ion-back-button>

Remove defaultHref=“home” from your code.

Use this <ion-back-button></ion-back-button>

Hope this will solve your issue.


Hi @Priyanka34,

That doesn’t fix it. I just pushed a new commit removing defaultHref.

By definition defaultHref shouldn’t matter there, should it? Because I am navigating from case to a person. Clicking back button should take me to a case. defaultHref should only matter when the app isn’t aware where I came from.

you can solve this issue by creating a directive for handle back stages.

Create directive ‘location-back-directive.directive’.


import {Directive, HostListener} from '@angular/core';
import {Location} from '@angular/common';

  selector: '[app-location-back]'
export class LocationBackDirectiveDirective {
  constructor(private location: Location) { }

  @HostListener('click', ['$event'])
  clickEvent(event) {

Now create a share module.


import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LocationBackDirectiveDirective } from '../../directive/location-back-directive.directive';

  declarations: [LocationBackDirectiveDirective],
  imports: [
  exports: [
export class ShareModule { }


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { ShareModule } from './share/share/share.module';

  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule,ShareModule],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  bootstrap: [AppComponent]
export class AppModule {}

    <ion-buttons slot="start">
      <ion-button app-location-back fill="clear">
         <ion-icon name="arrow-back" slot="icon-only"></ion-icon>

    <li><a routerLink="/person" routerDirection="forward">Person A</a></li>
    <li>Person B</li>
    <li>Person C</li>



import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import { PeoplePageRoutingModule } from './people-routing.module';

import { PeoplePage } from './';

import { ShareModule } from '../../share/share/share.module';
  imports: [
  declarations: [PeoplePage],
export class PeoplePageModule {}

Here i just show you a people page code. Use this way in all other pages, when you wants to handle back stages/routing.

Hope this will more clear for you and solve your issue.


Thanks Priyanka34, that kind of works technically 100%, however it reloads the previous page and does not gracefully animate back.

Thanks for your time.