Skip to content

bug(router): beforeEnter is not triggered for initial navigation #22936

@vicb

Description

@vicb

Bug Report

Ionic version:

[ ] 4.x
[x] 5.5.4

Current behavior:

I have a simple project with two routes /foo and /bar.

When I navigate to them via <ion-item button href=...> the beforeEnter hook is called as expected.

However when I visit navigate to my project by typing the url http://<site>/bar then the beforeEnter hook is not called.

I think this behavior is not expected ?
At least it could be a pain if you want to lazy-load the component in the beforeEnter hook (or check authentication, ...)

Expected behavior:

The beforeEnter hook should be called when directly navigating to the url, i.e. http://<site>/bar

Steps to reproduce:

  • Navigate to https://runkit.com/vicb/ionic-router
  • Click on "endpoint" at the to open the app,
  • Open the console
  • Click on go to bar / go to foo - you should see beforeEnter logs in the console
  • Manually update the url to end in "/foo" or "/bar",
  • The content of <ion-nav> is updated as expected but the beforeEnter is not triggered (no logs).

Workaround:

A workaround would be to force an initial navigation after the router is loaded:

    customElements.whenDefined('ion-router').then(() => {
      const router = document.querySelector('ion-router');
      const target = ...;
      router?.push(target, 'root');
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions