I'm currently working on adding a SW to my CRA. The registration part is fine however,I can't seem to get my SW to listen to events with because 'this' seems to be undefined and I wonder why. Here is my service-worker.js

`export function register(){
navigator.serviceWorker.register('/service-worker.js').then(reg => {
console.log(reg,'registration complete')}
}
this.addEventListener('install',()=> console.log('installed'))`

and in my index.js I simply call the exported register() function wich I think works because registration is working.

1 Answers

0
Cody Swann On
export function register(){
  navigator.serviceWorker.register('/service-worker.js').then(reg => {
  console.log(reg,'registration complete')}
}
self.addEventListener('install',()=> console.log('installed'))