-
Notifications
You must be signed in to change notification settings - Fork 58
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
ready event is only triggered on localhost #20
Comments
Yes, I'm just discovering the same thing. It might be intentional given this comment: register-service-worker/src/index.js Line 37 in cedb35f
Until the service worker is ready, you don't seem to get a ServiceWorkerRegistration instance. |
I've copied the following lines... register-service-worker/src/index.js Lines 33 to 34 in cedb35f
... and placed them right after the call to register the SW when isLocalhost returns false and now the ready hook fires as I expected: register-service-worker/src/index.js Line 38 in cedb35f
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. |
I was also surprised by this behavior. I think there should be a consistent In my use case I have a kiosk where I need my service worker to load/update some From the code: register-service-worker/src/index.js Lines 29 to 40 in cedb35f
I think the code change suggested by @assemblethis addresses this issue. That means inserting the following three lines after line 38:
|
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
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. |
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.
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. |
For what it's worth, I ended up writing my own If anyone is interested, here is what I did: https://gist.github.com/bseib/06164a973552fe9f814df97bb4d30305 |
Thanks, very interesting. I need to take a closer look at what you did. |
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. |
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 #20
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.
The text was updated successfully, but these errors were encountered: