Simple service worker example - amark/gun GitHub Wiki

Simple Service Worker Example

index.js

if ('serviceWorker' in navigator) {
  console.log('Registering service worker')

  const registration = await navigator.serviceWorker.register('/worker.js', {scope: '/'})
  console.log('Registered service worker')
}

worker.js

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!'
  })
})