Can I distinguish given external web sites have 'service-worker' or not?

My best guess is:

  1. retrieve all the js files from given URL
  2. search for string 'sw.js'

(i am unsure how to do this as well)

Given data would be like this:

const sitesToCheck = {
  • URL on a server you own or any external server? – Jankapunkt Jul 17 '17 at 6:48
  • 2
    On webpage, You can check under Application Pannel > Service Workers under Chrome Developer Tools – Atul Sharma Jul 17 '17 at 6:49

If you want to do this in code, you can only do it within the same origin as the worker(s) you want to check for. Within the same origin, you can use getRegistration(url) (which gives you a promise that will resolve to undefined or a ServiceWorkerRegistration object for the URL), or getRegistrations() (which gives you a promise for an array of ServiceWorkerRegistration objects). E.g.:

navigator.serviceWorker.getRegistrations().then(registrations => {
  • If it's NOT from the same origin, what else I can try? – RNA Jul 17 '17 at 7:18
  • 2
    @RNA: As far as I know, nothing, in a web browser; this stuff is restricted to the same origin for a reason. Not from a browser, you could download all of the site's JavaScript and check for calls to navigator.serviceWorker.register (this requires a lot of analysis, handling things like const sw = navigator.serviceWorker; sw.register(...);, or const foo = "ister"; navigator["ser" + "vice" + "Work" + "er"]["reg" + foo](...);). – T.J. Crowder Jul 17 '17 at 7:21

You can look at Service Worker Detector, a Chrome extension that detects if a website registers a Service Worker by reading the navigator.serviceWorker.controller property. It might also work in other browsers supporting Web Extensions, but it looks like it is not yet distributed as such.

However, it requires the script to be run in a browser, which might not cover your needs. You can try with a scriptable headless Chrome.

  • thanks for the clue, i found a tool called lighthouse(developers.google.com/web/tools/lighthouse) I'll try to get it working in my code :) – RNA Jul 19 '17 at 2:38
  • 1
    Lighthouse is indeed currently the best tool to test webapps for web development best practices, including performance and PWA. – Nicolas Hoizey Jul 19 '17 at 8:38

Your Answer

