我正在使用来自
Progressive Web app tutorial by Google
但我得到了一个错误:
Uncaught (in promise) TypeError:
Failed to execute 'clone' on 'Response':
Response body is already used
该网站使用第三方的javascript和样式表作为Web字体。
我想将托管在这些cdn上的资产添加到脱机缓存中。
addEventListener("fetch", function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request).then(function(response) {
var hosts = [
"https://fonts.googleapis.com",
"https://maxcdn.bootstrapcdn.com",
"https://cdnjs.cloudflare.com"
];
hosts.map(function(host) {
if (e.request.url.indexOf(host) === 0) {
caches.open(CACHE_NAME).then(function(cache) {
cache.put(e.request, response.clone());
});
}
});
return response;
});
})
);
});
这些是在流行的cdn上托管的,所以我的预感是他们应该为cors头做正确的事情。
以下是HTML中我要缓存的资产:
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css?family=Merriweather:900,900italic,300,300italic">
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css?family=Lato:900,300" rel="stylesheet">
<link rel="stylesheet" type="text/css"
href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
根据控制台日志,服务工作者是
尝试
要获取这些资产:
Fetch finished loading:
GET "https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css".
sw.js:32
Fetch finished loading:
GET "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML".
sw.js:32
Fetch finished loading:
GET "https://fonts.googleapis.com/css?family=Merriweather:900,900italic,300,300italic".
sw.js:32
Fetch finished loading:
GET "https://fonts.googleapis.com/css?family=Lato:900,300".
sw.js:32
Fetch finished loading:
GET "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/config/TeX-AMS-MML_HTMLorMML.js?V=2.7.1".
sw.js:32
Fetch finished loading:
GET "https://maxcdn.bootstrapcdn.com/font-awesome/latest/fonts/fontawesome-webfont.woff2?v=4.7.0".
sw.js:32
如果我删除
clone
,如中建议的
Why does fetch request have to be cloned in service worker?
,我会得到同样的错误:
TypeError: Response body is already used
如果我添加
{ mode: "no-cors" }
到取货处
Service worker CORS issue
,我将得到相同的错误和这些警告:
The FetchEvent for
"https://maxcdn.bootstrapcdn.com/font-awesome/latest/fonts/fontawesome-webfont.woff2?v=4.7.0"
resulted in a network error response: an "opaque" response was
used for a request whose type is not no-cors
The FetchEvent for
"https://fonts.gstatic.com/s/lato/v14/S6u9w4BMUTPHh50XSwiPGQ3q5d0.woff2"
resulted in a network error response: an "opaque" response was
used for a request whose type is not no-cors
The FetchEvent for
"https://fonts.gstatic.com/s/lato/v14/S6u9w4BMUTPHh7USSwiPGQ3q5d0.woff2"
resulted in a network error response: an "opaque" response was
used for a request whose type is not no-cors
The FetchEvent for
"https://fonts.gstatic.com/s/merriweather/v19/u-4n0qyriQwlOrhSvowK_l521wRZWMf6hPvhPQ.woff2"
resulted in a network error response: an "opaque" response was
used for a request whose type is not no-cors
我可以将这些资产添加到服务工作者的静态缓存中
install
事件,但我有理由仅在
fetch
事件。