   replaceDocument doc.body,title?.textContent, 'cache'
 
 
-replaceDocument = (body,title,cache) ->
+replaceDocument = (body,title) ->
   originalBody = document.body
   document.documentElement.appendChild body, originalBody
   document.documentElement.removeChild originalBody
   document.title = title
-  historyCache[window.history.state.position] = {url:document.location.href,body:body,title:title} if cache
-
+  lastState = window.history.state
 
 
 extractLink = (event) ->

  xhr.send()

fetchHistory = (state) ->
  cache = historyCache[state.position]
  if cache
    replaceDocument cache.body, cache.title
    triggerPageChange
    window.scrollTo(cache.scrollLeft,cache.scrollTop)
  else
    fetchReplacement document.location.href


    visit link.href
    event.preventDefault()

rememberInitialPage = ->
  window.history.replaceState { turbolinks: true, position: window.history.length - 1}, "", document.location.href
  lastState = window.history.state

browserSupportsPushState = window.history and window.history.pushState and window.history.replaceState

if browserSupportsPushState
  rememberInitialPage()
  window.addEventListener 'popstate', (event) ->
    if event.state?.turbolinks
      fetchHistory event.state
