Repository navigation
Proposal: workbox-messages module #1848
Description
To accompany the new workbox-window library in v4, and standardize the functionality that's needed for the skipWaiting message handler proposed in #1753 as well as what's already implemented in
workbox/packages/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
Lines 185 to 207 in 208850c
| _initWindowReadyDeferreds() { | |
| // A mapping between navigation events and their deferreds. | |
| this._navigationEventsDeferreds = new Map(); | |
| // The message listener needs to be added in the initial run of the | |
| // service worker, but since we don't actually need to be listening for | |
| // messages until the cache updates, we only invoke the callback if set. | |
| self.addEventListener('message', (event) => { | |
| if (event.data.type === 'WINDOW_READY' && | |
| event.data.meta === 'workbox-window' && | |
| this._navigationEventsDeferreds.size > 0) { | |
| if (process.env.NODE_ENV !== 'production') { | |
| logger.debug(`Received WINDOW_READY event: `, event); | |
| } | |
| // Resolve any pending deferreds. | |
| for (const deferred of this._navigationEventsDeferreds.values()) { | |
| deferred.resolve(); | |
| } | |
| this._navigationEventsDeferreds.clear(); | |
| } | |
| }); | |
| } | |
| } |
I'm proposing a new set of methods on workbox.core:
-
registerMessageCallback({messageType, callback})will take care of adding in amessageevent handler (if one doesn't already exist) that will in turn check for incomingmessages whoseevent.data.type === messageType, and when found, runawait callback(event)for each registered event. The callback mechanism will be wrapped inevent.waitUntil()(for browsers that support it: MessageEvent within the SW global should have waitUntil() w3c/ServiceWorker#669 (comment)) -
unregisterMessageCallback({messageType, callback})will do the opposite, and either unregister a specificcallback, or ifcallbackisnull, unregister all callbacks for the givenmessageType.
I think that keeping track of messageType values can be done on a per-module basis, i.e. the constant used by workbox-broadcast-cache-update can be defined locally there.
One additional area for discussion is whether this proposal can end up powering the mechanism that's already in place in workbox.core for registering callbacks that can be run when there's a global QUOTA_EXCEEDED error. There's a lot of similarities, but invoking callbacks triggered by an exception is different from invoking callbacks triggered by a message event.
Activity
philipwalton commentedon Jan 16, 2019
This proposal sounds great to me. And I think it definitely makes sense for the message registrations to be handled by the individual packages, not all grouped in
workbox-core.workbox-windowhas a smallEventTargetShimclass that we could move intoworkbox-core. It might be easier to use this sincemessageevents are required to be added in the initial execution phase of the SW, whereas here we'd want the flexibility to add/remove listeners in callbacks.philipwalton commentedon Jan 16, 2019
As for the format, I think something like this would be fine:
We could potentially also try to standardize on using verbs for action messages and nouns for notice message, or something like that. I believe right now our only message instances are:
Am I missing anything?
jeffposnick commentedon Jan 16, 2019
Oh, yeah, I forgot that adding in a
messageevent listener post-installwould break, just like the other event listeners. I'll take that into account,jeffposnick commentedon Jan 16, 2019
I did a quick prototype and it looks like adding in this functionality would take
workbox-core.prod.jsfrom 5513 to 5924 bytes. Given thatworkbox-coreis always loaded, those extra bytes might not be worth it for optional functionality.This logic could live in a new
workbox-messagesmodule instead.[-]Add a common 'message' event listener to workbox.core[/-]philipwalton commentedon Jan 16, 2019
In the CDN case, yes, but in the modules case it would only be loaded if a module that consumed it were loaded.
This is also true of things like the IDB wrapper, which currently gets loaded for CDN users even if they're only using precaching (or any other module that doesn't use IDB).
My preference would be to keep
workbox-coreas the single place for shared code, but I guess I can also see compelling reasons not to do that if the vast majority of our users consume workbox viaimportScripts().jeffposnick commentedon Jan 18, 2019
Moving over some thoughts from a separate conversation: if we had this module, it would be a good home for code that currently lives in
workbox-broadcast-cache-update:workbox/packages/workbox-broadcast-cache-update/BroadcastCacheUpdate.mjs
Lines 91 to 127 in 208850c
philipwalton commentedon Jan 20, 2019
Yesterday I discovered that the SW spec includes a buffering mechanism (client message queue) for when a SW calls
postMessage()on a window client. The default behavior is messages won't be dispatched on the window until afterDOMContentLoaded.Unfortunately, this behavior isn't yet implement in all browsers, so for the moment we'll have to continue using this
WINDOW_READYstrategy, but whatever we do here should be forward compatible enough that we can remove this code at some point.However, the buffering behavior applies to
postMessage()calls but notBroadcastChannelmessages. This, coupled with the fact thatBroadcastChannelis not even implemented in all browsers makes me wonder if we should switch to using postMessage exclusively.[-]Add a new workbox.messages module[/-]tomayac commentedon Apr 25, 2024
Hi there,
Workbox is moving to a new engineering team within Google. As part of this move, we're declaring a partial bug bankruptcy to allow the new team to start fresh. We realize this isn't optimal, but realistically, this is the only way we see it working. For transparency, here're the criteria we applied:
v7branch.Thanks, and we hope for your understanding!
The Workbox team