Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active November 5, 2023 15:18
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save cferdinandi/6e4a73a69b0ee30c158c8dd37d314663 to your computer and use it in GitHub Desktop.
Save cferdinandi/6e4a73a69b0ee30c158c8dd37d314663 to your computer and use it in GitHub Desktop.
// 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;
});
})
);
}
});
@Offbeatmammal
Copy link

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

@cferdinandi
Copy link
Author

@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