# Ionic PWA Loading The Wrong First Page

**URL:** <https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748>\
**Category:** ionic-v3\
**Created:** [May 17, 2018, 6:09pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748 "2018-05-17T18:09:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 17, 2018, 6:09pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/1 "2018-05-17T18:09:02Z")

</div>

I’m having a _weird_ issue with our PWA, where’s the app is initially loading the wrong page, before loading the correct one specified from the URL ([http://localhost:8100/#/groups](http://localhost:8100/#/groups)).

I’m defining `links` in our `IonicModule.forRoot` like:

```auto
[
{ component: PersonListPage, segment: 'people'},
{ component: GroupListPage, segment: 'groups'}, 
...
]

```

It seems random but it _always_ loads the **18th** segment in the list, seems bazaar right? If I re-arrange the items, it still loads the **18th** segment.

I thought maybe it could be related to _priority_, although we have `preloadModules` set to `false`.

I’ve added `null` checks to each page to ensure parameters are set so it doesn’t thrown an exception, although it still flashes that first page before loading the correct page.

Any thoughts or suggestions on what could be causing this issue?

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [May 17, 2018, 10:15pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/2 "2018-05-17T22:15:14Z")

</div>

> [@dalezak](#):
>
> initially loading the wrong page

Is the ‘wrong page’ your AppComponent’s `rootPage`:

```auto
  public rootPage: any = 'SignInPage'; 

```

Do user’s have to sign up/sign in?

```auto
    this.authService.afAuth.authState.subscribe(user => {

        if (user) {
          this.rootPage = 'TabsPage';
        } else {
          this.rootPage = 'SignInPage';
        }
      }, () => {
        this.rootPage = 'SignInPage';
      }
    );

```

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [May 17, 2018, 10:23pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/3 "2018-05-17T22:23:49Z")

</div>

I try to follow the Angular Style Guide: [https://angular.io/guide/styleguide#app-root-module](https://angular.io/guide/styleguide#app-root-module)

So my AppModule is very ‘light’:

```auto
@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    CoreModule,
    IonicModule.forRoot(AppComponent, {
      backButtonText: ''
      // preloadModules: true
      // tabsHideOnSubPages: true
    })
  ],
  bootstrap: [IonicApp],
  entryComponents: [AppComponent],
  providers: []
})
export class AppModule {}

```

And my CoreModule does all the heavy lifting

```auto
@NgModule({
  imports: [
    AngularFireModule.initializeApp(ENV.firebase),
    AngularFirestoreModule,
    AngularFireAuthModule,
    CommonModule,
    HttpClientModule,
    IonicModule,
    ...
    ServiceWorkerModule.register('/ngsw-worker.js', { enabled: ENV.production }),
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  exports: [],
  declarations: [],
  providers: [
    AuthService,
    ...
    { provide: LoggerService, useClass: ConsoleLoggerService },
    ...
    ServiceWorkerService
  ]
})
export class CoreModule {
  constructor( @Optional() @SkipSelf() parentModule: CoreModule,
               private translate: TranslateService,
               private afs: AngularFirestore) {

    throwIfAlreadyLoaded(parentModule, 'CoreModule');

    translate.setDefaultLang('en');

    const settings = { timestampsInSnapshots: true };
    afs.app.firestore().settings(settings);
  }
}

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

```

And, I lazy load pages.

BTW, you can configure your segments via @IonicPage:

```auto
@IonicPage({
  segment: 'sign-in'
})

```

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 5:38pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/4 "2018-05-18T17:38:12Z")

</div>

Hmm @robinyo, something strange is definitely happening with the routes but can’t figure out what.

I added a `LoadingPage` which I set `rootPage` to, so that should be loaded first before I determine if the user is logged in or not. When the PWA first loads, it shows the `LoadingPage` and then either shows `SigninPage` or `HomePage` correctly.

However if I refresh the page or try to load [http://localhost:8100/#/signin](http://localhost:8100/#/signin) directly, that’s when it loads the wrong page. Also now that there’s another segment listed in the `links`, it’s showing a different page first, in this case the last one in the array.

I’m setting `links` inside `IonicModule.forRoot` where `ROUTES` is the array of pages.

```auto
IonicModule.forRoot(MyApp, {}, {links:this.ROUTES})

```

And also setting `segment` inside of `IonicPage` as well.

```auto
@IonicPage({
  segment: 'signin'
})

```

Could setting `segment` in both `IonicPage` and `IonicModule.forRoot` be the issue? Or does the order you specify your `links` matter?

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 7:54pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/5 "2018-05-18T19:54:14Z")

</div>

Oh @robinyo, looks like this bug might be related?

> <https://stackoverflow.com/questions/45905615/ionic-deeplinker-breaks-on-refresh>

> <https://github.com/ionic-team/ionic/issues/12765>

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 8:55pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/6 "2018-05-18T20:55:42Z")

</div>

Ok, I think I’ve made some progress, by the looks of those issues the `DeepLinker` has problems sometimes matching names.

I previously had not defined any `name` values in `IonicPage` or in `deepLinkConfig`. So after adding a name for each component, I no longer get that random first page loading, so that’s good!

However I now started getting another error when trying to reload a page like [http://localhost:8100/#/groups:](http://localhost:8100/#/groups:)

```auto
Error: Uncaught (in promise): TypeError: Cannot set property 'groups' of undefined
TypeError: Cannot set property 'groups' of undefined

```

Clicking links in the PWA works fine, the problem only happens when reloading a page or trying to access a page by it’s URL like [http://localhost:8100/#/groups](http://localhost:8100/#/groups).

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 9:14pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/7 "2018-05-18T21:14:05Z")

</div>

Ok, looks like that specific error is being caused when loading values in `ionViewWillEnter` vs `ionViewDidEnter`.

When clicking links in the app, everything works as expected. However when reloading a page or accessing page directly like [http://localhost:8100/#/groups](http://localhost:8100/#/groups), some of values are not getting set as I’d expect.

Looks like I’ll need to dig deeper on how I’m passing values between pages versus how they are being set when loaded from the DeepLinker.

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [May 18, 2018, 9:16pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/8 "2018-05-18T21:16:33Z")

</div>

Did you try to patch the DeepLinker:

```auto
UrlSerializer.prototype.getLinkFromName = function (nameOrComponent) {
        return this.links.find(function (link) {
            return (link.component === nameOrComponent) 
                       || (link.name === nameOrComponent) 
                       || (nameOrComponent.indexOf(link.name )>-1); //added this line
        });
    };

```

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 9:34pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/9 "2018-05-18T21:34:01Z")

</div>

Ok, I think I’ve figured things out, _sort of_.

Turns out my original problem with the `DeepLinker` loading the wrong first page, was fixed when I gave all pages a `name` value _(in case someone else runs into the same problem)_.

The other problem is different, due to how I am expecting certain parameters being set in `ionViewWillEnter` of pages.

When its running as an app, I’m passing parameters in `navController.push` or `navController.setRoot` and then loading those values using `navParams.get` in pages `ionViewWillEnter`.

However when it’s running as a PWA, opening a page directly like [http://localhost:8100/#/groups](http://localhost:8100/#/groups) is relying on the `DeepLinker` to load that page, those same parameters are not being passed.

So I’ll need to rethink how pages expect parameters, since they need to work for both mobile app _and_ as PWA.

---

<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:** [May 18, 2018, 9:36pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/10 "2018-05-18T21:36:38Z")

</div>

Just FYI, the routing for Ionic 4 is likely to be way better. So I do everything I can except deal with the Deep Linker, expecting that this problem with “solve itself” soon.

Maybe a slap in your face, now that you’ve done so much work, but I do think things will get better sometime this summer.

---

<div class="post-metadata">

**Author:** ![dalezak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalezak/32/103044_2.png) [@dalezak](https://forum.ionicframework.com/u/dalezak)\
**Post date:** [May 18, 2018, 9:44pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/11 "2018-05-18T21:44:22Z")

</div>

Thanks for the message @AaronSterling, I’m looking forward to Ionic 4!

Our app was originally built for iOS and Android, just recently converted to also run as a PWA.

For the most part things are working alright, although it’s taking some refactoring to skip using the native plugins when running in the browser and now trying to get routes wired up has been a learning curve.

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [May 18, 2018, 9:48pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/12 "2018-05-18T21:48:26Z")

</div>

> [@dalezak](#):
>
> However when it’s running as a PWA, opening a page directly like [http://localhost:8100/#/groups](http://localhost:8100/#/groups) is relying on the DeepLinker to load that page, those same parameters are not being passed.

Remember if your bookmarking a URL then you need to provide the ‘id’ as a part of the URL. You can’t pass objects only object id’s.

```auto
@IonicPage({
  name: 'VenuePage',
  segment: 'venue/:id'
})

...

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private venuesService: VenuesService,
              private logger: LoggerService) {

    this.itemId = navParams.get('id');
  }

```

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [May 18, 2018, 11:01pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/13 "2018-05-18T23:01:54Z")

</div>

> [@dalezak](#):
>
> I previously had not defined any name values in IonicPage or in deepLinkConfig. So after adding a name for each component, I no longer get that random first page loading, so that’s good!

I’m also using tabs:

```auto
<ion-tabs #tabs selectedIndex="2" (ionChange)="onIonChanged($event)">
  <ion-tab [root]="tab0Root" tabUrlPath="activity-tab" tabIcon="fa-fal-list"></ion-tab>
  <ion-tab [root]="tab1Root" tabUrlPath="location-tab" tabIcon="fa-fal-map-marker"></ion-tab>
  <ion-tab [root]="tab2Root" tabUrlPath="search-tab" tabIcon="fa-fal-street-view"></ion-tab>
  <ion-tab [root]="tab3Root" tabUrlPath="person-tab" tabIcon="fa-fal-user"></ion-tab>
  <ion-tab [root]="tab4Root" tabUrlPath="notifications-tab" tabIcon="fa-fal-bell"></ion-tab>
</ion-tabs>

```

So I followed this advice: [URL navigation redirects to root page when using lazy loaded tabs template · Issue #12680 · ionic-team/ionic-framework · GitHub](https://github.com/ionic-team/ionic/issues/12680#issuecomment-323522508)

tabs.page.ts:

```auto
@IonicPage({
  name: 'TabsPage',
  segment: 'sbat' // tabs
})

```

search.page.ts:

```auto
@IonicPage({
  name: 'SearchPage',
  segment: 'hcraes' // search
})

```

verified.venues.page.ts:

```auto
@IonicPage({
  name: 'VerifiedVenuesPage',
  segment: 'verified-venues',
  defaultHistory: ['SearchPage']
})

```

So I can open a page directly, like: [http://localhost:8100/#/sbat/search-tab/verified-venues](http://localhost:8100/#/sbat/search-tab/verified-venues)

---

<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:35pm UTC](https://forum.ionicframework.com/t/ionic-pwa-loading-the-wrong-first-page/130748/14 "2021-07-19T21:35:19Z")

</div>

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


