-
Notifications
You must be signed in to change notification settings - Fork 1
/
turboPatch.js
119 lines (106 loc) · 3.64 KB
/
turboPatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const OldHttpRequest = Turbolinks.HttpRequest;
Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRequest {
constructor(_, location, referrer) {
super();
super(this, location, referrer);
}
requestCompletedWithResponse(response, redirectedToLocation) {
this.response = response;
return(this.redirect = redirectedToLocation);
}
requestFailedWithStatusCode(code) {
return(this.failCode = code);
}
oldSend() {
if (this.xhr && !this.sent) {
this.notifyApplicationBeforeRequestStart();
this.setProgress(0);
this.xhr.send();
this.sent = true;
return(this.delegate, 'requestStarted', o => o.requestStarted());
}
}
send() {
if (this.failCode) {
return(this.delegate.requestFailedWithStatusCode(this.failCode, this.failText));
} else if (this.response) {
return(this.delegate.requestCompletedWithResponse(this.response, this.redirect));
} else {
return(this.oldSend());
}
}
};
Turbolinks.HttpRequest = class HttpRequest {
constructor(delegate, location, referrer) {
const cache = Turbolinks.controller.cache.get(`prefetch${location}`);
if (cache) {
//Turbolinks.controller.cache = new Turbolinks.SnapshotCache 10
Turbolinks.controller.cache.delete(`prefetch${location}`);
console.log(JSON.stringify(Turbolinks.controller.cache.keys));
cache.delegate = delegate;
return cache;
} else {
return(new OldHttpRequest(delegate, location, referrer));
}
}
};
Turbolinks.SnapshotCache.prototype.delete = function(location) {
const key = Turbolinks.Location.wrap(location).toCacheKey();
return(delete this.snapshots[key]);
};
const preloadAttribute = function(link) {
const linkAttr = link.attributes['data-turbolinks-preload']
if (!linkAttr || linkAttr.value === 'false') {
return false;
} else {
return true;
}
}
const isNotGetMethod = function(link) {
link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get')
}
//This function returns true if the link or location shouldn't be
const notPreloadable = function(link, location){
if (preloadAttribute(link) === false) {
return true;
} else if (isNotGetMethod(link)) {
return true;
} else if (location.anchor || location.absoluteURL.endsWith("#")) {
return true;
} else if (location.absoluteURL === window.location.href) {
return true;
} else {
return false;
}
}
const preload = function(event) {
let link = Turbolinks.controller.getVisitableLinkForNode(event.target);
if (link) {
let location = Turbolinks.controller.getVisitableLocationForLink(link);
if (location) {
if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) {
if (notPreloadable(link, location)) {
return;
}
// If Turbolinks has already cached this location internally, use that default behavior
// otherwise we can try and prefetch it here
let cache = Turbolinks.controller.cache.get(location);
if (!cache) {
cache = Turbolinks.controller.cache.get(`prefetch${location}`);
}
if (!cache) {
const request = new Turbolinks.CachedHttpRequest(null, location, window.location);
Turbolinks.controller.cache.put(`prefetch${location}`, request);
return(request.send());
}
}
}
}
};
document.addEventListener("touchstart", preload);
document.addEventListener("mouseover", preload);