# Ionic 2 build --prod issue with nav.getPrevious()

**URL:** <https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566>\
**Category:** ionic-v3\
**Created:** [May 9, 2017, 8:01pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566 "2017-05-09T20:01:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 9, 2017, 8:01pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/1 "2017-05-09T20:01:00Z")

</div>

Hi,

I have a problem with ionic 2 --prod building.

I have this template:

`<button ion-button block (click)="goToPage()">Go to page</button>`

An this controller:

```auto
constructor(public nav: NavController) {..}

goToPage() {
  if(this.nav.getPrevious().component.name=="Page"){
     this.nav.pop();
  }
  else this.nav.push(Page);
}

```

I check if the previous page in nav stack is called “Page” if so I pop() to this page, if not I push this page.

Testing in browser is working well. Testing on Android / IOS, emulator / device with `ionic build ios` and `ionic build android` also working like a charm.

BUT when I `ionic build ios --prod` or `ionic build android --prod` the component “Page” is always pushed even if the previous page is also “Page”!

Any idea why? And how can I fix it?

Thanks

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 9, 2017, 8:06pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/2 "2017-05-09T20:06:14Z")

</div>

Never rely on class names as strings. As you have observed, minification breaks it. Figure out another solution, such as using lifecycle events.

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 9, 2017, 8:50pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/3 "2017-05-09T20:50:06Z")

</div>

Thank you for your answer. Can you please give me an example of checking the previous page? I only find this.nav.getPrevious() for that…

---

<div class="post-metadata">

**Author:** ![FrancoisIonic](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/francoisionic/32/106767_2.png) [@FrancoisIonic](https://forum.ionicframework.com/u/FrancoisIonic)\
**Post date:** [May 9, 2017, 9:26pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/4 "2017-05-09T21:26:37Z")

</div>

Or simply don’t rely on previous from browser app, just use push directly to the page, it’s way more reliable to my experience. Rethink your buttons to only call direct calls to a push page with simple functions. (I have already a 3 levels tabs nav with sub pages, and it works well, all showing top level back button if i want). (and i forgot all 3 tabs are child of home.ts).

goToPage()  
this.nav.push(Page);  
}

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 9, 2017, 9:38pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/5 "2017-05-09T21:38:01Z")

</div>

> [@EvenMind](#):
>
> Can you please give me an example of checking the previous page?

No, I’m saying don’t do that. Each page should be independent of one another, or your design is imposssible to test. Figure out a way to have lifecycle events let pages take control of their own destiny.

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 10, 2017, 10:02am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/6 "2017-05-10T10:02:31Z")

</div>

Here is the hierarchy of my app: 2 Tabs of lists.

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/6/2/62b811bb525c1727382dcfaa583de2a67a83afc4.jpg)

So when i go to `list of shops -> i choose a shop -> i go to the list of products (the list is on the same page) -> a choose a product -> i click on "see shop"` I want to do a nav.pop() to return to the shop page, because if not (if i push again the page “shop”) it will create an infinite navigation… Can you please help me to handle this problem?

---

<div class="post-metadata">

**Author:** ![mich356c](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mich356c/32/105794_2.png) [@mich356c](https://forum.ionicframework.com/u/mich356c)\
**Post date:** [May 10, 2017, 10:08am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/7 "2017-05-10T10:08:33Z")

</div>

Shouldn’t

```auto
navCtrl.popTo(ShopPageComponent);

```

Work just fine in this scenario?

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 10, 2017, 10:15am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/8 "2017-05-10T10:15:55Z")

</div>

> [@mich356c](#):
>
> popTo

Yeah, that works but sometimes in need to push the page and not popTo it. For exemple with this scenario:

`list of products -> product -> see shop` here I push the page “ShopPage” because it is not yet in the stack.

That’s why i need to determine if the previous page is already a “ShopPage”.

I thought about navParameters on the product page. For exemple if the “ShopPage” is already in the stack:

`this.nav.push(ProductPage, { button_see_shop: false });`

And then I disable the button “See shop”. It is a good idea?

---

<div class="post-metadata">

**Author:** ![mich356c](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mich356c/32/105794_2.png) [@mich356c](https://forum.ionicframework.com/u/mich356c)\
**Post date:** [May 10, 2017, 10:17am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/9 "2017-05-10T10:17:54Z")

</div>

What does the promise from popTo return? If it gives you an error saying it isn’t in the stack, you could push it then

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 10, 2017, 10:22am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/10 "2017-05-10T10:22:31Z")

</div>

I will try this. Thank you!

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 10, 2017, 11:09am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/11 "2017-05-10T11:09:32Z")

</div>

It doesn’t work because even if the ShoPage isn’t in the stack it just pop() to the previous page.  
I used my solution with navParameters and it worked 🙂

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 10, 2017, 8:22pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/12 "2017-05-10T20:22:25Z")

</div>

Can’t you just `setRoot(ShopPage)`?

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 11, 2017, 7:51am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/13 "2017-05-11T07:51:06Z")

</div>

No because my root is Tabs components. If i setRoot(ShopPage) I will not have my tabs at the bottom.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 11, 2017, 8:37am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/14 "2017-05-11T08:37:49Z")

</div>

Hmm. Maybe it makes sense to others, but I would find this a really confusing UX. If tabs are ever present, I expect them to always be present.

---

<div class="post-metadata">

**Author:** ![EvenMind](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@EvenMind](https://forum.ionicframework.com/u/EvenMind)\
**Post date:** [May 11, 2017, 9:03am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/15 "2017-05-11T09:03:00Z")

</div>

> [@rapropos](#):
>
> If tabs are ever present, I expect them to always be present.

Yeah it is the case! I was saying that if i setRoot(ShopPage) the tabs will no longer be present.

---

<div class="post-metadata">

**Author:** ![zgluis](https://avatars.discourse-cdn.com/v4/letter/z/e274bd/32.png) [@zgluis](https://forum.ionicframework.com/u/zgluis)\
**Post date:** [April 25, 2018, 2:37pm UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/16 "2018-04-25T14:37:15Z")

</div>

You can try using:

this.navCtrl.getPrevious().id == “MyPage”

---

<div class="post-metadata">

**Author:** ![alterora](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@alterora](https://forum.ionicframework.com/u/alterora)\
**Post date:** [January 11, 2019, 8:02am UTC](https://forum.ionicframework.com/t/ionic-2-build-prod-issue-with-nav-getprevious/89566/17 "2019-01-11T08:02:24Z")

</div>

zgluis, you are a lifesaver! I had a similar issue and your suggestion resolve it. Your response should be marked as the correct answer.
