- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.2k
Simple service worker example
        Daniel Raeder edited this page May 28, 2022 
        ·
        1 revision
      
    if ('serviceWorker' in navigator) {
  console.log('Registering service worker')
  const registration = await navigator.serviceWorker.register('/worker.js', {scope: '/'})
  console.log('Registered service worker')
}console.log('Loaded service worker!')
// Service worker code...
// For example, this is a listener for push notifications, 
// based on this article: https://thecodebarbarian.com/sending-web-push-notifications-from-node-js.html
self.addEventListener('push', ev => {
  const data = ev.data.json()
  console.log('Got push', data)
  self.registration.showNotification(data.title, {
    body: 'Hello, World!'
  })
})