Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

asset change detection

  • Loading branch information...
commit a5e81561b9e4844a6e90da81556bf57ec5f2e85e 1 parent 6ca53da
@davydotcom davydotcom authored
Showing with 24 additions and 7 deletions.
  1. +23 −7 lib/assets/javascripts/turbolinks.js.coffee
  2. +1 −0  test/index.html
View
30 lib/assets/javascripts/turbolinks.js.coffee
@@ -2,7 +2,7 @@ pageCache = []
currentState = null
initialized = false
referer = document.location.href
-
+assets = []
visit = (url) ->
if browserSupportsPushState
@@ -12,7 +12,6 @@ visit = (url) ->
else
document.location.href = url
-
fetchReplacement = (url) ->
triggerEvent 'page:fetch'
xhr = new XMLHttpRequest
@@ -20,9 +19,11 @@ fetchReplacement = (url) ->
xhr.setRequestHeader 'Accept', 'text/html, application/xhtml+xml, application/xml'
xhr.setRequestHeader 'X-XHR-Referer', referer
xhr.onload = ->
- changePage extractTitleAndBody(xhr.responseText)...
- reflectRedirectedUrl xhr
- triggerEvent 'page:load'
+ doc = createDocument xhr.responseText
+ unless assetsChanged extractAssetsFrom doc
+ changePage extractTitleAndBody(doc)...
+ reflectRedirectedUrl xhr
+ triggerEvent 'page:load'
xhr.onabort = -> console.log 'Aborted turbolink fetch!'
xhr.send()
@@ -80,6 +81,7 @@ rememberCurrentState = ->
rememberInitialPage = ->
unless initialized
+ assets = extractAssetsFrom document
rememberCurrentUrl()
rememberCurrentState()
initialized = true
@@ -87,6 +89,21 @@ rememberInitialPage = ->
recallScrollPosition = (page) ->
window.scrollTo page.positionX, page.positionY
+extractAssetsFrom = (doc) ->
+ headAssets = []
+ for script in document.head.getElementsByTagName 'script'
+ headAssets.push script.src if script.src
+ for link in document.head.getElementsByTagName 'link'
+ headAssets.push link.href if link.href
+ headAssets
+
+assetsChanged = (headAssets)->
+ if assets.length == headAssets.length
+ for asset, index in assets
+ unless asset == headAssets[index]
+ document.location.reload()
+ return true
+ false
triggerEvent = (name) ->
event = document.createEvent 'Events'
@@ -94,8 +111,7 @@ triggerEvent = (name) ->
document.dispatchEvent event
-extractTitleAndBody = (html) ->
- doc = createDocument html
+extractTitleAndBody = (doc) ->
title = doc.querySelector 'title'
[ title?.textContent, doc.body ]
View
1  test/index.html
@@ -16,6 +16,7 @@
<li><a href="/other.html"><span>Wrapped link</span></a></li>
<li><a href="http://www.google.com/">Cross origin</a></li>
<li><a href="/other.html" onclick="if(!confirm('follow link?')) { return false}">Confirm Fire Order</a></li>
+ <li><a href="/reload.html"><span>Asset Change</span></a></li>
<li><a href="/dummy.gif?12345">Query Param Image Link</a></li>
<li><a href="#">Hash link</a></li>
</ul>
Please sign in to comment.
Something went wrong with that request. Please try again.