[英]Call Back not fired when listening to 'sync' event in service worker
I am listening to the 'sync'
event in my service worker file but NOTHING happens. 我正在收听服务工作文件中的
'sync'
事件但是没有发生。 Even my console.log
does not fire 甚至我的
console.log
也不会触发
I ensure that i returned a promise in the waitUntil()
function. 我确保我在
waitUntil()
函数中返回了一个promise。
this is what my main.js file looks like 这是我的main.js文件的样子
finalOrder.addEventListener('submit', function(event) {
event.preventDefault();
var body = domArrayToObject(event.target)
showForm();
if ('serviceWorker' in navigator && 'SyncManager' in window) {
(navigator.serviceWorker.ready
.then(worker => {
return worker.sync.register('write-req')
.then(function(s){
localStorage.setItem('req', JSON.stringify(body));
}).catch(err => console.log(err))
}).catch(err => console.log(err)))
} else {
sendMails(body);
}
})
this is my sendMails function (urlstring is a firebase function endpoint) 这是我的sendMails函数(urlstring是一个firebase函数端点)
function sendMails(body) {
return (Promise.all([fetch('urlstring', {
method: 'POST',
body: JSON.stringify(body),
headers:{
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(response => console.log(res))
.catch(err => console.log(err)),
fetch('urlstring', {
method: 'POST',
body: JSON.stringify(body),
headers:{
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(response => console.log(res))
.catch(err => console.log(err))]).catch(err => console.log(err)))
}
this is my service worker file 这是我的服务工作者文件
self.addEventListener('sync', ev => {
console.log('[Yaay Internet Connection]', ev)
if (ev.tag == 'write-req') {
console.log('[Service Worker] Syncing new Posts');
var body = localStorage.getItem('req');
ev.waitUntil(sendMails(body)
.then((res) => {
self.registration.showNotification('Order has been placed!!!',
{
body: 'Your Order has been placed. You will hear from us soon',
image: '',
icon: '',
badge: '',
actions: [
{ action: 'add', title: 'add', icon: ''}
]
})
}));
}
})
I want to listen to the 'sync' event and fire the callback. 我想听'sync'事件并触发回调。 Sending the request to the endpoint if there is connection already or it is re established.
如果已经建立连接或重新建立连接,则将请求发送到端点。
结果我所要做的就是使用indexedDB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.