single-spa icon indicating copy to clipboard operation
single-spa copied to clipboard

Angular preventNavigation does not work as expected

Open anchor-volume opened this issue 3 years ago • 1 comments

Describe the bug or question single-spa:before-routing-event fires after the url has been changed but before unmounting and mounting. Calling cancelNavigation doesn't cancel the navigation. The navigation has occurred and the url has changed.

cancelNavigation instead cancels the unmount and mounting that would have occurred during the routing change and initiates a new routing event back to the previous route.

When logging the events and the mounting state the single-spa logs also do not reflect the actual mounting state but assumes the unmount/mount will occur. I added additional logging to the angular module to log when mounting actually occurs to demonstrate the difference

Issues

  1. "single-spa:before-routing-event" occurs after location.href has changed
  2. cancelNavigation fires another "single-spa:before-routing-event" which can create an infinite loop
  3. getMountedApps() returns incorrect mount state

To Reproduce

To reproduce create a new single-spa application with angular microfrontends, listen to the event single-spa:before-routing-event and call cancelNavigation() for all attempted route changes. console.log the events and notice the infinite loop of routing events.

Or clone this repo and follow the instructions in the Readme.md Repo: single-spa-angular-routing-bug Quick setup: clone repo, npm i, npm start

Expected behavior single-spa:before-routing-event should fire before any routing event and before any url change. cancelNavigation should prevent the navigation from happening instead of going back to the previous navigation

Screenshots and/or console output image

image

Additional context

  • The example does not use single-spa layout.html
  • reproduced with angular@14 and single-spa-angular@7

anchor-volume avatar Jan 06 '23 18:01 anchor-volume

Facing the same issue with angular@16 and single-spa-angular@9

gagan-lohia avatar Jan 17 '24 23:01 gagan-lohia