Created
April 14, 2024 20:25
-
-
Save 443pablo/6f512a3d788159fe83a94fd5d674c82c to your computer and use it in GitHub Desktop.
simple service worker that caches all URLs that are fetced in a whitelist
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 RUNTIME = "RUNTIMENAME" | |
const HOSTNAME_WHITELIST = [ | |
self.location.hostname, | |
] | |
// The Util Function to hack URLs of intercepted requests | |
const getFixedUrl = (req) => { | |
let now = Date.now() | |
let url = new URL(req.url) | |
url.protocol = self.location.protocol | |
if (url.hostname === self.location.hostname) { | |
url.search += (url.search ? "&" : "?") + "cache-bust=" + now | |
} | |
return url.href | |
} | |
self.addEventListener("activate", event => { | |
event.waitUntil(self.clients.claim()) | |
}) | |
self.addEventListener("fetch", event => { | |
// Skip some of cross-origin requests, like those for Google Analytics. | |
if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) { | |
try { | |
const cached = caches.match(event.request) | |
const fixedUrl = getFixedUrl(event.request) | |
const fetched = fetch(fixedUrl, {cache: "no-store"}) | |
const fetchedCopy = fetched.then(resp => resp.clone()) | |
event.respondWith( | |
Promise.race([fetched.catch(), cached]) | |
.then(resp => resp || fetched) | |
) | |
// Update the cache with the version we fetched (only for ok status) | |
event.waitUntil( | |
Promise.all([fetchedCopy, caches.open(RUNTIME)]) | |
.then(([response, cache]) => response.ok && cache.put(event.request, response)) | |
) | |
} catch { | |
//oh well, the network probably was offline | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment