NekR / offline-plugin

Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
MIT License
4.52k stars 295 forks source link

getRegistration ignores scope #435

Closed lilfolr closed 5 years ago

lilfolr commented 5 years ago

Should the getRegistration method in the runtime template optionally include the scope as an arg (if one has been defined)?

https://github.com/NekR/offline-plugin/blob/e2fd1ab6126664a72e6529b7d6f19c3c80e60099/tpls/runtime-template.js#L223

So something like this:

<% if (ServiceWorker.scope) { %>
navigator.serviceWorker.getRegistration(<%- JSON.stringify(ServiceWorker.scope) %>).then(function(registration) { 
 <% } %>
...
GGAlanSmithee commented 5 years ago

Does sound like a bug, have you experienced any actual problem due to this @lilfolr?

lilfolr commented 5 years ago

Yeah, so our app has to define the scope to ensure the service worker doesn't affect other applications under the same domain. When we define the scope the above method can't find the registered service worker to update it.

We have a workaround though - basically set autoUpdate to false and manually check for updates in the app (specifying the scope) ie:

swUpdate() {
  navigator.serviceWorker.getRegistration(SCOPE).then((registration) => {
    if (!registration) return;
    return registration.update();
  });
}
GGAlanSmithee commented 5 years ago

@lilfolr sounds legit, would you consider sending a PR our way?

lilfolr commented 5 years ago

Can do. Have a bit on at work atm, but I'll try get something out for you to review in the coming weeks