-
Notifications
You must be signed in to change notification settings - Fork 2
/
offline.js
122 lines (107 loc) · 4.42 KB
/
offline.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
(function () {
"use strict";
/* global importScripts */
/* global self */
/* global caches */
/* global fetch */
/* global URL */
var projectDir = "/scalajs-offline"
// Include SW cache polyfill this script will be downloaded and cached by default
importScripts(projectDir+"/js/serviceworker-cache-polyfill.js");
//version
var version = "0.0.9"
// Cache name definitions
var cacheNameStatic = "scalajs-offline-"+version;
var cacheNameGithub = "scalajs-offline-github";
var currentCacheNames = [
cacheNameStatic,
cacheNameGithub
];
// A new ServiceWorker has been registered
//self.addEventListener("install", function (event) {
// console.log("Installing SW .. " + new Date())
// event.waitUntil(
// caches.open(cacheNameStatic)
// .then(function (cache) {
// return cache.addAll([
// projectDir+"/index.html",
// projectDir+"/js/scalajs-offline-opt.js",
// projectDir+"/js/scalajs-offline-jsdeps.js"
// ]);
// })
// );
//});
self.addEventListener("install", hola(event))
// New service worker is activated at this point ( now you can safely perform deleting/migrating old cache)
self.addEventListener("activate", function (event) {
event.waitUntil(
caches.keys()
.then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (currentCacheNames.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
// when a network request made from app
self.addEventListener("fetch", function (event) {
var requestURL = new URL(event.request.url);
if (requestURL.hostname.indexOf(".github.com") > -1) {
event.respondWith(githubAPIResponse(event.request))
} else {
event.respondWith(
caches.match(event.request)
.then(function (response) {
if (response) { // if we cached already just send that response back
return response;
}
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then( // if not cached get from network return it and then cache it
function (response) {
var shouldCache = false;
if (response.type === "basic" && response.status === 200) {
shouldCache = cacheNameStatic;
}
if (shouldCache) {
var responseToCache = response.clone();
caches.open(shouldCache)
.then(function (cache) {
var cacheRequest = event.request.clone();
cache.put(cacheRequest, responseToCache);
});
}
return response;
}
);
})
);
}
});
/**
* get scalajs github repos.
* @param request
* @returns {*}
*/
function githubAPIResponse(request) {
if (request.headers.get('Accept') == 'x-cache/only') {
return caches.match(request).then(function(response) {
return response;
});
} else {
return fetch(request.clone()).then(function(response) {
if(response.status === 200 ) { // only cache successful responses , as github supports CORS we can check status of response for non supported cors origins we'll get opaque response
return caches.open(cacheNameGithub).then(function(cache) {
cache.put(request,response.clone())
return response;
});
} else {
return response;
}
});
}
}
})();