My sync event not firing, am using chrome webserver to serve a page on my desktop PC on localhost:5000 and using a fetch api to POST data to the local development server on a separate port localhost:1337. Have turned off my wifi, the chrome webserver and also the offline toggle in the chrome devtool, yet still not firing my event.
This is my code in serviceworker.js snippet for sync
self.addEventListener('sync', function(event) {
if (event.tag == 'syncrhronizeOfflineData')
{
event.waitUntil(syncrhronizeData());
}
});
async function syncrhronizeData() {
console.log('now synchronizing data...');
//first get all favourites in the localDB and post them to server
const favData = await window.localforage.getItem('restaurants')
.filter((r) => r.isoffline );
console.log('favdata', favData);
}
Have not actually posted to the database yet, trying to console favdata but not getting the message, the event is not been triggered.
Snippet in main.js where i register serviceworker
/** * Service worker functions below */
registerServiceWorker = () => {
if (!navigator.serviceWorker) return;
navigator.serviceWorker.register('/service-worker.js')
.then((reg) => {
if (!navigator.serviceWorker.controller) return;
if (reg.waiting) {
updateWorker(reg.waiting);
return;
}
if (reg.installing) {
trackInstalling(reg.installing);
return;
}
reg.addEventListener('updatefound', () => {
trackInstalling(reg.installing);
});
}).catch(function(err) {
console.error(err); // the Service Worker didn't install correctly
});
//background sync for favourites and reviews offline posting
if ('SyncManager' in window || 'sync' in reg) {
navigator.serviceWorker.ready.then(function(swRegistration) {
console.log('[ServiceWorker] is ready - sync is registered');
return swRegistration.sync.register('syncrhronizeOfflineData');
});
}
};
Am getting a console message that '[ServiceWorker] is ready - sync is registered' but not getting a message from the sync event after i posted data offline and reconnect back.