I’ve been recently trying to understand some of the implications of HTTP/2 - which in itself was triggered by my impression that the ServiceWorker spec focuses heavily (perhaps exclusively) on request/response (including streaming) interactions.
I was curious to hear about some other opinions about some of the conclusions that I have arrived at.
This seems like a very deliberate choice.
My interpretation of this is to strongly encourage designing all client/server interactions in terms of request/response (fetch) - while relegating SSE to only serve as a notification source - fighting fat event syndrome.
So at a very general level there is the intent to limit the SSE traffic to notifications to “use your request/response machinery to update yourself” rather than pushing actual updates over SSE.
The favoured bidirectional communication pattern seems to be:
Client to server:
- POST/PUT data to server
Server to client:
- SSE to client “new data available”
- GET new data from server
Given that all of this can happen over the same connection it’s probably less problematic than it was under HTTP/1.1
The micro-pain of the additional GET in the case of the server-to-client interaction has a desirable macro-effect - all data is forced through the Fetch API.
- All browser fetches go through though the ServiceWorker giving it an opportunity to cache the request/response (if so desired).
- The ServiceWorker isn’t interested in ephemeral data, e.g. “real time data” that may have a temporary effect on the page as it is currently rendered but wouldn’t leave a trace if the page was reloaded.
- However any data that may be necessary to “weave” the page together again, especially when the network connection is poor or non-existent, is non-ephemeral and therefore a candidate for caching.
- Forcing all data through the Fetch API simplifies site caching. Non-ephemeral data delivered via SSE:
For example HTML, CSS, and JS files should be stored in the cache, while JSON data should be stored in IndexedDB. Note that this is only a guideline, not a firm rule.