[Feature] Replace BroadcastChannel API with Client API in Service Worker #401
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
There are many different APIs available for the two-way communication between service worker and web pages. According to the suggestion of web.dev, previously we used the simple BroadcastChannel API.
https://web.dev/articles/two-way-communication-guide
However, if the service worker has been killed by the browser, messages sent via the BroadcastChannel API cannot revive it. This causes unstable service worker life while users are using the webapp.
This PR replaces BroadcastChannel API with the more primitive Client API as we found that messages sent via Client API will revive a stopped service worker. This ensures the normal functioning of our keep-alive mechanism.
Primary Change
BroadcastChannel
with Client API (navigator.serviceWorker.controller.postMessage()
andclient.postMessage()
)clientRegistry
to remember mapping between incoming messages and client idsweb_service_worker.ts
->service_worker.ts
service_worker.ts
->extension_service_worker.ts
Testing
examples/service-worker
The chat webapp is able to correctly keeping service worker alive after this change.