-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
187 lines (154 loc) · 6.29 KB
/
sw.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
function messageAllClients(message) {
return self.clients.matchAll().then(clients => {
clients.forEach(client => client.postMessage(message));
})
}
const cacheName = "rivers.run"
const waitOnFirstLoad = 2500 //Milliseconds to wait before fetching items on preload list. Helps prevent duplicate requests on first load.
//Array of items to try and preload on install (the serviceWorker will install without them preloaded). Can be exact or relative to serviceWorker scope
const preloadList = [
"",
"about.html",
"map.html", //Only load code for map page - not the Google Maps part.
"packages/map.js",
"clubs.html",
"settings.html",
"favorites.html",
"FAQ.html",
"index.css",
"packages/allPages.js",
"packages/index.js",
"packages/favorites.js",
"packages/map.js",
"riverdata.json",
"legal/Privacy Policy.html",
"legal/Terms of Service.html",
"legal/DISCLAIMER.html",
]
function rebaseURL(url) {
//Fills in relative URLs using the serviceWorker scope
return (new URL(url, registration.scope)).href
}
function activateHandler(event) {
event.waitUntil((async function() {
//Allow requests by the page to get into browser cache, so that we don't sent 2 requests for the same thing.
await new Promise((resolve, reject) => {
setTimeout(resolve, waitOnFirstLoad)
})
const cache = await caches.open(cacheName)
let requests = []
for (let index in preloadList) {
let url = rebaseURL(preloadList[index])
requests.push(fetch(url))
}
for (let index in requests) {
let request = requests[index]
try {
let response = await request
await cache.put(response.url, response)
}
catch(e) {
console.error(e)
}
}
})())
}
self.addEventListener("activate", activateHandler)
self.addEventListener("install", function() {
self.skipWaiting()
})
//Milliseconds to wait for network response before using cache
//When set to 0, cached data will be returned immediately, and cache will be updated in background.
const defaultWaitPeriod = 1000
//TODO: Default to network, fallback to cache with Google Maps. Google Maps is throwing errors sometimes for unknown reasons.
function fetchHandler(event) {
event.respondWith((async function(){
let waitperiod = defaultWaitPeriod
let cache = await caches.open(cacheName)
let url = event.request.url
let fromcache = await caches.match(url)
//Use Date header to determine how long to wait - Note that the date header isn't always set.
let age;
if (fromcache) {
age = Date.now() - new Date(fromcache.headers.get("date")).getTime()
}
let returnNetwork = false
let fromnetwork = fetch(event.request)
if (
url.includes("docs.google.com") //Avoid filling up cache with opaque responses from docs.google.com
|| url.includes("googleapis.com") //May want to temporarily cache some images, but we mostly
|| url.includes("node")
|| url.includes("gaugeReadings")
|| url.includes("script.googleusercontent.com") //Don't store the unused results from writeupmaker uploads and submissions.
|| url.includes("ih3.googleusercontent.com")
|| url.includes("tile.openstreetmap.org") //Mapping code - Cached seperately
|| url.includes("mesonet1.agron.iastate.edu") //Weather maps - not cached.
|| url.includes("ggpht.com") //I believe this is street view. Don't cache.
) {
returnNetwork = true
//Override - Cache Google Maps JavaScript
if (url.includes("maps.googleapis.com/maps/api/js?")) {
//Main JavaScript file
returnNetwork = "default"
}
if (url.includes("maps.googleapis.com") && url.includes(".js")) {
//Auxillary JavaScript files.
returnNetwork = "default"
}
}
if (returnNetwork === true) {
return fromnetwork
}
else if (returnNetwork === "default") {
waitperiod = false //Wait for network to error before using cache.
}
else if (age > 60*1000*60*24*1) {
waitperiod = 2500 //If the data is very old, wait a bit longer try and get a new copy.
}
if (!fromcache) {
//No cache. All we can do is return network response
let response = await fromnetwork
await cache.put(url, response.clone())
return response
}
else {
//We have cached data
return new Promise(function(resolve, reject){
let served = 0
//If the fetch event fails (ex. offline), return cached immediately
fromnetwork.catch(function(e){
messageAllClients(url + " errored. Using cache.")
if (!served) {
served = 1
resolve(fromcache)
}
})
//Fetch from network and update cache
fromnetwork.then(function(response){
let otherServed = served
served = 1
cache.put(url, response.clone()).then(() => {
if (otherServed) {
messageAllClients("Updated cache for " + url)
}
else {
messageAllClients(url + " has been loaded from the network")
}
resolve(response)
})
})
//If the network doesn't respond quickly enough, use cached data
if (waitperiod !== false) {
setTimeout(function(){
if (!served) {
served = 1
messageAllClients(url + " took too long to load from network. Using cache")
resolve(fromcache)
}
}, waitperiod)
}
})
}
}()))
}
self.addEventListener("fetch", fetchHandler)