两次尝试:
修改现有标头:
self.addEventListener('fetch', function (event) {
event.request.headers.set("foo", "bar");
event.respondWith(fetch(event.request));
});
Run Code Online (Sandbox Code Playgroud)
失败了Failed to execute 'set' on 'Headers': Headers are immutable.
创建新Request对象:
self.addEventListener('fetch', function (event) {
var req = new Request(event.request, {
headers: { "foo": "bar" }
});
event.respondWith(fetch(req));
});
Run Code Online (Sandbox Code Playgroud)
失败了 Failed to construct 'Request': Cannot construct a Request with a Request whose mode is 'navigate' and a non-empty RequestInit.
(另请参阅如何更改响应的标头?)
嘿伙计们,我这个问题一直在苦苦挣扎,所以我决定发帖.
我在我的应用程序上安装了一个服务工作者,它安装得很好,激活得很好,缓存也可以.
但是当我点击一个302的页面完成缓存时,它会告诉我:
" http:// localhost:8000/form / " 的FetchEvent 导致网络错误响应:重定向响应用于重定向模式不是"跟随"的请求.
我已经阅读了很多关于这个主题的内容,我在这里查阅了帖子:服务工作者打破301重定向,然后https://github.com/w3c/ServiceWorker/issues/737 和https:// github. COM/GoogleChromeLabs/SW-预缓存/问题/ 220
据我所知,提取时的默认重定向模式是{redirect:"follow"},但是当我从重定向页面查看重定向模式时,我可以看到它是{redirect:"manual"}所以基本上我必须要做什么这是"手动".
以为我有点困惑,我正在努力如何在我的代码中实现这一点.
如果你们可以帮助我,你可以节省我的一天.
非常感谢你 !
这是我的代码:
const STATIC_CACHE_NAME = 'exell-static-v28';
const DYNAMIC_CACHE_NAME = 'exell-dynamic-v4';
// INSTALLING THE SERVICE WORKER AND PRECACHING APPSHELL
self.addEventListener('install', function(event) {
console.log('[Service Worker] Service Worker installed');
event.waitUntil(
caches.open(STATIC_CACHE_NAME) // Create a static cache
.then(function(cache) {
console.log('[Service Worker] Precaching App Shell');
cache.addAll([ // Add static files to the cache
'/',
'/build/app.js',
'/build/global.css',
'login',
'logout',
'offline',
'form/',
'form/new/first_page',
'form/new/second_page', …Run Code Online (Sandbox Code Playgroud)