-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-js.js
111 lines (111 loc) · 4.54 KB
/
cache-js.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
/*
* © Aymane Taibi 2017
* github/nimda95/cacheres
* This script uses the HTML5 localStorage API to cache css and js files instead of the normal browser cache.
*/
var cacheJs = {
config: null,
loadedCounter: 0,
resourcesCounter: 0,
initializeDone: function() {
console.info('cacheJs finished loading all files');
cacheJs.config.onFinished();
},
initialize: function(config) {
cacheJs.getConfig(config);
},
getConfig: function(config) {
var localStorageConfig = localStorage.getItem('cacheJs.localStorageConfig');
cacheJs.config = config;
if (localStorageConfig === null) {
localStorage.setItem('cacheJs.localStorageConfig', JSON.stringify(cacheJs.config));
cacheJs.checkFiles(cacheJs.config, cacheJs.config);
} else {
cacheJs.checkFiles(cacheJs.config, JSON.parse(localStorageConfig));
}
},
checkFiles: function(newConfig, oldConfig) {
for (var i in newConfig.files) {
if (newConfig.files.hasOwnProperty(i)) {
cacheJs.resourcesCounter++;
}
}
for (var i in newConfig.files) {
if (newConfig.files[i] !== undefined) {
if (oldConfig !== {} && oldConfig.files[i] !== undefined) {
if (localStorage.getItem(i) !== null && oldConfig.files[i].version === newConfig.files[i].version && newConfig.files[i].version !== "-") {
cacheJs.checkIfLoaded();
} else {
cacheJs.getFile(i, newConfig.files[i]);
}
} else {
cacheJs.getFile(i, newConfig.files[i]);
}
} else {
if (newConfig.files[i] !== undefined && localStorage.getItem(i) !== null) {
cacheJs.getFile(i, newConfig.files[i]);
}
}
}
localStorage.setItem('cacheJs.localStorageConfig', JSON.stringify(newConfig));
},
injectFile: function(name, resource) {
var resourceTag = document.createElement(resource.type);
resourceTag.innerHTML = localStorage.getItem(name);
for (var i in resource.attributes) {
if (resource.attributes[i] !== undefined) {
resourceTag.setAttribute(resource.attributes[i].name, resource.attributes[i].value);
}
}
document.head.appendChild(resourceTag);
},
checkIfLoaded: function() {
cacheJs.loadedCounter++;
if (cacheJs.loadedCounter === cacheJs.resourcesCounter) {
cacheJs.injectAllFiles();
}
},
getFile: function(name, resource) {
var x = new XMLHttpRequest();
x.onreadystatechange = function() {
if (x.readyState === 4) {
if ([304, 302, 200, 201, 202].indexOf(x.status) !== -1) {
localStorage.setItem(name, "/*" + name + "*/\n" + x.responseText);
cacheJs.checkIfLoaded();
} else if (x.status === 0) {
var resourceTag = document.createElement(resource.type);
switch (resource.type) {
case 'style':
resourceTag.setAttribute('href', resource.uri);
resourceTag.setAttribute('rel', 'stylesheet');
break;
case 'script':
resourceTag.setAttribute('src', resource.uri);
break;
default:
console.warn('Unknown resource type : ' + resource.type);
}
resourceTag.onload = function() {
cacheJs.checkIfLoaded();
}
for (var i in resource.attributes) {
if (resource.attributes[i] !== undefined) {
resourceTag.setAttribute(resource.attributes[i].name, resource.attributes[i].value);
}
}
document.head.appendChild(resourceTag);
} else {
console.warn('Unable to load Resource : ' + name + '(response code ' + x.status + ')', resource.uri);
}
}
}
x.open('GET', resource.uri, true);
x.send();
},
injectAllFiles: function() {
for (var i in cacheJs.config.files) {
cacheJs.injectFile(i, cacheJs.config.files[i]);
}
cacheJs.initializeDone();
}
}