![](/img/trans.png)
[英]Use ReadableStream with Response to return HTML from fetch event of Service Worker
[英].html won't fetch from service worker
我正在努力讓一個簡單的服務人員離線工作。 我嘗試了很多例子都沒有成功。 首先我認為這是因為我使用的是基於動態 php 的網站,或者它可能是我的服務器或我的 htaccess。 這對我來說是個謎。 獲取 index.html 時,一切都在我的本地主機上運行,但在服務器上,即使在分析 chrome devtools 網絡選項卡時正在加載其他所有內容,該頁面也無法離線加載。
所以我試圖在我的服務器上制作這個簡單的 html 離線 PWA,並在另一台服務器上創建相同的源。
https://sw.punchunique.com
https://punchunique.neocities.org/test.html或
https://punchunique.neocities.org/
也許是因為我沒有后備。html 但是如果它已經加載到緩存中,為什么我需要一個
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('mysite-static-v3').then(function(cache) {
return cache.addAll([
'index.html',
'punch-fixed.css',
'general.css',
'punch-homepage.css',
'thesansextralight_plain-webfont.woff',
'thesansextralight_plain-webfont.woff2',
'TweenMax.min.js',
'PLUGINS.js',
'punch.webmanifest',
'sw-demo.js',
'favicon.ico',
'favicon-16x16.png',
'favicon-32x32.png',
'favicon-194x194.png',
'apple-touch-icon.png',
'apple-touch-icon-72x72.png',
'apple-touch-icon-120x120.png',
'apple-touch-icon-144x144.png',
'apple-touch-icon-152x152.png',
'android-chrome-96x96.png',
'android-chrome-192x192.png',
'android-chrome-512x512.png',
'mstile-48x48.png',
'mstile-144x144.png',
'mstile-270x270.png',
'mstile-558x558.png',
'mstile-558x270.png',
'bcg-img-sect1.jpg',
'green-hook.png',
]);
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
// Cache only
// If a match isn't found in the cache, the response
// will look like a connection error
// event.respondWith(caches.match(event.request));
// Network only
// event.respondWith(fetch(event.request));
// or simply don't call event.respondWith, which
// will result in default browser behaviour
// CACHE then NETWORK
event.respondWith(
caches.open('mysite-dynamic').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
離線時我有這個 獲取腳本時發生未知錯誤。 和Uncaught (in promise) TypeError: Failed to fetch
but all the caches files are present on server and nothing is missing。
這是具有 static 和動態緩存的服務工作者的完美工作示例
var CACHE_STATIC_NAME = 'static-v4';
var CACHE_DYNAMIC_NAME = 'dynamic-v2';
self.addEventListener('install', function(event) {
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache) {
console.log('[Service Worker] Precaching App Shell');
cache.addAll([
'/',
'/index.html',
'/src/js/app.js',
'/src/js/feed.js',
'/src/js/promise.js',
'/src/js/fetch.js',
'/src/js/material.min.js',
'/src/css/app.css',
'/src/css/feed.css',
'/src/images/main-image.jpg',
'https://fonts.googleapis.com/css?family=Roboto:400,700',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://cdnjs.cloudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css'
]);
})
)
});
self.addEventListener('activate', function(event) {
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) {
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
} else {
return fetch(event.request)
.then(function(res) {
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache) {
cache.put(event.request.url, res.clone());
return res;
})
})
.catch(function(err) {
});
}
})
);
});
在 fetch 事件中使用 match() function 和 {ignoreVary:true} 作為第二個參數
caches.match(event.request,{ignoreVary:true})
.then(function(response) {....}
它所做的是避免匹配請求的標頭。 由於 header 匹配,您的應用程序將在本地主機上運行,而不是在實時環境中運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.