baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
467 stars 40 forks source link

"navigate" event triggers twice if `pushState : false` #41

Closed DanweDE closed 5 years ago

DanweDE commented 9 years ago

If pushState : false is used in the options, the "navigate" event is being triggered two times. This is the case in both Chrome 41 Ubuntu 14.10 and Firefox 36.0.4 Ubuntu 14.10 after calling Grapnel#navigate. When using pushState : true the event is only triggered once as expected.

vojtechkral commented 9 years ago

I'm experiencing this bug too, it's quite unfortunate. Steps to reproduce:

Define a route with a handler function and then try this code in console: location.hash = ''; location.hash = '/some/route/'

The handler is triggered twice. Interestingly enough, it only happens when the hash is changed in a quick succesion like this.

vojtechkral commented 9 years ago

The bug is caused by Grapnel's path() function, which reads the path from current URL rather than from the hashchange event object. The trouble is that when two hashchange events come one after another, the URL changes too quickly and path() returns the wrong path.