Android+Chrome not registering service workers

Noticed today that new service worker registration isn’t firing on Android Chrome, tested on a Pixel XL - Chrome 65.0.3325.109

It appears like the event listener isn’t firing here:

https://github.com/discourse/discourse/blob/28365f8ae5ed4c57da0afc6841fbbdfc1d96d0eb/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6#L5

@tgxworld It seemed pretty deliberate to have that event listener, but can we do without it?

6 Likes

So from what I tested on Android Chrome, the load event seems to only fire when the page is first loaded into a new tab. Once the tab has been loaded, the load event doesn’t fire until I close the tab and create a new one. Not sure if that is the intended behavior that the Chrome team wanted, perhaps we should check with them on this.

It was what the ServiceWorker guide from Google recommended when I was reading through the guide a few months back.

A simple rule of thumb would be to delay registration until after the load event fires on window , like so

We can definitely do without it but that does mean we have to be sure that whatever is happening in the initializer does not affect the page load time significantly.

4 Likes

It’s strange because it’s not consistent. Sometimes I’m able to get the service worker in a new tab and refresh both (more consistently on meta), but on other sites I cannot seem to get the service worker to re-install even when opening a new tab.

My best guess is that ember’s initialization method may fire after the window’s load event, so it never gets called in some cases.

Other oddities, the service worker registrations only work in production environments with compiled assets - I’ve added a registration case for development as well.

I’ve prepared a PR with the suggested fixes, let me know if it looks OK:

https://github.com/discourse/discourse/pull/5749

3 Likes

Another candidate for “delay until there’s not too much work” is requestIdleCallback.

2 Likes

A nice suggestion - just wish there was more browser support for it. :frowning:

1 Like

Simplest is just to measure, if real short skip delay, if longer than 3ms sleep 3 seconds and register

3 Likes

I haven’t seen that function take longer than 0.2ms. I’ve measured it around 0.17ms on average, so it seems OK without the delay.

2 Likes