service worker is javascript file that run every time.It keeps running even you
close the browser.Due to this we can send push notification,offline mode.
//Register our service worker in index.html
<script>
if('serviceWorker' in navigator){
window.addEventListener('load', ()=> {
//registering of serviceWorker.
navigator.serviceWorker('./serviceworker.js')
.then((reg)=> console.log("Success:", reg.scope));
.catch((err)=> console.log("Failure: ", err));
})}
<script>
everything ok then we will see Sucess: localhost:3000 in console of browser
if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('/sw.js').then(function(registration) { // Registration was successful console.log('ServiceWorker registration successful with scope: ', registration.scope); }, function(err) { // registration failed :( console.log('ServiceWorker registration failed: ', err); }); });}
const filter = {
url: [
{
urlMatches: 'https://www.google.com/',
},
],
};
chrome.webNavigation.onCompleted.addListener(() => {
console.info("The user has loaded my favorite website!");
}, filter);