首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >worker.js:74 Uncaught (承诺) TypeError:未能在‘Cache’上执行'put‘:请求方法'POST’不受支持

worker.js:74 Uncaught (承诺) TypeError:未能在‘Cache’上执行'put‘:请求方法'POST’不受支持
EN

Stack Overflow用户
提问于 2022-08-04 14:55:12
回答 1查看 227关注 0票数 0

我正在构建PWA,当我在:http://0.0.0.0:10000上运行它时,它按照预期工作。

但是当我在它预定的域上运行它时,我在Chrome中得到了这个错误。火狐很好用。

代码语言:javascript
运行
复制
worker.js:74 Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request method 'POST' is unsupported

在我的模板里

代码语言:javascript
运行
复制
<script>
    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
            navigator.serviceWorker.register('./worker.js');
        });
    }

</script>

我下载了worker.js,希望它能很好地工作。

代码语言:javascript
运行
复制
/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

// Names of the two caches used in this version of the service worker.
// Change to v2, etc. when you update any of the local resources, which will
// in turn trigger the install event again.
const PRECACHE = 'precache-v1';
const RUNTIME = 'runtime';

// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
    './css/custom.css',
    './css/navbar-top-fixed.css',
    './css/bootstrap.min.css',
    './worker.js',
    './js/bootstrap.bundle.min.js',
    './imgs/icon-128x128.png',
    './imgs/apple-touch-icon.png',
    './imgs/ms-touch-icon-144x144-precomposed.png',
    './imgs/chrome-touch-icon-192x192.png',
    './imgs/logo.png',
    './imgs/mascable.png',
    './favicon.ico'
];

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
    event.waitUntil(
        caches.open(PRECACHE)
            .then(cache => cache.addAll(PRECACHE_URLS))
            .then(self.skipWaiting())
    );
});

// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
    const currentCaches = [PRECACHE, RUNTIME];
    event.waitUntil(
        caches.keys().then(cacheNames => {
            return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
        }).then(cachesToDelete => {
            return Promise.all(cachesToDelete.map(cacheToDelete => {
                return caches.delete(cacheToDelete);
            }));
        }).then(() => self.clients.claim())
    );
});

// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
    // Skip cross-origin requests, like those for Google Analytics.
    if (event.request.url.startsWith(self.location.origin)) {
        event.respondWith(
            caches.match(event.request).then(cachedResponse => {
                if (cachedResponse) {
                    return cachedResponse;
                }

                return caches.open(RUNTIME).then(cache => {
                    return fetch(event.request).then(response => {
                        // Put a copy of the response in the runtime cache.
                        return cache.put(event.request, response.clone()).then(() => {//<-- throws error
                            return response;
                        });
                    });
                });
            })
        );
    }
});

有什么线索吗?

EN

回答 1

Stack Overflow用户

发布于 2022-08-23 13:51:30

您应该检查列表中的每一项是否存在,并且是正确的url。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73238081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档