我一直在尝试使用服务工作者和django-pwa构建渐进的Django Web应用程序。一切似乎都可以正常工作,但是每当我加载页面,以Chrome打开离线模式,然后重新加载,我会收到此错误:
" https://haverford.getdibs.apps.app/login/?redirect_to =/dashboard/"的获取版本产生了网络错误响应:用于重定向模式的请求使用了重定向响应,其重定向模式不是"关注"。
现在,我已经看到其他人在这里遇到了一个看似相似的问题,但是我不太了解答案中发生了什么。我认为我需要将重定向模式设置为"关注"以获取我提出的请求,但我不确定该怎么做。
这是我的服务工作者:
var staticCacheName = "django-pwa-v" + new Date().getTime();
var filesToCache = [
'/offline',
];
// Cache on install
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
// Clear cache on activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => (cacheName.startsWith("django-pwa-")))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))
);
})
);
});
// Serve from Cache
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request);
})
.catch(() => {
return caches.match('offline');
})
)
});
我的印象是我必须以某种方式将事件进行。重新定向模式"关注",但是当我在该行中放一个断点并检查事件时。重新测试对象,其redirect
设置为 'manual'
。我将如何更改它?
我寻求理解实际问题的任何帮助将不胜感激。
谢谢。
非常感谢,sideshowbarker。
方法1在此链接中有助于解决我的问题。
我从此替换了我的安装事件:
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
:
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return fetch('/offline')
.then(response => cache.put('/offline', new Response(response.body)));
})
)
});
当然,现在它仅适用于一个URL端点('/offline'
),但是我想对多个提取请求也相似。