I am trying to implement offline capabilities for my web app but I'm experiencing some strange behavior while offline and it's probably something I'm missing since it's reproducible both in Chrome and Firefox.
I'll describe below what the sample app I created does:
MyCache
previously created by the service worker disappears.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vanishing cache storage test</title>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then((registration) => {
console.log('Service worker registered sucessfully:', registration);
})
.catch((error) => {
console.error('Failed to register service worker:', error.stack);
});
});
}
</script>
</head>
<body>
<h1>Hi!</h1>
</body>
</html>
const CACHE_NAME = 'MyCache';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
return cache.add('/');
})
.then(() => {
console.debug(CACHE_NAME, 'created and populated');
})
);
});
self.addEventListener('activate', (event) => {
console.debug('SW activated');
});
Google Chrome Version: 87.0.4280.141 (Official Build) (x86_64) Firefox version: 85.0b5 (64-bit, Developer Edition) OS: macOS v11.1 (Big Sur)
Is this expected behavior? If so, why? 🤔
before going offline: cache is A-OK after refreshing while offline: cache is gone!
The cache content is still there. If you go online and refresh the page, you can see it again (it isn't being redownloaded).
I've filed https://bugs.chromium.org/p/chromium/issues/detail?id=1164309, as storage should be visible in devtools even if the page doesn't load.