Permalink
Browse files

Bypass initial popstate - Alternative to #322

  • Loading branch information...
1 parent 7ee1d92 commit e69bead53582fe40b9fe758a9751f20ab8f3053a @reed reed committed Jan 31, 2014
Showing with 15 additions and 1 deletion.
  1. +6 −0 CHANGELOG.md
  2. +9 −1 lib/assets/javascripts/turbolinks.js.coffee
View
@@ -17,6 +17,12 @@
*Nick Reed*
+* Delay binding of history change handler long enough to bypass the popstate event that some
+ browsers fire on the initial page load. Prevents duplicate requests in certain scenarios when
+ traversing the browser history.
+
+ *Anton Pawlik + Nick Reed*
+
## Turbolinks 2.2.0 (January 10, 2014)
* Add transition cache feature. When enabled, cached copies of pages will be shown
@@ -290,6 +290,12 @@ allowLinkExtensions = (extensions...) ->
htmlExtensions.push extension for extension in extensions
htmlExtensions
+
+# Delay execution of function long enough to miss the popstate event
+# some browsers fire on the initial page load.
+bypassOnLoadPopstate = (fn) ->
+ setTimeout fn, 500
+
installDocumentReadyPageEventTriggers = ->
document.addEventListener 'DOMContentLoaded', ( ->
triggerEvent 'page:change'
@@ -316,7 +322,9 @@ initializeTurbolinks = ->
createDocument = browserCompatibleDocumentParser()
document.addEventListener 'click', installClickHandlerLast, true
- window.addEventListener 'popstate', installHistoryChangeHandler, false
+
+ bypassOnLoadPopstate ->
+ window.addEventListener 'popstate', installHistoryChangeHandler, false
# Handle bug in Firefox 26/27 where history.state is initially undefined
historyStateIsDefined =

0 comments on commit e69bead

Please sign in to comment.