Last active
December 17, 2024 11:58
-
-
Save cferdinandi/6e4a73a69b0ee30c158c8dd37d314663 to your computer and use it in GitHub Desktop.
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
// Core assets | |
let coreAssets = []; | |
// On install, cache core assets | |
self.addEventListener('install', function (event) { | |
// Cache core assets | |
event.waitUntil(caches.open('app').then(function (cache) { | |
for (let asset of coreAssets) { | |
cache.add(new Request(asset)); | |
} | |
return cache; | |
})); | |
}); | |
// Listen for request events | |
self.addEventListener('fetch', function (event) { | |
// Get the request | |
let request = event.request; | |
// Bug fix | |
// https://stackoverflow.com/a/49719964 | |
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') return; | |
// HTML files | |
// Network-first | |
if (request.headers.get('Accept').includes('text/html')) { | |
event.respondWith( | |
fetch(request).then(function (response) { | |
// Create a copy of the response and save it to the cache | |
let copy = response.clone(); | |
event.waitUntil(caches.open('app').then(function (cache) { | |
return cache.put(request, copy); | |
})); | |
// Return the response | |
return response; | |
}).catch(function (error) { | |
// If there's no item in cache, respond with a fallback | |
return caches.match(request).then(function (response) { | |
return response || caches.match('/offline.html'); | |
}); | |
}) | |
); | |
} | |
// CSS & JavaScript | |
// Offline-first | |
if (request.headers.get('Accept').includes('text/css') || request.headers.get('Accept').includes('text/javascript')) { | |
event.respondWith( | |
caches.match(request).then(function (response) { | |
return response || fetch(request).then(function (response) { | |
// Return the response | |
return response; | |
}); | |
}) | |
); | |
return; | |
} | |
// Images | |
// Offline-first | |
if (request.headers.get('Accept').includes('image')) { | |
event.respondWith( | |
caches.match(request).then(function (response) { | |
return response || fetch(request).then(function (response) { | |
// Save a copy of it in cache | |
let copy = response.clone(); | |
event.waitUntil(caches.open('app').then(function (cache) { | |
return cache.put(request, copy); | |
})); | |
// Return the response | |
return response; | |
}); | |
}) | |
); | |
} | |
}); |
Greate content, it really helped me in my PWA
really helpful, but it seems to not be caching .js files for offline use? assume I've messed up somewhere - it seems to handle the .html, .css, .png, and .jpg files fine
@Offbeatmammal I don't think my 'text/javascript'
MIME type check is correct. Browsers either changed how that's served, or I messed up when researching this (more likely the case). Looking right now to see if I can figure out what it should be.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pawan-poudel-github Yes you can, though you might want to add some default assets to
coreAssets
array. But you can drop this right in as-is.