yyx990803 / register-service-worker

A script to simplify service worker registration with hooks for common events.
MIT License
638 stars 58 forks source link

Allow `register` to be called after `onload` event #33

Closed cainrus closed 4 years ago

cainrus commented 5 years ago

It's not possible to register service-worker on demand right now.

sodatea commented 5 years ago

I'm sorry but I don't understand what difference does it make?

cainrus commented 5 years ago

I'm sorry but I don't understand what difference does it make?

if you call register function on click event, then service-worker will not register itself, because load event has been triggered already. Bug has been fixed 🤓

kmohrf commented 4 years ago

This is helpful if the service worker registration is an optional feature in a web app. In my case we produce a single build that can be configured at runtime through a JSON file. If the organization that uses the software provided the necessary configuration options the service worker is registered and the web app manifest is injected into the page. But as the configuration is loaded after the load event has been triggered, register-service-worker will never actually register the service worker.

eduardosada commented 4 years ago

Would be nice to have this PR merged.

nicolas-t commented 4 years ago

Hello,

I expected the registration to happen when I called the register function. Not after an "arbitrary" event of the page lifecycle.

The proposed solution is nice because it's not breaking, and should still be compatible with vue-cli PWA plugin generated file registerServiceWorker.js

On the other hand, I think it'd be more flexible (and intuitive) to let the parent script handle the timing of the registration.

Something like this:

import { register } from 'register-service-worker'

window.addEventListener('load', () => {
    register('/service-worker.js', { ... })
})

Which would also allow this (for example) :

<script>
import { register } from 'register-service-worker'

export default {
    // ...

    mounted() {
        register('/service-worker.js', { ... })
    }
}
</script>
cainrus commented 4 years ago

@nicolas-t

On the other hand, I think it'd be more flexible (and intuitive) to let the parent script handle the timing of the registration.

I believe it's a breaking change for next major version.

sodatea commented 4 years ago

Thanks for the help. I've also added a compatibility layer to avoid ReferenceError in IE11. This feature will be landed in the 1.7 release.