-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 2 | Add JS responsible about SW
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register('../sw.js', { scope: '/' }).then((reg) => { | ||
if (reg.installing) { | ||
console.log('Service worker installing'); | ||
} else if(reg.waiting) { | ||
console.log('Service worker installed'); | ||
} else if(reg.active) { | ||
console.log('Service worker active'); | ||
} | ||
|
||
}).catch((error) => { | ||
console.log('Registration failed with ' + error); // Registration failed | ||
}); | ||
|
||
// Communicate with the service worker using MessageChannel API. | ||
function sendMessage(message) { | ||
return new Promise((resolve, reject) => { | ||
const messageChannel = new MessageChannel(); | ||
messageChannel.port1.onmessage = function(event) { | ||
resolve(`Direct message from SW: ${event.data}`); | ||
}; | ||
|
||
navigator.serviceWorker.controller.postMessage(message, [messageChannel.port2]) | ||
}); | ||
} | ||
} |