This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener("install", async e => { | |
self.skipWaiting(); | |
e.waitUntil(cacheResources()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CACHE_NAME = "cache-1577110930975"; | |
const urlsToCache = [ | |
'/', | |
'/css/main.css', | |
'/js/main.js', | |
'js/9912a73a702eb9bdf8e0.js', | |
'js/234j2h3g4jh3g234gj32.js', | |
'css/g312321gjhg123j2112x.css' | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { src, dest, series } = require("gulp"); | |
const minify = require("gulp-minify"); | |
const template = require("gulp-template"); | |
const copyServiceWorker = () => | |
src("./src/sw.js") | |
.pipe(minify()) | |
.pipe(dest("dist/")) | |
.pipe(template({ buildVersion: +new Date() })) | |
.pipe(dest("./dist")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CACHE_NAME = "cache-<%= buildVersion %>"; | |
const urlsToCache = ['/', '/css/main.css', '/js/main.js', <%= buildFiles %>]; | |
self.addEventListener("install", async e => { | |
self.skipWaiting(); | |
e.waitUntil(cacheResources()); | |
}); | |
const cacheResources = async () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener("activate", e => { | |
e.waitUntil(clearOldCache()); | |
}); | |
const clearOldCache = async () => { | |
const cacheNames = await caches.keys(); | |
const oldCacheName = cacheNames.find(name => name !== CACHE_NAME); | |
caches.delete(oldCacheName); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('activate', function (event) { | |
event.waitUntil( | |
caches.keys().then(function (cacheNames) { | |
cacheNames.map(function (cacheName) { | |
if (cacheName.indexOf(cacheVersion) < 0) { | |
return caches.delete(cacheName); | |
} | |
}); | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CACHE_NAME = 'cache-v1'; | |
const urlsToCache = ['/', '/css/main.css', '/js/main.js']; | |
self.addEventListener("install", async e => { | |
e.waitUntil(cacheResources()); | |
}); | |
const cacheResources = async () => { | |
const cache = await caches.open(CACHE_NAME); | |
return cache.addAll(urlsToCache); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener("fetch", e => { | |
e.respondWith(getResponseByRequest(e.request)); | |
}); | |
const getResponseByRequest = async request => { | |
const cache = await caches.open(CACHE_NAME); | |
const cachedResponse = await cache.match(request); | |
return cachedResponse || fetch(request); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('fetch', function(event) { | |
event.respondWith( | |
caches.open(CACHE_NAME).then(function(cache) { | |
return cache.match(event.request).then(function (response) { | |
return response || fetch(event.request).then(function(response) { | |
return response; | |
}); | |
}); | |
}) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
navigator.serviceWorker.register('/sw.js').catch(console.log); | |
</script> |
NewerOlder