激活服务工作者后重新发送请求

问题描述

我在asp核心中使用Service Worker,因此感到困惑,因为当Service Worker激活并且我在浏览器请求中按F12键时,在离线模式或在线模式下都没有问题!!!!!!

我更换了服务人员,但是又发生了。

这是我的服务人员:

(function () {
    'use strict';
    var version = 'v1.0::NetworkFirst';
    var offlineUrl = "/Home/Offline";
    function updateStaticCache() {
        return caches.open(version)
            .then(function (cache) {
                return cache.addAll([
                    offlineUrl,'/','/Home/Index','/Home/Contact','/Home/About'
                ]);
            });
    }
    function addToCache(request,response) {
        if (!response.ok)
            return;
        var copy = response.clone();
        caches.open(version)
            .then(function (cache) {
                cache.put(request,copy);
            });
    }
    self.addEventListener('install',function (event) {
        event.waitUntil(updateStaticCache());
    });
    self.addEventListener('activate',function (event) {
        event.waitUntil(
            caches.keys()
                .then(function (keys) {
                    return Promise.all(keys
                        .filter(function (key) {
                            return key.indexOf(version) !== 0;
                        })
                        .map(function (key) {
                            return caches.delete(key);
                        })
                    );
                })
        );
    });
    self.addEventListener('fetch',function (event) {
        var request = event.request;
        if (request.method !== 'GET') {
            event.respondWith(
                fetch(request)
                    .catch(function () {
                        return caches.match(offlineUrl);
                    })
            );
            return;
        }
        event.respondWith(
            fetch(request)
                .then(function (response) {
                    addToCache(request,response);
                    return response;
                })
                .catch(function () {
                    return caches.match(request)
                        .then(function (response) {
                            return response || caches.match(offlineUrl);
                        })
                        .catch(function () {                           
                        });
                })
        );
    });
})();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)