# Exploring ServiceWorkers for Discourse

**URL:** <https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422>\
**Category:** Feature\
**Created:** [August 23, 2015, 4:36am UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422 "2015-08-23T04:36:07Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)\
**Post date:** [August 23, 2015, 4:36am UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/1 "2015-08-23T04:36:08Z")

</div>

ServiceWorkers are a new “web platform” feature that seem to be maturing about now. Here’s two good overviews of what they can do: [Using ServiceWorker in Chrome today - JakeArchibald.com](https://jakearchibald.com/2014/using-serviceworker-today/#demos) [Using Service Workers - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers)

Note also that **because ServiceWorkers can modify network requests, they are only available on HTTPS sites.** In other words: ServiceWorkers can only ever be a value-add for us, _never_ a requirement. The app must always work without them.

What can ServiceWorkers do for Discourse?

- (Best start) Merge message-bus requests across multiple open tabs on the same Discourse forum.

- (Questionable value) Force caching of vendor.js and application.js

- (Great value) Push & Mobile Notifications

- (Easily screwed up) Caching of topic & post data

- (Best value, hardest) Deliver the entire initial load from local cache

Implementation issues I’ve thought of already:

- If there’s a delay between getting topic data and getting the message bus current position from the server, we could lose updates. **Solution** : deliver the current message bus position in the topic response (and other pages).
- The message bus currently screws up big time if the backlog is cleared (`redis-cli FLUSHALL`). **Solution** : The server should deliver a message on \_\_global when a client requests a too-high ID, and the client will reload the page (or the worker will reload all pages and discard its messagebus data).
- A new ServiceWorker that gets downloaded and installed will not activate until **all clients** (browser tabs) **using the old version are disposed**. This is a problem for Discourse, because tabs are long-lived. The solution is to get all active clients to refresh. We’ll need some design work to figure out how to do this in a non-intrusive way (the current behavior, “silently reload on navigation, or _show a popup after 2 hours_” is actually user-hostile in the second case.)

Implementation notes:

- use `Clients.matchAll({ includeUncontrolled: true, type: 'window'})` to get all the window objects to trigger refreshes, via postMessage() or navigate(). TODO: how to identify new version vs old version? Make sure not to force refreshes on auth popups (client.frameType).

Reply with comments, or if you have any use-cases that I forgot about.

---

_[View the full topic](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422)._
