微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在“缓存”上执行“放置”:不支持请求方法“POST” - PWA Service Worker

如何解决无法在“缓存”上执行“放置”:不支持请求方法“POST” - PWA Service Worker

我正在创建一个带有 react 的 PWA。尽管它通过了灯塔测试并且可以安装,但我一直在控制台中看到这两个错误。当我浏览应用程序时,这些错误被记录了很多次。

Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request method 'POST' is unsupported
        at Cache.put (<anonymous>)
        at worker.js:20

Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request scheme 'chrome-extension' is unsupported
        at Cache.put (<anonymous>)
        at worker.js:20

第 20 行是 cache.put(event.request,resClone);

这是我的 worker.js 文件中的代码

const CACHE_NAME = 'My App';

// Install a service worker
self.addEventListener('install',event => {
    console.log('Opened cache');
});

// Cache and return request
self.addEventListener('fetch',event => {
    event.respondWith(
        fetch(event.request).then(res => {
            const resClone = res.clone();
            caches
                .open(CACHE_NAME)
                .then(cache => {
                    cache.put(event.request,resClone);
                });
            return res;
        }).catch(err => caches.match(event.request).then(res => res))
    );
});

// Update a service worker
self.addEventListener('activate',event => {
    const cacheWhitelist = [];
    cacheWhitelist.push(CACHE_NAME);

    event.waitUntil(
        caches.keys().then((cacheNames) => Promise.all(
            cacheNames.map((cacheName) => {
                if (!cacheWhitelist.includes(cacheName)) {
                    return caches.delete(cacheName);
                }
            })
        ))
    );
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。