我在爲我的網站設置服務人員時遇到了一些問題。避免與服務人員緩存start_url
我只想緩存css/js/fonts和一些images/svg,我不想緩存HTML,因爲它全部都是每分鐘更新一次。
它有點作品,但嘗試在我的智能手機上,即使我已經添加它,我仍然收到「添加到主屏幕」的通知。在Chrome Dev應用程序中,我沒有收到添加按鈕。
還與燈塔我收到以下錯誤:
「用戶不會被提示安裝Web應用程序,故障「不與200下線時迴應」:清單START_URL不由服務人員緩存。「
現在我的sw.js就是這樣。正如你所看到的,我評論了fetch部分,因爲它正在緩存HTML並且Cookies也不起作用。
圍繞一個簡單的服務人員「模板」使用嗎?
const PRECACHE = 'app-name';
const RUNTIME = 'runtime';
// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'/css/file.css',
'/js/file.js',
'/images/logo.png',
'/fonts/roboto/Roboto-Regular.woff2'
]
// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
const currentCaches = [PRECACHE, RUNTIME];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});
// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
// Skip cross-origin requests, like those for Google Analytics.
// if (event.request.url.startsWith(self.location.origin)) {
// event.respondWith(
// caches.match(event.request).then(cachedResponse => {
// if (cachedResponse) {
// return cachedResponse;
// }
// return caches.open(RUNTIME).then(cache => {
// return fetch(event.request).then(response => {
// // Put a copy of the response in the runtime cache.
// return cache.put(event.request, response.clone()).then(() => {
// return response;
// });
// });
// });
// })
// );
// }
});