javascriptservice-workersw-precache

sw-precache push notification listener


I would like to create my first PWA app and I want to use sw-precache. I would like to have push notifications in my app, but I don't see any option to add push notification and notification click listeners via sw-precache generate.

Should I add some sort of concatenate after the files is generated?

Does anyone knows how to do this?


Solution

  • You can add in additional logic using the importScripts option in sw-precache.

    For example, if you create a push-listener.js file with the following:

    self.addEventListener('push', event => {
      // Your code here.
    });
    

    You can configure sw-precache to import it:

    {
      importScripts: ['path/to/push-listener.js'],
      // Other sw-precache options here.
    }