2018-04-17 18:38:48 -05:00
|
|
|
|
2018-04-18 14:00:38 -05:00
|
|
|
<%
|
2018-09-06 18:16:13 -05:00
|
|
|
manifest_data = Webpacker::manifest.refresh
|
2018-09-07 21:56:13 -05:00
|
|
|
manifest_timestamp = File.mtime(Webpacker::config.public_manifest_path).to_i
|
|
|
|
pack_assets = manifest_data.values.select { |asset| asset !~ /\.map$/ }
|
2018-04-18 14:00:38 -05:00
|
|
|
%>
|
|
|
|
|
2018-09-07 21:56:13 -05:00
|
|
|
var cacheName = "parsley-cache-<%= manifest_timestamp %>";
|
2018-04-17 18:38:48 -05:00
|
|
|
|
|
|
|
var staticAssets = [
|
2018-04-18 14:00:38 -05:00
|
|
|
"/"
|
|
|
|
<% pack_assets.each do |a| %>
|
|
|
|
,"<%= a %>"
|
|
|
|
<% end %>
|
2018-04-17 18:38:48 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
self.addEventListener('install', function(event) {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.open(cacheName).then(function (cache) {
|
|
|
|
console.log('[ServiceWorker] Caching app shell');
|
|
|
|
return cache.addAll(staticAssets);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.keys().then(function (keyList) {
|
|
|
|
return Promise.all(keyList.map(function (key) {
|
|
|
|
if (key !== cacheName) {
|
2018-09-07 21:56:13 -05:00
|
|
|
console.log(`[ServiceWorker] Removing old cache: ${key}`);
|
2018-04-17 18:38:48 -05:00
|
|
|
return caches.delete(key);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('fetch', function(event) {
|
2018-04-18 14:00:38 -05:00
|
|
|
var reqUrl = new URL(event.request.url);
|
2018-04-18 17:04:25 -05:00
|
|
|
var isCacheThenNetwork = event.request.headers.get("Cache-Then-Network") === "true";
|
2018-04-18 14:00:38 -05:00
|
|
|
var x, asset;
|
|
|
|
|
2018-09-06 18:16:13 -05:00
|
|
|
// Any non-GET or non-http(s) request should be ignored
|
|
|
|
if (event.request.method !== 'GET' || event.request.url.indexOf('http') !== 0) {
|
2018-09-07 21:56:13 -05:00
|
|
|
return;
|
2018-09-06 18:16:13 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 17:04:25 -05:00
|
|
|
// Cache-first response for static assets
|
2018-04-18 14:00:38 -05:00
|
|
|
for (x = 0; x < staticAssets.length; x++) {
|
|
|
|
asset = staticAssets[x];
|
|
|
|
if (asset === reqUrl.pathname) {
|
|
|
|
event.respondWith(
|
|
|
|
caches.match(event.request).then(function (response) {
|
|
|
|
return response || fetch(event.request);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:04:25 -05:00
|
|
|
// If the 'CacheThenNetwork' header is set, hit the network, cache the page, and return the response
|
|
|
|
if (isCacheThenNetwork) {
|
|
|
|
event.respondWith(caches.open(cacheName).then(function (cache) {
|
|
|
|
return fetch(event.request)
|
|
|
|
.then(function (response) {
|
|
|
|
cache.put(event.request, response.clone());
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}));
|
2018-04-18 14:00:38 -05:00
|
|
|
|
2018-04-18 17:04:25 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network, falling back to cache by default to support offline browsing of any cached resources
|
2018-04-18 14:00:38 -05:00
|
|
|
event.respondWith(caches.open(cacheName).then(function(cache) {
|
|
|
|
return fetch(event.request)
|
|
|
|
.then(function(response) {
|
|
|
|
cache.put(event.request, response.clone());
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch(function() {
|
|
|
|
return caches.match(event.request);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2018-04-17 18:38:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('message', function (event) {
|
|
|
|
if (event.data === "skipWaiting") {
|
|
|
|
self.skipWaiting();
|
|
|
|
}
|
|
|
|
});
|