为什么404.html在服务工作者缓存期间导致未捕获的错误?

阿里尔·马格巴努阿

我在Web应用程序中实现了service worker,并尝试缓存所有html,css,js和图像文件。我的托管服务是firebase,在成功部署后,我测试了该服务是否可以正常工作以及文件是否将被缓存,不幸的是会发生以下错误。

未捕获(承诺)TypeError:请求失败

service-worker.js

let cacheName = 'my-tools-v1';
let filesToCache = [
    '/',
    '/css/app.css',
    '/css/diffview.css',
    '/css/json-lint.css',
    '/css/materialize.css',
    '/images/favicon.png',
    '/images/icons/apple-touch-icon.png',
    '/images/icons/apple-touch-icon-57x57.png',
    '/images/icons/apple-touch-icon-72x72.png',
    '/images/icons/apple-touch-icon-76x76.png',
    '/images/icons/apple-touch-icon-114x114.png',
    '/images/icons/apple-touch-icon-120x120.png',
    '/images/icons/apple-touch-icon-144x144.png',
    '/images/icons/apple-touch-icon-152x152.png',
    '/images/icons/apple-touch-icon-180x180.png',
    '/images/icons/icon-72x72.png',
    '/images/icons/icon-96x96.png',
    '/images/icons/icon-128x128.png',
    '/images/icons/icon-144x144.png',
    '/images/icons/icon-152x152.png',
    '/images/icons/icon-192x192.png',
    '/images/icons/icon-384x384.png',
    '/images/icons/icon-512x512.png',
    '/js/index.js',
    '/js/css-lint.js',
    '/js/difflib.js',
    '/js/diffview.js',
    '/js/ipsum-generator.js',
    '/js/json2.js',
    '/js/json-lint.js',
    '/js/jsonlint.js',
    '/js/lorem-ipsum.js',
    '/js/materialize.js',
    '/js/visual-difference.js',
    '/bower_components/codemirror/lib/codemirror.js',
    '/bower_components/codemirror/mode/javascript/javascript.js',
    '/bower_components/codemirror/mode/css/css.js',
    '/bower_components/codemirror/addon/edit/matchbrackets.js',
    '/bower_components/codemirror/addon/comment/continuecomment.js',
    '/bower_components/codemirror/addon/comment/comment.js',
    '/bower_components/ft-csslint/dist/csslint.js',
    '/bower_components/jquery/dist/jquery.slim.min.js',
    '/bower_components/kanye-ipsum/dist/jquery.kanye-ipsum.min.js',
    '/bower_components/codemirror/lib/codemirror.css',
    '/index.html',
    '/css-lint.html',
    '/ipsum-generator.html',
    '/json-lint.html',
    '/visual-difference.html',
    '/notfound.html',
    '/404.html'
];

self.addEventListener('install', (e) => {
    console.log('[ServiceWorker] Install');

    e.waitUntil(
        caches.open(cacheName).then((cache) => {
            console.log('[ServiceWorker] Caching app shell');
            return cache.addAll(filesToCache);
        })
    );
});

self.addEventListener('activate', (e) => {
    console.log('[ServiceWorker] Activate');

    e.waitUntil(
        caches.keys().then(function(keyList) {
            return Promise.all(keyList.map((key) => {
                if (key !== cacheName) {
                    console.log('[ServiceWorker] Removing old cache', key);
                    return caches.delete(key);
                }
            }));
        })
    );
});

self.addEventListener('fetch', (e) => {
    console.log('[ServiceWorker] Fetch', e.request.url);

    e.respondWith(
        caches.match(e.request).then((response) => {
            return response || fetch(e.request);
        })
    );
});

然后,因此没有缓存所有文件。但是,如果我删除404.html或将其重命名为其他名称,则服务工作程序将正常运行,并且将缓存所有文件。同样奇怪的是,在我的本地服务器中,服务工作者可以工作并缓存404.html,但是在firebase中失败。

为什么404.html在服务工作者缓存期间导致未捕获的错误?我该如何解决?

亚伯拉罕

Cache.addAll()是全有或全无的API。如果任何响应不在200 HTTP状态码范围内,则不会缓存任何内容。

如果任何资源无法缓存,cache.addAll将拒绝。这意味着仅当cache.addAll中的所有资源都已被缓存时,服务工作者才会安装。

Firebase返回404 Not Found/404.html文件。

解决此问题的方法是拥有一个/notfound.html与您一样的文件,然后在fetch需要时将其返回

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

同时使用GWT火力地堡服务工作者没有找到(404错误)

什么是服务工作者控制台?&Chrome浏览器在哪里?

event.waitUntil在服务工作者中做什么,为什么需要它?

事件处理程序外部的服务工作者中的代码什么时候运行?

为什么TestRestTemplate忽略404客户端错误?

`pushManager.subscribe`和`pushManager.getSubscription`服务工作者之间有什么区别

Chrome DevTools“禁用缓存”:它除了做什么外(对于服务工作者脚本)实际上有什么作用?

WebView:以编程方式清除服务工作者缓存

为什么我的$ resource POST导致404错误?

具有SSL证书缓存的服务工作者

PWA服务工作者缓存/更新预期行为

究竟是什么触发此服务工作者代码运行的?

注销时如何清除服务工作者缓存?

将版本/哈希添加到服务工作者文件后会发生什么?

create-react-app的最佳服务工作者策略是什么?

为什么默认的create-react-app应用程序会使服务工作者立即删除/冗余?

在服务工作者中服务之前,可以编辑缓存的index.html吗?

如何防止服务工作者删除缓存,PWA

如何由服务工作者更新缓存的资源

为什么我在Azure上的代码隐藏出现404错误?

成功注册服务工作者后,为什么没有点击ServiceWorkerRegistration回调?

为什么IIS 8.5 URL重写会引发404错误?

为什么会收到错误代码404?

为什么jboss服务无法启动,为什么我的其余部分出现404错误?

服务工作者从磁盘缓存中缓存资源

服务工作者在反应中缓存的路由

为什么我的 livewire.js 连接出现 404 错误?

服务工作者缓存与 VuexPersistence

服务工作者缓存破坏谷歌地图 api