4

I'm writing a library that contains a service worker. I'd like to create instructions for use something like this:

npm install my-lib
...
import MyLib from 'my-lib'
...
MyLib.init();

And in the init() function I need to register the service worker.

I'd like the actual service worker file to be in my library, and it would be nice if I didn't have to ask the client to change their webpack.config.js file.

Is there a way for my library to indicate that the service worker file should be copied to the server with no processing so that it can be registered?

I was thinking my library would have a structure like:

public
    sw.js
src
    etc.js
index.js
package.json

Thanks for any help you can give. Most of the info I've found concerns the PWA service worker that caches assets and I don't see much info on just simple, generic service workers.

Paulie
  • 1,940
  • 3
  • 20
  • 34
  • For the moment, I'm using https://github.com/webpack-contrib/copy-webpack-plugin Copy Webpack Plugin in my test program, so I'm able to keep working on this, but I'm really hoping I don't need to force my clients to install anything or modify a config file. – Paulie May 07 '18 at 21:53

1 Answers1

2

You can use a Blob instead of a separate file for the worker script: How to create a Web Worker from a string

Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23
  • Interesting idea. I'll play with this and see how it works for me. My first thought is that it will be hard to debug and maintain it, but maybe that's ok. – Paulie May 07 '18 at 21:51