[w3c/ServiceWorker] Can't register same ServiceWorker file to different subdomain (#994)

I currently have two subdomains (e.g., `example.com` and `staging.example.com`) that, for now, point to the same IP address/server. I've been working on `example.com` (and have since registered a ServiceWorker with that domain), but have recently wanted to shift work onto another server that `staging.example.com` will point to. Just now I tried accessing `staging.example.com` and get this message in my console:

> shell.js:29 ServiceWorker registration failed: DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL ('https://example.com') does not match the current origin ('https://staging.example.com').

Line 26 of shell.js is: `window.navigator.serviceWorker.register("/serviceworker.js")`.

I found that modifying the parameter to `location.origin + "/serviceworker.js"` removes the above error from my console, but I get a new error:

> shell.js:29 ServiceWorker registration failed: DOMException: Failed to register a ServiceWorker: ServiceWorker script evaluation failed

I found that this was caused by my install event listener:

```
self.addEventListener("install", (event_) => {
  // once the SW is installed, fetch the resources
  // to make this work offline and speed up load time
  event_.waitUntil(
    caches.open(cacheName).then((cache_) => {
      // add a list of all of the assets
      return cache_.addAll([
        "/",
        "/manifest.webmanifest",
        "/css/shell.css",
        "/html/shell.html",
        "/img/favicon.ico",
        "/img/fractal.jpg",
        "/js/third_party/adapter-2.0.4.js",
        "/js/cookies.js",
        "/js/objects.js",
        "/js/shell.js"
      ]).then(() => self.skipWaiting());
    })
  );
});
```

I fixed it by prepending each string in the array with `location.origin + `... A very ugly fix that looks like this:

```
self.addEventListener("install", (event_) => {
  // once the SW is installed, fetch the resources
  // to make this work offline and speed up load time
  event_.waitUntil(
    caches.open(cacheName).then((cache_) => {
      // add a list of all of the assets
      return cache_.addAll([
        location.origin + "/",
        location.origin + "/manifest.webmanifest",
        location.origin + "/css/shell.css",
        location.origin + "/html/shell.html",
        location.origin + "/img/favicon.ico",
        location.origin + "/img/fractal.jpg",
        location.origin + "/js/third_party/adapter-2.0.4.js",
        location.origin + "/js/cookies.js",
        location.origin + "/js/objects.js",
        location.origin + "/js/shell.js"
      ]).then(() => self.skipWaiting());
    })
  );
});
```

So everything works now this way. Both `example.com` and `staging.example.com`, while pointing to the same server, work as expected.

I'm using the latest version of Chrome, fyi. Is the method for obtaining the origin specified in the ServiceWorker specification? Because Chrome seems to only be looking at the top-level domain and nothing else. Like a request to `https://staging.example.com/resource` is seen by Chrome's ServiceWorker as `https://example.com/resource`. Is this inline with your specification?

Also, sorry in advance if this is not the place for this issue. I'm new here.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/994

Received on Friday, 21 October 2016 00:29:55 UTC