Search code examples
service-workerjsbin

Is it possible to create jsbin code with service worker?


In JSBin, I don't see an option to add service worker. Is it possible? Or, is there any other options?


Solution

  • I don't think it's possible to put together an example/demo that registers your own service worker using JSBin.

    In terms of other options, what I tend to do is use GitHub's Gists to store my HTML and service worker JavaScript, and then use RawGit to serve the resources. RawGit gives you HTTPS plus proper Content-Type headers, both of which are necessary in order to register a service worker.

    Here's an example of a Gist that uses this setup.

    You need to get the "Raw" URL for your HTML (click on the "Raw" button in the Gist interface), and then paste that URL into https://rawgit.com/. When registering your service worker from your HTML, always use a relative URL (like navigator.serviceWorker.register('sw.js');), and include the code for your service worker in another file that's part of the same Gist.

    You'll end up with a URL served by RawGit that will let you access your HTML and can register and use your service worker file.