Добавлено уведомление о обновлении
This commit is contained in:
@@ -8,10 +8,10 @@ self.addEventListener('fetch', event => event.respondWith(onFetch(event)));
|
||||
|
||||
const cacheNamePrefix = 'offline-cache-';
|
||||
const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
|
||||
const offlineAssetsInclude = [ /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/, /\.webmanifest$/ ];
|
||||
const offlineAssetsExclude = [ /^service-worker\.js$/ ];
|
||||
const offlineAssetsInclude = [/\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/, /\.webmanifest$/];
|
||||
// ИСКЛЮЧАЕМ также service-worker-assets.js
|
||||
const offlineAssetsExclude = [/^service-worker\.js$/, /\/service-worker-assets\.js$/];
|
||||
|
||||
// Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'.
|
||||
const base = "/";
|
||||
const baseUrl = new URL(base, self.origin);
|
||||
const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href);
|
||||
@@ -19,7 +19,6 @@ const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.ur
|
||||
async function onInstall(event) {
|
||||
self.skipWaiting();
|
||||
|
||||
// Fetch and cache all matching items from the assets manifest
|
||||
const assetsRequests = self.assetsManifest.assets
|
||||
.filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url)))
|
||||
.filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url)))
|
||||
@@ -30,6 +29,12 @@ async function onInstall(event) {
|
||||
async function onActivate(event) {
|
||||
await self.clients.claim();
|
||||
|
||||
// НОВОЕ: Уведомляем все открытые вкладки о том, что новый SW активирован
|
||||
const clientsList = await self.clients.matchAll();
|
||||
clientsList.forEach(client => {
|
||||
client.postMessage({ type: 'SW_ACTIVATED', version: self.assetsManifest.version });
|
||||
});
|
||||
|
||||
// Delete unused caches
|
||||
const cacheKeys = await caches.keys();
|
||||
await Promise.all(cacheKeys
|
||||
@@ -38,13 +43,16 @@ async function onActivate(event) {
|
||||
}
|
||||
|
||||
async function onFetch(event) {
|
||||
// НОВОЕ: никогда не перехватываем файлы Service Worker
|
||||
const url = event.request.url;
|
||||
if (url.includes('/service-worker.js') || url.includes('/service-worker-assets.js')) {
|
||||
return fetch(event.request);
|
||||
}
|
||||
|
||||
let cachedResponse = null;
|
||||
if (event.request.method === 'GET') {
|
||||
// For all navigation requests, try to serve index.html from cache,
|
||||
// unless that request is for an offline resource.
|
||||
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
|
||||
const shouldServeIndexHtml = event.request.mode === 'navigate'
|
||||
&& !manifestUrlList.some(url => url === event.request.url);
|
||||
&& !manifestUrlList.some(u => u === event.request.url);
|
||||
|
||||
const request = shouldServeIndexHtml ? 'index.html' : event.request;
|
||||
const cache = await caches.open(cacheName);
|
||||
@@ -52,4 +60,4 @@ async function onFetch(event) {
|
||||
}
|
||||
|
||||
return cachedResponse || fetch(event.request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user