Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$location.path() in $routeChangeStart causes infinite loop #9665

Closed
sylouuu opened this issue Oct 17, 2014 · 6 comments
Closed

$location.path() in $routeChangeStart causes infinite loop #9665

sylouuu opened this issue Oct 17, 2014 · 6 comments

Comments

@sylouuu
Copy link

sylouuu commented Oct 17, 2014

Hi,

I didn't have this issue with 1.2.26. Extract of my code:

app.run(['$rootScope', '$location', 'config', function ($rootScope, $location, config) {
    $rootScope.$on('$routeChangeStart', function (event, next, current) {
        // Maintenance mode
        if (config.project.maintenance === true) {
            event.preventDefault();

            // The redirect must be inside this method since angular 1.3.0
            $rootScope.$evalAsync(function () {
                $location.path('/maintenance');
            });
        }
    });
});

The /maintenance route has an empty controller. This code is causing an infinite loop, but not with 1.2.26. Got this error.

I understand why there is an infinite loop as I don't test the current route before redirecting. Why it worked before?

Is it a bug or something that I skipped from the changelog?

Bests

@caitp
Copy link
Contributor

caitp commented Oct 17, 2014

What do you mean by "empty controller"? Can you please create a reproduction of your issue, thanks.

@sylouuu
Copy link
Author

sylouuu commented Oct 17, 2014

@caitp
Copy link
Contributor

caitp commented Oct 17, 2014

So,

  1. $locationChangeStart fires
  2. $routeChangeStart fires during $locationChangeStart
  3. Your $routeChangeStart handler prevents default, cancelling $locationChangeStart and $routeChangeStart
  4. Your $routeChangeStart handler sets $location.path() and digests async or continues current digest
  5. url watch sees that the url has changed, and fires the URL change listener
  6. GOTO 1. because this list repeats itself

So, the issue here is basically that you're perpetually redirecting and aborting, and the evalAsync queue never fully drains.

I think this would work for you if you avoided changing the path if you're already within a maintenance path.

I'm not sure if this is really a valid bug, @tbosch what do you think? is it possible for us to solve this without the user fixing code?

@tbosch
Copy link
Contributor

tbosch commented Oct 18, 2014

Sorry, busy with prep for NgEurope...

@pkozlowski-opensource
Copy link
Member

For me it sounds very much similar to #9607 that also talks about redirects during route change change start event. We've got a fix for it in #9678 where preventDefault call will be no longer needed but it won't fix the situation where we are trying to redirect back to the same route.

Not sure why it worked in 1.2.x - we should probably understand it better but at the same time I would be tempted to say "won't fix" as there is an explicit infinite redirection in the example code.

@Narretz
Copy link
Contributor

Narretz commented Jan 22, 2016

Explicite Infinite redirection is not a bug, I agree.

@Narretz Narretz closed this as completed Jan 22, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants