Nav: Race condition navigating to another page after initial page load causes reload of initial page #7535
Description
Scenario:
In iOS apps, one can register a custom URL scheme, so you can deep link to your app from the web/other apps. In Cordova, this is exposed via a handleOpenURL
global function that you implement. With jQM, I want that function to essentially call $(':mobile-pagecontainer').pagecontainer('change', url)
to navigate to the deep linked page.
Note, while this issue was discovered in a Cordova app, I was able to reproduce it quite easily in the browser, so going forward we will assume the browser case as the main scenario as it's a lot simpler to manage.
Repro:
- Open Chrome/Safari (I tested Chrome on Win7 and Safari on iOS), possibly in a private mode window to keep it 'clean'.
- Navigate to http://jsbin.com/gogat/1
Expected:
The browser will load the initial page, and then navigate to "Page Two". You should end up on "Page Two".
Actual:
The browser loads the initial page, navigates to "Page Two", and then quickly navigates back to the initial page. Note, the URL bar still thinks it is on "Page Two"!
Thoughts
I've done a bunch of debugging on this, and as best I can tell what's happening is that the initial popstate
event that is fired by Chrome and Safari is getting delayed until after the navigation to page two has occurred. Thus, when the initial popstate
gets here, the conditions to trap it fail (since there are two pages on the history stack, AND the current URL !== the initial page URL), and it ends up propagating and causing a navigation back to the initial page.
If I delay the navigate call by a long time, say 5 seconds, then you don't see this race condition.