Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ready event is only triggered on localhost #20

Closed

Comments

@yennor
Copy link

yennor commented Feb 4, 2019

as it seems. The ready event, will only be triggered if the service-worker runs on localhost.
That seems to be quite confusing (at least to me). If that is intentionally then it should be documented.

@assemblethis
Copy link

assemblethis commented Aug 7, 2019

Yes, I'm just discovering the same thing.

It might be intentional given this comment:

// Is not local host. Just register service worker

Until the service worker is ready, you don't seem to get a ServiceWorkerRegistration instance.

@assemblethis
Copy link

I've copied the following lines...

navigator.serviceWorker.ready.then(registration => {
emit('ready', registration)

... and placed them right after the call to register the SW when isLocalhost returns false and now the ready hook fires as I expected:

registerValidSW(swUrl, emit, registrationOptions)

This seems to be the behavior you'd expect, that the ready hook would fire whether or not isLocalhost returns true or false. But I may be wrong. I also opened another issue #36 that is possibly related.

@bseib
Copy link
Contributor

bseib commented Sep 30, 2019

I was also surprised by this behavior. I think there should be a consistent ready event that you can expect in both dev and production environments.

In my use case I have a kiosk where I need my service worker to load/update some mp4 video files into the local cache before I instantiate the root Vue component (whose children refer to these videos). So I need an event that says a service worker is "ready" (regardless of which version of service worker it is).

From the code:

window.addEventListener('load', () => {
if (isLocalhost()) {
// This is running on localhost. Lets check if a service worker still exists or not.
checkValidServiceWorker(swUrl, emit, registrationOptions)
navigator.serviceWorker.ready.then(registration => {
emit('ready', registration)
})
} else {
// Is not local host. Just register service worker
registerValidSW(swUrl, emit, registrationOptions)
}
})

I think the code change suggested by @assemblethis addresses this issue. That means inserting the following three lines after line 38:

     navigator.serviceWorker.ready.then(registration => { 
       emit('ready', registration) 
     }) 

bseib added a commit to bseib/register-service-worker that referenced this issue Sep 30, 2019
Emit the 'ready' event regardless of being on localhost development or
in production. When a service worker is active and ready, emit the
signal.

Fixes yyx990803#20
@assemblethis
Copy link

assemblethis commented Oct 16, 2019

I still can't see why you wouldn't fire the 'ready' event for non-localhost. bseib's PR seems good and is what I've been using.

@assemblethis
Copy link

Ok, I'm just using register-service-worker as is (v1.6.2) since PR #37 hasn't been approved yet. So, instead, after registering the service-worker I then have my own ready hook placed right after. This is because, as far as I understand it, the ready hook will only fire in a DEV environment since it requires localhost.

The code in this project seems to have been taken from the create-react-app template as far as I can tell.

In keeping with the name of the project it will register the service worker reliably but will not tell you when it's ready.

So it seems to me the 'ready' hook from this project should be ignored and you should just have your own 'ready' hook placed right after the register function returns.

register(`${process.env.BASE_URL}service-worker.js`, { 'code for hooks minus ready hook' });
navigator.serviceWorker.ready.then(swr => { 'ready hook code' });

My worry is that there is some foundational technical piece of information that I'm missing about service workers, such as when they're running properly they always run as localhost since essentially they're running on the device. But I haven't experienced that.

@bseib
Copy link
Contributor

bseib commented Nov 11, 2019

For what it's worth, I ended up writing my own ServiceWorkerManager so that I could have a solid understanding of when the service worker related events occur, and so I'd really know what the state of "current" and "waiting" services workers are at those moments.

If anyone is interested, here is what I did: https://gist.github.com/bseib/06164a973552fe9f814df97bb4d30305

@assemblethis
Copy link

Thanks, very interesting. I need to take a closer look at what you did.

@frostfire64
Copy link

I join the reports of this behavior being bizarre, such unexpected behavior should be at least clearly documented. I wish I searched issues with the package before I spend many hours trying to debug my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment