我正在尝试在我的项目中实现django progressive web app,但我在缓存页面方面遇到了一些问题
在chrome中,我得到以下错误
Uncaught (in promise) TypeError: Request failed
下面是相应的代码
serviceworker.js
var staticCacheName = 'djangopwa-v1';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(staticCacheName).then(function(cache) {
return cache.addAll([
'/base_layout'
]);
})
);
});
self.addEventListener('fetch', function(event) {
var requestUrl = new URL(event.request.url);
if (requestUrl.origin === location.origin) {
if ((requestUrl.pathname === '/')) {
event.respondWith(caches.match('/base_layout'));
return;
}
}
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
views.py
...
def base_layout(request):
return render(request, 'main/home.html')
...
url.py
urlpatterns = [
...
#pwa
path('', include('pwa.urls')),
]
任何帮助都将不胜感激
解决方案:
我在this线程上使用了一个解决方案,效果非常好。希望对某人有所帮助:)
serviceworker.js
相关问题 更多 >
编程相关推荐