Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Abort XHR on popstate / Maintain history when aborting XHR #162

merged 1 commit into from

3 participants


I was working on a fix for #155 yesterday, but @davydotcom beat me to the punch this morning. He took almost the exact approach as I was, but his commit didn't handle a couple side effects:

  1. If you clicked a link and then clicked the back button before the request finished, the page would still change to the requested page once the request finally finished. Adding xhr?.abort() to fetchHistory() fixes this.

  2. If you clicked a link and then clicked another link before the first one loaded, the first one will abort and the second requested page will load. This is what we just fixed, but if the next thing you do is click the back button, nothing will happen. Calling rememberCurrentUrl() on xhr abort ensures the proper behavior when navigating the history.

  3. I also refactored a little bit by moving all the xhr = null statements into the onloadend callback.


just checked over all of this, It all looks good, I like xhr?.abort() better as well :+1: @dhh


apologies for not catching those also, kinda rushed that one. Might tackle some better tests here soon.

@dhh dhh merged commit 4ed2285 into rails:master
@smidwap smidwap referenced this pull request

Transition cache #286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2013
  1. @reed
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 7 deletions.
  1. +6 −7 lib/assets/javascripts/
13 lib/assets/javascripts/
@@ -22,13 +22,12 @@ fetchReplacement = (url) ->
# Remove hash from url to ensure IE 10 compatibility
safeUrl = removeHash url
- xhr.abort() if xhr
+ xhr?.abort()
xhr = new XMLHttpRequest 'GET', safeUrl, true
xhr.setRequestHeader 'Accept', 'text/html, application/xhtml+xml, application/xml'
xhr.setRequestHeader 'X-XHR-Referer', referer
- xhr.onabort = ->
- xhr = null
xhr.onload = =>
doc = createDocument xhr.responseText
@@ -42,11 +41,10 @@ fetchReplacement = (url) ->
triggerEvent 'page:load'
- xhr = null
- xhr.onerror = ->
- document.location.href = url
- xhr = null
+ xhr.onloadend = -> xhr = null
+ xhr.onabort = -> rememberCurrentUrl()
+ xhr.onerror = -> document.location.href = url
@@ -54,6 +52,7 @@ fetchHistory = (state) ->
if page = pageCache[state.position]
+ xhr?.abort()
changePage page.title, page.body
recallScrollPosition page
triggerEvent 'page:restore'
Something went wrong with that request. Please try again.