ServiceWorker 'fetch' 事件未针对图标触发
ServiceWorker 'fetch' event not triggered for favicons
我正在使用 Service Worker 在我的站点中缓存内容 (https://adhiyan.in)。 Serviceworker 正在正确安装和激活。在 'fetch' 事件处理程序中,如果可用,我将从缓存中提供内容。如果内容在缓存中不可用,我将获取内容并使用相同内容更新缓存。它能够提供资产并按需缓存新资产。
importScripts('/assets/js/serviceworker-cache-polyfill.js');
var CACHE_NAME = 'adhiyan-cache-v1';
var urlsToCache = [
'/',
'/assets/css/app.min.css',
'/assets/js/app.min.js'
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
console.log("Cache hit for '" + event.request.url + "'- return response");
return response;
}
console.log("Cache miss for '" + event.request.url + "'");
// IMPORTANT: Clone the request. A request is a stream and
// can only be consumed once. Since we are consuming this
// once by cache and once by the browser for fetch, we need
// to clone the response
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then(
function(response) {
// Check if we received a valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
console.log("Invalid response for '" + event.request.url + "'");
return response;
}
// Cache only '/assets'
if (event.request.url.indexOf('/assets') == -1) {
console.log("Not an asset '" + event.request.url + "'");
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 2 stream.
var responseToCache = response.clone();
console.log("Updating cache for '" + event.request.url + "'");
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
但是,我看到浏览器在没有触发 'fetch' 事件的情况下下载网站图标。我可以从 Chrome devtools 的网络面板确认行为。有人可以帮助我理解这种行为吗?我是否遗漏了代码中的某些内容?
这是 Chrome 中的错误:https://code.google.com/p/chromium/issues/detail?id=448427
它应该允许 service workers 拦截请求。
我正在使用 Service Worker 在我的站点中缓存内容 (https://adhiyan.in)。 Serviceworker 正在正确安装和激活。在 'fetch' 事件处理程序中,如果可用,我将从缓存中提供内容。如果内容在缓存中不可用,我将获取内容并使用相同内容更新缓存。它能够提供资产并按需缓存新资产。
importScripts('/assets/js/serviceworker-cache-polyfill.js');
var CACHE_NAME = 'adhiyan-cache-v1';
var urlsToCache = [
'/',
'/assets/css/app.min.css',
'/assets/js/app.min.js'
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
console.log("Cache hit for '" + event.request.url + "'- return response");
return response;
}
console.log("Cache miss for '" + event.request.url + "'");
// IMPORTANT: Clone the request. A request is a stream and
// can only be consumed once. Since we are consuming this
// once by cache and once by the browser for fetch, we need
// to clone the response
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then(
function(response) {
// Check if we received a valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
console.log("Invalid response for '" + event.request.url + "'");
return response;
}
// Cache only '/assets'
if (event.request.url.indexOf('/assets') == -1) {
console.log("Not an asset '" + event.request.url + "'");
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 2 stream.
var responseToCache = response.clone();
console.log("Updating cache for '" + event.request.url + "'");
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
但是,我看到浏览器在没有触发 'fetch' 事件的情况下下载网站图标。我可以从 Chrome devtools 的网络面板确认行为。有人可以帮助我理解这种行为吗?我是否遗漏了代码中的某些内容?
这是 Chrome 中的错误:https://code.google.com/p/chromium/issues/detail?id=448427
它应该允许 service workers 拦截请求。