Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django服务工作者无法缓存页面:未捕获(承诺中)类型错误:请求失败_Python_Django_Service_Progressive Web Apps - Fatal编程技术网

Python Django服务工作者无法缓存页面:未捕获(承诺中)类型错误:请求失败

Python Django服务工作者无法缓存页面:未捕获(承诺中)类型错误:请求失败,python,django,service,progressive-web-apps,Python,Django,Service,Progressive Web Apps,我正在尝试在我的项目中实现django progressive web app,但我在缓存页面方面遇到了一些问题 在chrome中,我得到以下错误 Uncaught (in promise) TypeError: Request failed 下面是相应的代码 serviceworker.js var staticCacheName = 'djangopwa-v1'; self.addEventListener('install', function(event) { event.wai

我正在尝试在我的项目中实现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);
      })
    );
});
var staticCacheName = 'djangopwa-v1';

self.oninstall = function (evt) {
    evt.waitUntil(caches.open(staticCacheName).then(function (cache) {
        return Promise.all(['/', 'main/home.html'].map(function (url) {
            return fetch(new Request(url, { redirect: 'manual' })).then(function (res) {
                return cache.put(url, res);
            });
        }));
    }));
};
self.onfetch = function (evt) {
    var url = new URL(evt.request.url);
    if (url.pathname != '/' && url.pathname != 'main/home.html') return;
    evt.respondWith(caches.match(evt.request, { cacheName: staticCacheName }));
};
views.py

...

def base_layout(request):
    return render(request, 'main/home.html')

...
url.py

urlpatterns = [
    ...
    #pwa
    path('', include('pwa.urls')),
]
我遵循了本教程:

任何帮助都将不胜感激

解决方案:

我在线程上使用了一种解决方案,效果非常好。希望对某人有所帮助:)

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);
      })
    );
});
var staticCacheName = 'djangopwa-v1';

self.oninstall = function (evt) {
    evt.waitUntil(caches.open(staticCacheName).then(function (cache) {
        return Promise.all(['/', 'main/home.html'].map(function (url) {
            return fetch(new Request(url, { redirect: 'manual' })).then(function (res) {
                return cache.put(url, res);
            });
        }));
    }));
};
self.onfetch = function (evt) {
    var url = new URL(evt.request.url);
    if (url.pathname != '/' && url.pathname != 'main/home.html') return;
    evt.respondWith(caches.match(evt.request, { cacheName: staticCacheName }));
};