Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[offline][PWA]ServiceWorker example code #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion pwa-sw.js
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
self.addEventListener("fetch", function(e) {}); /* not actually caching anything. just a dummy service worker so we can install. */
var CACHE_PREX = 'webretro';
var CACHE_NAME = CACHE_PREX+'v1'; //if you have any version update change here
var CACHE_PATH = serviceWorker.scriptURL.split('/').slice(0,-1).join('/')+'/';
var urlsToCache = [ //set some cache path or file,but it not important you can not set it ,change "fetch(event)"
//Specific url path
'index.html',
'',
];
Object.entries(
{
install(event){
console.log('serviceWorker install');
return self.skipWaiting();//跳过等待
},
activate(event){
//delete Specific url: const cache = await caches.open(CACHE_NAME);cache.delete(url);
console.log('serviceWorker activate');
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (CACHE_NAME != cacheName&&cacheName.includes(CACHE_PREX)) {
return caches.delete(cacheName);
}
})
);
})
);
},
fetch(event){
return event.respondWith(new Promise(async resolve=>{
var url = event.request.url.replace(CACHE_PATH,''),cacheTime;
var response = await caches.match(event.request);
if(navigator.onLine){
//online
if(response){
//Expired updates:new Date() - Date.parse(response.headers.get('date'))>86400
//update on background:fetch(event.request).then(async res=>await caches.open(CACHE_NAME).put(event.request, res.clone()))
}
if(!response){
//down file
response = await fetch(event.request);
if(urlsToCache.includes(event.request.url.replace(CACHE_PATH,''))){
//if you not set Specific url
//you can if(/assets\//.test(url)) then all in the assets file will cache!
const cache = await caches.open(CACHE_NAME);
console.log('[Service Worker] Caching new resource: ' + url);
//save cache url
cache.put(event.request, response.clone());
}
}
}
resolve(response);

}));
},
message(event){
//set message or postMessage
console.log(event.data);
}
}
).forEach(
entry=>{
self.addEventListener(entry[0],entry[1]);
}
);