- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直收到这个错误:
Uncaught (in promise) DOMException: Failed to execute 'respondWith' on 'FetchEvent': The event has already been responded to.
我知道如果异步内容在获取函数中发生,服务 worker 会自动响应,但我不能完全弄清楚这段代码中的哪一位是违规者:
importScripts('cache-polyfill.js');
self.addEventListener('fetch', function(event) {
var location = self.location;
console.log("loc", location)
self.clients.matchAll({includeUncontrolled: true}).then(clients => {
for (const client of clients) {
const clientUrl = new URL(client.url);
console.log("SO", clientUrl);
if(clientUrl.searchParams.get("url") != undefined && clientUrl.searchParams.get("url") != '') {
location = client.url;
}
}
console.log("loc2", location)
var url = new URL(location).searchParams.get('url').toString();
console.log(event.request.hostname);
var toRequest = event.request.url;
console.log("Req:", toRequest);
var parser2 = new URL(location);
var parser3 = new URL(url);
var parser = new URL(toRequest);
console.log("if",parser.host,parser2.host,parser.host === parser2.host);
if(parser.host === parser2.host) {
toRequest = toRequest.replace('https://booligoosh.github.io',parser3.protocol + '//' + parser3.host);
console.log("ifdone",toRequest);
}
console.log("toRequest:",toRequest);
event.respondWith(httpGet('https://cors-anywhere.herokuapp.com/' + toRequest));
});
});
function httpGet(theUrl) {
/*var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;*/
return(fetch(theUrl));
}
如有任何帮助,我们将不胜感激。
最佳答案
问题是您对 event.respondWith()
的调用是在您的顶级 promise 的 .then()
子句中,这意味着它将在之后异步执行顶级 promise 解决。为了获得您期望的行为,event.respondWith()
需要作为 fetch
事件处理程序执行的一部分同步执行。
你的 promise 中的逻辑有点难以理解,所以我不确定你想要完成什么,但一般来说你可以遵循这种模式:
self.addEventListerner('fetch', event => {
// Perform any synchronous checks to see whether you want to respond.
// E.g., check the value of event.request.url.
if (event.request.url.includes('something')) {
const promiseChain = doSomethingAsync()
.then(() => doSomethingAsyncThatReturnsAURL())
.then(someUrl => fetch(someUrl));
// Instead of fetch(), you could have called caches.match(),
// or anything else that returns a promise for a Response.
// Synchronously call event.respondWith(), passing in the
// async promise chain.
event.respondWith(promiseChain);
}
});
这是一般的想法。 (如果您最终用 async
/await
替换 promise,代码看起来会更干净。)
关于javascript - 服务 worker 错误 : event already responded to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46838778/
如何修复 Excel VBA 中的 Not Responding 错误? 我的代码是完美的,我没有收到任何错误,但在我运行它之后,我会看到 Excel VBA 的NOT RESPONDING 顶部并且
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Keep app responsive during long task 在处理大型查询或长流程循环时如何避免“无响
这是我第一次遇到有人试图侵入我创建的网站。我可以做什么来阻止这些尝试? 顺便说一句,他们的 SQL 注入(inject)不可能起作用,而且我们拥有的任何数据都不能被正常使用该网站的任何人使用。 附加:
我的 java 进程停止响应。我尝试 jstack 但失败并出现以下错误。 21039: Unable to open socket file: target process not respondi
This question already has answers here: Closed 7 years ago. Possible Duplicate: Dismiss keyboard on
我不确定这是否重复,我尝试过的可能的补救措施无效。 (下面会提到) 我目前正在为我正在做的项目使用 Theme.AppCompat.NoActionBar 并且正在使用 android.support
这是一个简单的用户登录。我只是想了解这个概念。让我解释一下我的代码问题。这是我的表单: User: Password: check_login.php: Use
我正在创建一个线程,它可以控制所有程序过程消息并且不会卡住程序。我写了this code我得到了 program not responding。 有没有人有使用线程的经验可以帮助我? 最佳答案 Win
我将 Selenium 与 Python 结合使用。单击按钮时,我在 widows Python 中收到一条消息: Not responding in widows Python I have the
我有一个长时间运行的方法,必须在 UI 线程上运行。(Devex - gridView.CopyToClipboard()) 我不需要 UI 在复制时响应,我添加了启动画面,这样用户就不会感到无聊。
对于测试,使程序显示“无响应”的最简单方法是什么?中断 Windows 消息或将其置于无限循环中? 我试过一个简单的循环 while(true) 但这行不通。测试应用程序是 C# 控制台应用程序。它在
App里发生的最糟糕的事是弹出应用无响应”Application Not Responding” (ANR) 对话框.本课讲的是如何保持应用响应,避免ANR。 什么触发ANR 通常,系统会在应用
我们遇到一个客户运行我们基于 WPF 的程序的问题。他可以启动应用程序、创建新文档、在 TreeView 中导航菜单项以查看文档的不同部分、更改几乎(如果不是全部)我们的文本框值、将项目添加到列表(使
在VCL主线程中,我运行一个脚本,该脚本可能需要几分钟才能执行(这是一个还原数据库脚本)。 如果用户单击UI,则我的标题为“无响应”。 Application.ProcessMessages不起作用。
我这里有一个无意义的小脚本,我正在 MATLAB R2013b 中执行它: clear all; n = 2000; times = 50; i = 0; tCPU = tic; disp 'CPU:
我想将验证错误发送回另一个页面(添加),因此我的save操作具有以下功能: @Transactional(readOnly = false) def save(AddDomainCommand com
我正在构建一个 JavaFX 应用程序并使用 JOptionPane 来显示对话框 我遇到的问题之一是创建一个新对话框并且不在 5 秒左右关闭它会导致主 JavaFX 阶段进入“无响应”状态 在新线程
好吧,我尝试将信息从 servlet 发送到 jsp,所以我尝试使用 request.setAttribute() 和 respond.getRequestDispatcher("page.jsp")
我对代码进行排序有什么问题吗?当我打开程序时,我看到错误“程序未响应”。请检查我的代码。 void sorting(el_list **first) { el_list *newfirst =
出于测试目的,我需要编写一个程序,将著名的“应用程序未响应”消息显示在屏幕上。我不知道我怎么会激起这样的 react 。 最快、最简单的方法是什么?最好是 C/C++ 或 Java 最佳答案 在 Wi
我是一名优秀的程序员,十分优秀!