Skip to content

Instantly share code, notes, and snippets.

@rbazinet
Forked from hopsoft/prefetch.js
Created February 6, 2020 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbazinet/033d1d79ce29e1483d82c3137de00de5 to your computer and use it in GitHub Desktop.
Save rbazinet/033d1d79ce29e1483d82c3137de00de5 to your computer and use it in GitHub Desktop.
Turbolinks Prefetching
const hoverTime = 400
const fetchers = {}
const doc = document.implementation.createHTMLDocument('prefetch')
function fetchPage (url, success) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.setRequestHeader('VND.PREFETCH', 'true')
xhr.onreadystatechange = () => {
if (xhr.readyState !== XMLHttpRequest.DONE) return
if (xhr.status !== 200) return
success(xhr.responseText)
}
xhr.send()
}
function prefetchTurbolink (url) {
fetchPage(url, responseText => {
doc.open()
doc.write(responseText)
doc.close()
const snapshot = Turbolinks.Snapshot.fromHTMLElement(doc.documentElement)
Turbolinks.controller.cache.put(url, snapshot)
})
}
function prefetch (url) {
if (prefetched(url)) return
prefetchTurbolink(url)
}
function prefetched (url) {
return location.href === url || Turbolinks.controller.cache.has(url)
}
function prefetching (url) {
return !!fetchers[url]
}
function cleanup (event) {
const element = event.target
clearTimeout(fetchers[element.href])
element.removeEventListener('mouseleave', cleanup)
}
document.addEventListener('mouseover', event => {
if (!event.target.dataset.prefetch) return
const url = event.target.href
if (prefetched(url)) return
if (prefetching(url)) return
cleanup(event)
event.target.addEventListener('mouseleave', cleanup)
fetchers[url] = setTimeout(() => prefetch(url), hoverTime)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment