Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vicb opened this issue Feb 17, 2021 · 4 comments · Fixed by #23123
Closed

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

vicb opened this issue Feb 17, 2021 · 4 comments · Fixed by #23123
Labels
package: core @ionic/core package type: bug a confirmed bug report
Milestone

Comments

@vicb
Copy link
Contributor

vicb commented Feb 17, 2021

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');
    });
@ionitron-bot ionitron-bot bot added the triage label Feb 17, 2021
@vicb vicb changed the title bug(router): beforeEnter is not triggered for initial mavigation bug(router): beforeEnter is not triggered for initial navigation Feb 17, 2021
@liamdebeasi liamdebeasi added package: core @ionic/core package type: bug a confirmed bug report labels Feb 18, 2021
@ionitron-bot ionitron-bot bot removed the triage label Feb 18, 2021
@liamdebeasi
Copy link
Contributor

Thanks for the issue. Can you try the following dev build and let me know if it resolves the issue?

Using NPM:

npm install @ionic/core@5.6.0-dev.202102181507.9d44979

Using CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@5.6.0-dev.202102181507.9d44979/dist/ionic/ionic.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/@ionic/core@5.6.0-dev.202102181507.9d44979/dist/ionic/ionic.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@5.6.0-dev.202102181507.9d44979/css/ionic.bundle.css" />

@vicb
Copy link
Contributor Author

vicb commented Feb 18, 2021

@liamdebeasi Thanks a lot for the super quickly fixing it.

I have tested it in the attached runkit and in my app and it works great ! (no more need for the workaround I mentioned).

I left a couple of minor comments on the PR.

@liamdebeasi liamdebeasi added this to the 5.7.0 milestone Mar 2, 2021
liamdebeasi pushed a commit that referenced this issue May 20, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
resolves #22936
@liamdebeasi
Copy link
Contributor

Thanks for the issue. This has been resolved via #23123, and a fix will be available in an upcoming release of Ionic Framework.

@ionitron-bot
Copy link

ionitron-bot bot commented Jun 19, 2021

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.