74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
const CACHE_NAME = 'minecraft-web-v1';
|
|
const ASSETS_TO_CACHE = [
|
|
'./',
|
|
'./index.html',
|
|
'./index.js',
|
|
'./index.wasm',
|
|
'./mc_platform.js',
|
|
'./manifest.json'
|
|
];
|
|
|
|
// Install event: cache core assets
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(ASSETS_TO_CACHE);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Activate event: clean up old caches
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event: Network falling back to cache
|
|
self.addEventListener('fetch', event => {
|
|
// Only intercept GET requests, and skip extensions like browser-sync if running locally
|
|
if (event.request.method !== 'GET') return;
|
|
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then(response => {
|
|
// Check if we received a valid response
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// IMPORTANT: Clone the response. A response is a stream
|
|
// and because we want the browser to consume the response
|
|
// as well as the cache consuming the response, we need
|
|
// to clone it so we have two streams.
|
|
var responseToCache = response.clone();
|
|
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
})
|
|
.catch(() => {
|
|
// If network fails, fallback to cache
|
|
return caches.match(event.request).then(response => {
|
|
if (response) {
|
|
return response;
|
|
}
|
|
// Could return a fallback offline page here if needed
|
|
});
|
|
})
|
|
);
|
|
});
|