forked from yuehu/turbolinks
/
index.js
313 lines (257 loc) · 7.15 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Turbolinks in component.
*/
var delegate = require('delegate');
var domparse = require('dom-parser');
var execute = require('execute-script');
var Emitter = require('emitter');
exports = module.exports = new Emitter();
var hasHistoryState = history && history.pushState && history.replaceState && (history.state !== undefined || navigator.userAgent.match(/Firefox\/2[6|7]/));
var buggyBrowsers = navigator.userAgent.match(/CriOS\//);
// TODO: cookie
var isSupported = hasHistoryState && !buggyBrowsers;
var currentState, referrer, cacheStorage = {}, cacheSize = 10;
function visit(url) {
if (!isSupported) return location.href = url;
exports.emit('page:visit', {url: url});
// remember referer
referrer = location.href;
// set cacheSize = 0 to disable cache
if (cacheSize) {
cacheCurrentPage();
}
// reflect new url
if (url !== referrer) {
history.pushState({turbolinks: true, url: url}, '', url);
}
var cachedPage = cacheStorage[url];
if (cachedPage) {
return fetchHistory(cachedPage);
}
return fetch(url, function() {
if (location.hash) {
return location.href = location.href;
} else {
window.scrollTo(0, 0);
}
});
}
/**
* Fetch and render the data.
*/
function fetch(url, cb) {
exports.emit('page:fetch', {url: url});
if (fetch.xhr) {
fetch.xhr.abort();
}
// remove hash for IE10 compatibility
var safeURL = removeHash(url);
fetch.xhr = request(safeURL, function(xhr) {
exports.emit('page:receive');
var doc;
var ct = xhr.getResponseHeader('Content-Type');
if (validContentType(ct) && validStatus(xhr.status)) {
doc = domparse(xhr.responseText);
}
if (!doc) {
return location.href = url;
}
render(doc, true);
// reflect redirected url
var loc = xhr.getResponseHeader('X-XHR-Redirected-To');
if (loc) {
var preservedHash = removeHash(loc) === loc ? document.hash : '';
history.replaceState(currentState, '', loc + preservedHash);
}
cb && cb();
exports.emit('page:load');
});
fetch.xhr.onloadend = function() {
fetch.xhr = null;
};
}
/**
* Fetch from history.
*/
function fetchHistory(page) {
if (request.xhr) {
request.xhr.abort();
}
render(page);
// restore position
window.scrollTo(page.positionX, page.positionY);
exports.emit('page:restore');
}
/**
* Render data to document.
*/
function render(doc, runscript) {
// update title
if (doc.title && doc.title.valueOf()) {
document.title = doc.title;
}
var body = doc.body;
// remove <noscript>
body.innerHTML = body.innerHTML.replace(/<noscript[\S\s]*?<\/noscript>/ig, '');
// update body
document.documentElement.replaceChild(body, document.body);
// update head
updateHead(doc.head);
if (runscript) {
executeScripts(document.body);
}
currentState = history.state;
exports.emit('page:change');
exports.emit('page:update');
}
/**
* Send a GET request.
*/
function request(url, cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader('Accept', 'text/html, application/xhtml+xml, application/xml');
xhr.setRequestHeader('X-XHR-Referer', referrer);
xhr.onload = function() {
cb && cb(xhr);
};
xhr.onerror = function() {
location.href = url;
};
// emit progress data
if (xhr.upload) {
xhr.upload.onprogress = function(e){
e.percent = e.loaded / e.total * 100;
exports.emit('progress', e);
};
}
xhr.send();
return xhr;
}
/**
* Cache current page
*/
function cacheCurrentPage() {
cacheStorage[currentState.url] = {
url: document.location.href,
head: document.head,
body: document.body,
title: document.title,
positionY: window.pageYOffset,
positionX: window.pageXOffset,
cachedAt: new Date().getTime()
};
// limitation on cache size
var cacheKeys = Object.keys(cacheStorage);
var limitAt = cacheKeys.map(function(url) {
return cacheStorage[url].cachedAt;
}).sort(function(a, b) {
return b - a;
})[cacheSize];
cacheKeys.forEach(function(url) {
if (cacheStorage[url].cachedAt < limitAt) {
exports.emit('page:expire', cacheStorage[url]);
delete cacheStorage[url];
}
});
}
/**
* Remove hash on a URL.
*/
function removeHash(url) {
var link = url;
if (!url.href) {
link = document.createElement('A');
link.href = url;
}
return link.href.replace(link.hash, '');
}
/**
* Validate content type of a response.
*/
function validContentType(ct) {
return ct.match(/^(?:text\/html|application\/xhtml\+xml|application\/xml)(?:;|$)/);
}
/**
* Validate response status code.
*/
function validStatus(code) {
return code < 400;
}
function executeScripts(doc) {
var scripts = doc.querySelectorAll('script:not([data-turbolinks-eval="false"])');
for (var i = 0; i < scripts.length; i++) {
execute(scripts[i]);
}
}
function updateHead(head) {
var nodes = head.querySelectorAll('meta');
for (var i = 0; i < nodes.length; i++) {
(function(meta) {
var property = meta.getAttribute('property');
if (!meta.name && !property) return;
var selector;
if (meta.name) {
selector = 'meta[name="' + meta.name + '"]';
} else {
selector = 'meta[property="' + property + '"]';
}
var original = document.head.querySelector(selector);
if (original) {
original.content = meta.content;
} else {
document.head.appendChild(meta);
}
})(nodes[i]);
}
}
// initialize for event
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', function() {
exports.emit('page:change');
exports.emit('page:update');
}, true);
}
// initialize for click
if (isSupported) {
// remember current url
history.replaceState({turbolinks: true, url: location.href}, '', location.href);
// remember current state
currentState = history.state;
delegate.bind(document, 'a', 'click', handleClick, true);
// state change
setTimeout(function() {
window.addEventListener('popstate', function(e) {
if (e.state && e.state.turbolinks) {
var cachedPage = cacheStorage[e.state.url];
if (cachedPage) {
cacheCurrentPage();
fetchHistory(cachedPage);
} else {
visit(e.target.location.href);
}
}
}, false);
}, 500);
}
function handleClick(e) {
if (!e.defaultPrevented) {
var node = e.delegateTarget;
// ignore cross origin link
var crossOriginLink = location.protocol !== node.protocol || location.host !== node.host;
// ignore anchors
var anchoredLink = (node.hash && removeHash(node)) === removeHash(location) || node.href === location.href + '#';
var url = removeHash(node);
var nonHtmlLink = url.match(/\.[a-z]+(\?.*)?$/g) && !url.match(/\.(?:htm|html)?(\?.*)?$/g);
var targetLink = node.target.length !== 0;
var nonStandardClick = e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey;
var ignoreClick = crossOriginLink || anchoredLink || nonHtmlLink || targetLink || nonStandardClick;
if (!ignoreClick) {
visit(node.href);
return e.preventDefault();
}
}
}
exports.cacheSize = cacheSize;
exports.isSupported = isSupported;
exports.visit = visit;