我正在尝试将服务工作者集成到我的站点中,并且在某种程度上可以运行add to home screen
,并且可以在devTools中看到它正在运行并已注册。但是,当我尝试登录该站点或从该站点注销(或任何其他与post
相关的请求)时,它在控制台中为我提供了此提示,只是返回“该站点无法在浏览器中访问:
Uncaught (in promise) TypeError:
Request method 'POST' is unsupported at
self.addEventListener.event.respondWith.
caches.match.then.caches.open.then.fetch.then.response
请注意,我可以导航至/login
(获取请求),但是当我单击该按钮登录(发送发帖请求)时,会遇到上述问题。
这是我正在使用的Service Worker文件。
const PRECACHE = 'precache-v2';
const RUNTIME = 'runtime';
// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'/',
'/contact',
'/manifest.json',
'/svg/404.svg',
];
// 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.dlete(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(() => {
return response;
});
});
});
})
);
}
});
具体来说,根据控制台错误,在这一行:
return cache.put(event.request, response.clone()).then(() => {return response;});
任何帮助将不胜感激。