Skip to content

Simple service worker example

Daniel Raeder edited this page May 28, 2022 · 1 revision

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

This wiki is where all the GUN website documentation comes from.

You can read it here or on the website, but the website has some special features like rendering some markdown extensions to create interactive coding tutorials.

Please feel free to improve the docs itself, we need contributions!

Clone this wiki locally