如何在 respondWith 中 cancel/modify/alter/replace 请求?
How to cancel/modify/alter/replace a request in respondWith?
我正在使用工作箱并尝试缓存这样的请求:
this.serviceWorker.addEventListener('fetch', (event) => {
const request = event.request;
event.respondWith(
caches.open('cache name')
.then(cache => cache.match(request))
.then(async (cachedResponse) => {
// todo something
如何取消 event.request 并发送我的自定义请求?
感谢您的帮助。
您可以像这样更改或完全更改传出请求:
this.serviceWorker.addEventListener('fetch', (event) => {
const request = event.request;
return event.respondWith(
caches.open('cache name')
.then(cache => cache.match(request))
.then(async (cachedResponse) => {
// create a new request, fetchi it, and return the result
return fetch(new Request(request.url ....))
})
注意我在第三行代码中添加的return语句:)
有关构建新请求的更多信息:
我正在使用工作箱并尝试缓存这样的请求:
this.serviceWorker.addEventListener('fetch', (event) => {
const request = event.request;
event.respondWith(
caches.open('cache name')
.then(cache => cache.match(request))
.then(async (cachedResponse) => {
// todo something
如何取消 event.request 并发送我的自定义请求? 感谢您的帮助。
您可以像这样更改或完全更改传出请求:
this.serviceWorker.addEventListener('fetch', (event) => {
const request = event.request;
return event.respondWith(
caches.open('cache name')
.then(cache => cache.match(request))
.then(async (cachedResponse) => {
// create a new request, fetchi it, and return the result
return fetch(new Request(request.url ....))
})
注意我在第三行代码中添加的return语句:)
有关构建新请求的更多信息: