service worker
This commit is contained in:
parent
fcf191e355
commit
91e9e78650
@ -1,10 +1,15 @@
|
|||||||
|
|
||||||
|
<%
|
||||||
|
pack_assets = [asset_pack_path("application.js"), asset_pack_path("application.css")].select { |a| a.present? }
|
||||||
|
%>
|
||||||
|
|
||||||
var cacheName = "parsley-cache-<%= File.mtime(Webpacker::manifest.config.public_manifest_path).to_i %>";
|
var cacheName = "parsley-cache-<%= File.mtime(Webpacker::manifest.config.public_manifest_path).to_i %>";
|
||||||
|
|
||||||
var staticAssets = [
|
var staticAssets = [
|
||||||
"/",
|
"/"
|
||||||
"<%= asset_pack_path("application.js") %>",
|
<% pack_assets.each do |a| %>
|
||||||
"<%= asset_pack_path("application.css") %>"
|
,"<%= a %>"
|
||||||
|
<% end %>
|
||||||
];
|
];
|
||||||
|
|
||||||
self.addEventListener('install', function(event) {
|
self.addEventListener('install', function(event) {
|
||||||
@ -30,11 +35,33 @@ self.addEventListener('activate', function(event) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', function(event) {
|
self.addEventListener('fetch', function(event) {
|
||||||
event.respondWith(
|
var reqUrl = new URL(event.request.url);
|
||||||
caches.match(event.request).then(function (response) {
|
var x, asset;
|
||||||
return response || fetch(event.request);
|
|
||||||
})
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('message', function (event) {
|
self.addEventListener('message', function (event) {
|
||||||
|
Loading…
Reference in New Issue
Block a user