Last active
September 23, 2019 19:22
-
-
Save glennfu/936548b58091f284dbb5c1a769130bfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OldHttpRequest = Turbolinks.HttpRequest | |
class Turbolinks.CachedHttpRequest extends Turbolinks.HttpRequest | |
constructor: (_, location, referrer) -> | |
super(this, location, referrer) | |
requestCompletedWithResponse: (response, redirectedToLocation) -> | |
@response = response | |
@redirect = redirectedToLocation | |
requestFailedWithStatusCode: (code) -> | |
@failCode = code | |
oldSend: -> | |
if @xhr and not @sent | |
@notifyApplicationBeforeRequestStart() | |
@setProgress(0) | |
@xhr.send() | |
@sent = true | |
@delegate?.requestStarted?() | |
send: () -> | |
if @failCode | |
@delegate.requestFailedWithStatusCode(@failCode, @failText) | |
else if @response | |
@delegate.requestCompletedWithResponse(@response, @redirect) | |
else | |
@oldSend() | |
class Turbolinks.HttpRequest | |
constructor: (delegate, location, referrer) -> | |
cache = Turbolinks.controller.cache.get("prefetch" + location) | |
if cache | |
Turbolinks.controller.cache.delete("prefetch" + location) | |
cache.delegate = delegate | |
return cache | |
else | |
return new OldHttpRequest(delegate, location, referrer) | |
Turbolinks.Cache::delete = (location) -> | |
key = Turbolinks.Location.wrap(location).toCacheKey() | |
delete @snapshots[key] | |
preload = (event) -> | |
if link = Turbolinks.controller.getVisitableLinkForNode(event.target) | |
if location = Turbolinks.controller.getVisitableLocationForLink(link) | |
if Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location) | |
if (method = link.attributes["data-method"])? && method.value != 'get' | |
return | |
if location.anchor or location.absoluteURL.endsWith("#") | |
return | |
if location.absoluteURL == window.location.href | |
return | |
# If Turbolinks has already cached this location internally, use that default behavior | |
# otherwise we can try and prefetch it here | |
cache = Turbolinks.controller.cache.get(location) | |
if !cache | |
cache = Turbolinks.controller.cache.get("prefetch" + location) | |
if !cache | |
request = new Turbolinks.CachedHttpRequest(null, location, window.location) | |
Turbolinks.controller.cache.put("prefetch" + location, request) | |
request.send() | |
document.addEventListener("touchstart", preload) | |
document.addEventListener("mouseover", preload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed to change
Turbolinks.Cache
toTurbolinks.SnapshotCache
when using Turbolinks 5.0