mirror of
https://github.com/chinchang/web-maker.git
synced 2025-08-02 11:30:22 +02:00
build: import virtual file handling sw in main sw
This commit is contained in:
44
src/virtual-file-service-worker.js
Normal file
44
src/virtual-file-service-worker.js
Normal file
@@ -0,0 +1,44 @@
|
||||
self.addEventListener('fetch', function(event) {
|
||||
// console.log("fetch event", event.request.url);
|
||||
if (event.request.url.indexOf('/user/') !== -1) {
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(response => {
|
||||
// console.log("responding with ", response);
|
||||
if (response !== undefined) {
|
||||
return response;
|
||||
}
|
||||
return '';
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
function getContentType(url) {
|
||||
if (url.match(/\.html$/)) {
|
||||
return 'text/html; charset=UTF-8';
|
||||
} else if (url.match(/\.css$/)) {
|
||||
return 'text/css; charset=UTF-8';
|
||||
}
|
||||
if (url.match(/\.js$/)) {
|
||||
return 'application/javascript; charset=UTF-8';
|
||||
}
|
||||
return 'text/html; charset=UTF-8';
|
||||
}
|
||||
self.addEventListener('message', function(e) {
|
||||
// console.log("message aya sw main", e.data);
|
||||
caches.open('webmaker-files').then(function(cache) {
|
||||
for (const url in e.data) {
|
||||
if (Object.prototype.hasOwnProperty.call(e.data, url)) {
|
||||
// console.log('Received data', url, e.data[url])
|
||||
cache.put(
|
||||
url,
|
||||
new Response(e.data[url], {
|
||||
headers: {
|
||||
'Content-Type': getContentType(url)
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user