sgdfj
This commit is contained in:
parent
7b51a92c48
commit
34ffa32571
2 changed files with 33 additions and 11 deletions
|
@ -8,6 +8,7 @@ const URLS_TO_CACHE = [
|
|||
'https://lastfm.freetls.fastly.net/'
|
||||
];
|
||||
|
||||
// Install event: Pre-cache the static resources
|
||||
self.addEventListener('install', function(event) {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
|
@ -17,15 +18,36 @@ self.addEventListener('install', function(event) {
|
|||
);
|
||||
});
|
||||
|
||||
// Fetch event: Serve cached files and dynamically cache .mp3 or .flac files
|
||||
self.addEventListener('fetch', function(event) {
|
||||
const requestUrl = new URL(event.request.url);
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(function(response) {
|
||||
return response || fetch(event.request);
|
||||
})
|
||||
caches.match(event.request).then(function(response) {
|
||||
// If the resource is already cached, return it
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Otherwise, fetch the resource and check if it's an audio file
|
||||
return fetch(event.request).then(function(fetchResponse) {
|
||||
// Clone the response (responses can only be used once)
|
||||
const fetchResponseClone = fetchResponse.clone();
|
||||
|
||||
// Check if the file ends with .mp3 or .flac
|
||||
if (requestUrl.pathname.endsWith('.mp3') || requestUrl.pathname.endsWith('.flac')) {
|
||||
caches.open(CACHE_NAME).then(function(cache) {
|
||||
cache.put(event.request, fetchResponseClone);
|
||||
});
|
||||
}
|
||||
|
||||
return fetchResponse;
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Activate event: Clean up old caches
|
||||
self.addEventListener('activate', function(event) {
|
||||
event.waitUntil(
|
||||
caches.keys().then(function(cacheNames) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue