# Enable View Cache for Ionic

**URL:** <https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235>\
**Category:** ionic-v3\
**Created:** [January 15, 2018, 6:01am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235 "2018-01-15T06:01:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dohoangn](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dohoangn](https://forum.ionicframework.com/u/dohoangn)\
**Post date:** [January 15, 2018, 6:01am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/1 "2018-01-15T06:01:28Z")

</div>

I am looking for ionic page cache similar to ionic 1 cache-view=true, so everytime I navigate back to the page it will show me the latest state I was in (search results, paging, filter, category, keyword …). However I learned that this feature is not available in Ionic 3.  
If I use [navPush] like this

```auto
<button ion-button icon-only color="royal" [navPush]="searchProductPage">
	<ion-icon name="search"></ion-icon>
</button>

```

Everytime I click in search button to go to search page, the state is reset, everything clear  
If I use the back button on navigation bar then the state is cached  
I have tried popTo(page) but the problem is it will remove all history down to search page from nav stack, if I want to remember the search page, then that’s ok, but If I have another page (for example settings), then it is not possible.

I thought if I get the old page from the nav stack and move it the last index it would work, but in fact it is not working, the popTo also removing history.

```auto
public goToPage( page: any ) {
        let hasPage: boolean = false;
        for ( let v of this.navCtrl.getViews() )
        {
            if (page.name === v.component.name) {
                this.navCtrl.popTo( page );
                hasPage = true;
                console.log(".......Pop to page from history")
            }
        }
        if (!hasPage)
            this.navCtrl.push( page );

    }

```

or

```auto
public goToPage( page: any ) {
        let hasPage: boolean = false;
        let oldPage: Component;
        let index: number;
        console.log("......Nav size: " + this.navCtrl.length())
        for ( let i=0; i < this.navCtrl.length(); i++ )
        {
            let v = this.navCtrl.getViews()[i]
            if (page.name === v.component.name) {
                oldPage = v.component;
                hasPage = true;
                index = i;
                console.log(".......Pop to page from history: " + page.name)
            }
        }
        if (!hasPage)
            this.navCtrl.push( page );
        else {

            this.navCtrl.insert(this.navCtrl.length(), oldPage);
             this.navCtrl.popTo( oldPage );
            console.log(".......insert old page: " + oldPage.name)
        }
    }

```

I also try ngrx/store however it is became very complex as it has to create reducer for keywork, filters, categories tree, search results, pager (infinitive scrolling), even so it could not get the last location in the scrolling that user have previous view. I have use ngrx/store in save product interface, and it is fine as it only have to save single product ngmodel.

The cache-view in ionic 1 is to convenient for this use case.

Could you please give me a direction?  
Thanks!

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [January 15, 2018, 7:25am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/2 "2018-01-15T07:25:10Z")

</div>

You can just buiid a provider that keeps a global state, sort of a redux-lite. You don’t need full ngrx. The advantages of ngrx are: immutability, memoized selectors, and the ability to program side effects. If you don’t need those, just create a simple provider, and read the provider’s data from any page you want.

---

<div class="post-metadata">

**Author:** ![dohoangn](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dohoangn](https://forum.ionicframework.com/u/dohoangn)\
**Post date:** [January 15, 2018, 11:55pm UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/3 "2018-01-15T23:55:47Z")

</div>

I think it will be hard and messy to keep track of all object in the page, like results, scrolling position, filters, page, query keyword and also need to sync from page to provider, then provider to page. Getting the old page from somewhere in nav stack to put it in the last index position will be better, instead of pop to the page and remove all pages after this page, we can move the page to the last position.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [January 15, 2018, 11:59pm UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/4 "2018-01-15T23:59:20Z")

</div>

Is there some reason NavController.insert does not work for this? You seem to be describing something that is already covered by the API.

---

<div class="post-metadata">

**Author:** ![dohoangn](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dohoangn](https://forum.ionicframework.com/u/dohoangn)\
**Post date:** [January 16, 2018, 12:21am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/5 "2018-01-16T00:21:13Z")

</div>

I have tried in my method public goToPage

`this.navCtrl.insert(this.navCtrl.length(), oldPage);`

but did not seem to work

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [January 16, 2018, 12:29am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/6 "2018-01-16T00:29:53Z")

</div>

> [@dohoangn](#):
>
> did not seem to work

I can’t do anything with this. insert and insertPages perform what you asked for in your answer to me.

---

<div class="post-metadata">

**Author:** ![dohoangn](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dohoangn](https://forum.ionicframework.com/u/dohoangn)\
**Post date:** [January 16, 2018, 12:36am UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/7 "2018-01-16T00:36:47Z")

</div>

the problem is moving a page from middle of stack to last index by insert only get the navigation flow to the page, but the state of the page is cleared out, but popTo method keep the state of the page. Could the NavController team provider a method like movePage(fromIndex, toIndex) and REMEMBER THE STATE OF THE PAGE?

---

<div class="post-metadata">

**Author:** ![nicolopaganin](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nicolopaganin](https://forum.ionicframework.com/u/nicolopaganin)\
**Post date:** [March 1, 2018, 1:14pm UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/8 "2018-03-01T13:14:01Z")

</div>

Hi, any news on this feature?

I need it too, is very boring lose the page state everytime the user change page.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:54pm UTC](https://forum.ionicframework.com/t/enable-view-cache-for-ionic/118235/9 "2021-07-19T21:54:51Z")

</div>

Closed on July 19, 2021, 9:54pm UTC


