我在我的网站上介绍服务工作者。并且我使用app-shell方法来响应requests.Below是我的代码结构。服务工作者app-shell PHP实现
serviceWorker.js
self.addEventListener("fetch", function(event) {
if (requestUri.indexOf('-spid-') !== -1) {
reponsePdPage(event,requestUri);
}else{
event.respondWith(fetch(requestUri,{mode: 'no-cors'}).catch(function (error){
console.log("error in fetching => "+error);
return new Response("not found");
})
);
}
});
function reponsePdPage(event,requestUri){
var appShellResponse=appShellPro();
event.respondWith(appShellResponse); //responds with app-shell
event.waitUntil(
apiResponse(requestUri) //responds with dynamic content
);
}
function appShellPro(){
return fetch(app-shell.html);
}
function apiResponse(requestUri){
var message=['price':'12.45cr'];
self.clients.matchAll().then(function(clients){
clients.forEach(function (client) {
if(client.url == requestUri)
client.postMessage(JSON.stringify(message));
});
});
}
的App-shell.html
<html>
<head>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.onmessage = function (evt) {
var message = JSON.parse(evt.data);
document.getElementById('price').innerHTML=message['price'];
}
}
</script>
</head>
<body>
<div id="price"></div>
</body>
</html>
serviceWorker.js是我唯一的服务人员文件。每当我收到-spid- url的请求时,我会调用reponsePdPage函数。在reponsePdPage函数中,我首先回应了app-shell.html。之后我打电话apiResponse功能,调用PostMessage的发送动态data.The 听者的留言信息写在APP-shell.html。
我面临的问题是,有时会在收听者注册之前调用发送消息。这意味着apiResponse调用发送消息,但它们不是注册该事件的监听器。所以我无法捕获数据。?在我的实现中他们的错误。
如果我使用第一种方法,即从受控页面调用postMessage,这意味着服务工作线程将保持闲置直到我从客户端调用postMessage。在第二种方法中,如果服务未完成,我检查IndexedDB中的值。 –
我不理解服务员“闲置”的担忧。这不是由您来管理服务工作线程的执行。浏览器将决定杀死软件或者基于你的控制之外的启发式软件保持它活着。重要的是你可以调用'postMessage()',你将(最终)得到一个响应。 –
我的意思是说,因为所有的后端计算都是在第二次调用中完成的(意味着** apiResponse **方法),并且服务工作人员只有在客户端调用post消息时才会启动此方法。在返回app-shell之后启动api调用并将结果放入缓存不是一个好主意。 –