# Exploring ServiceWorkers for Discourse

**URL:** https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422
**Category:** Feature
**Created:** [2015年八月23日 04:36 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422 "2015-08-23T04:36:07Z")
**Posts on this page:** 20
**Page:** 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: [2015年八月23日 04:36 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.

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [2015年八月23日 11:58 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/2 "2015-08-23T11:58:23Z")

</div>

Very excited about this! I’m hoping it’d make sense to bundle “Add to Homescreen” into this effort.

> [@Fully support Android Chrome's "Add to Homescreen"](https://meta.discourse.org/t/fully-support-android-chromes-add-to-homescreen/21067):
>
> New in Chrome 39 Beta is an improved [add to homescreen](https://developer.chrome.com/multidevice/android/installtohomescreen) experience. Since Chrome M31, you can set up your web app to have an application shortcut icon added to a device’s homescreen, and have the app launch in full-screen “app mode” using Chrome for Android’s “Add to homescreen” menu item. New in Chrome M39, you can define the metadata associated with your web application in a JSON-based manifest. The manifest provides a way to wrap metadata about a web application into a single file. Using …

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年八月24日 00:55 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/3 "2015-08-24T00:55:36Z")

</div>

I like this, but it needs to be tightly scoped.

Our **first** priority is getting the push notifications going for Android, cause that unlocks brand new functionality.

Our **second** priority is a PR to message bus that enables centralizing of message bus polling without turning message\_bus.js into a “soup” of completely hard to understand and debug code that falls back super cleanly.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2015年八月24日 01:38 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/4 "2015-08-24T01:38:22Z")

</div>

And before we do any of this, there are critical items on the 1.4 release list that are not done…

---

<div class="post-metadata">

### Author: ![Tom\_Newsom](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tom_newsom/32/115981_2.png) [@Tom\_Newsom](https://meta.discourse.org/u/Tom_Newsom)
#### Post date: [2015年九月18日 09:16 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/5 "2015-09-18T09:16:43Z")

</div>

Praises be for push notifications! 😃  
I bet Safari doesn’t support them ☹

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2015年九月18日 09:27 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/6 "2015-09-18T09:27:18Z")

</div>

![](https://global.discourse-cdn.com/meta/original/3X/4/a/4a1e7e981cdfa7a4a9d2a1475af623790de17047.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/c/b/cbc6660d54a22e46f2a5caee7b86e578214be6c9.png)

I did a quick spike and got it to work. Do we have a 👍 to continue?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2015年九月18日 10:15 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/7 "2015-09-18T10:15:46Z")

</div>

Sure go ahead 👍

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2015年九月28日 05:55 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/8 "2015-09-28T05:55:14Z")

</div>

Finally got it to work properly 😄

 ![](https://global.discourse-cdn.com/meta/original/3X/e/e/ee5d803b048c0d6f50949b2c8f1e1d49804825a2.png) ![](https://global.discourse-cdn.com/meta/original/3X/f/9/f964c1405a72074b1149a79543a61263bb4ac5fa.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/e/6/e63ec3cc43025692930d46e54c70e9aeb048046d.gif)

[https://github.com/discourse/discourse/pull/3815](https://github.com/discourse/discourse/pull/3815)

---

<div class="post-metadata">

### Author: ![Tom\_Newsom](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tom_newsom/32/115981_2.png) [@Tom\_Newsom](https://meta.discourse.org/u/Tom_Newsom)
#### Post date: [2015年九月28日 09:12 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/9 "2015-09-28T09:12:42Z")

</div>

Oh my 😄

Can’t wait to try this out!  
(and 10 seconds later have iOS users complain that they’re not supported. Come on Apple!)

---

<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: [2015年九月28日 20:12 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/10 "2015-09-28T20:12:15Z")

</div>

Here’s the docs for Firefox push notifications 😉 [Push API - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2015年十月12日 15:43 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/11 "2015-10-12T15:43:01Z")

</div>

Hmmm the Push API isn’t available until FireFox 42, I’ll wait till it is released first before integrating push notifications for FireFox. In the mean time, I’ll update my PR to account for different browsers.

---

<div class="post-metadata">

### Author: ![steko](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@steko](https://meta.discourse.org/u/steko)
#### Post date: [2016年一月26日 18:30 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/12 "2016-01-26T18:30:39Z")

</div>

Seems like Firefox 44 has Push Notifications:

> **[Firefox Can Now Get Push Notifications From Your Favorite Sites | The Mozilla...](https://blog.mozilla.org/en/firefox/firefox-can-now-get-push-notifications-from-your-favorite-sites/)**
>
> UPDATED TO CLARIFY HOW TO MANAGE PUSH NOTIFICATIONS Firefox for Windows, Mac and Linux now lets you choose to receive push notifications from websites if y

---

<div class="post-metadata">

### Author: ![fbender](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fbender/32/116736_2.png) [@fbender](https://meta.discourse.org/u/fbender)
#### Post date: [2016年二月7日 21:03 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/13 "2016-02-07T21:03:13Z")

</div>

> [@riking](#):
>
> 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.)

This is not (or at least no longer) true, see the `clients.claim()` API: [https://slightlyoff.github.io/ServiceWorker/spec/service\_worker/#clients-claim](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#clients-claim)

---

<div class="post-metadata">

### Author: ![Hector](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@Hector](https://meta.discourse.org/u/Hector)
#### Post date: [2016年二月8日 13:51 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/14 "2016-02-08T13:51:29Z")

</div>

Good news from Microsoft land: service workers and the push api moved to High Priority in the roadmap for Edge.

[https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6263630-service-worker](https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6263630-service-worker)  
[https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6263693-push-api](https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6263693-push-api)

---

<div class="post-metadata">

### Author: ![Rob\_Nicholson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rob_nicholson/32/63483_2.png) [@Rob\_Nicholson](https://meta.discourse.org/u/Rob_Nicholson)
#### Post date: [2016年三月9日 10:25 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/15 "2016-03-09T10:25:15Z")

</div>

Big thumbs up for push notifications. We’re currently trialling Discourse to replace PhpBB for CAMRA (170,000 members) and I’ve been a fan of Discourse - having used StackExchange for years.

The single biggest requirement for the replacement system is so smartphone and tablet friendly but Discourse isn’t sadly ticking all the boxes. It’s better than PhpBB but maybe not enough for us to recommend ☹

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2016年三月9日 10:41 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/16 "2016-03-09T10:41:29Z")

</div>

> [@Rob\_Nicholson](#):
>
> so smartphone and tablet friendly but Discourse isn’t sadly ticking all the boxes

I think that is a bit unfair, I use Discourse quite extensively over mobile as do many others, we have spent enormous amounts of engineering effort making sure the mobile story is great. To add to that you can do everything over email (reply, like, open topics etc) which also captures a bunch of use cases.

We plan to add support for push notification in a shell mobile application. However, the current experience is far superior to many other mobile sites I have seen and use. You achieve much of the notification over email anyway.

---

<div class="post-metadata">

### Author: ![Rob\_Nicholson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rob_nicholson/32/63483_2.png) [@Rob\_Nicholson](https://meta.discourse.org/u/Rob_Nicholson)
#### Post date: [2016年三月9日 11:53 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/17 "2016-03-09T11:53:10Z")

</div>

I’m not sure why it’s “unfair”. I like Discourse but to say it’s as mobile friendly as it _could_ be would be ignoring the truth. The stakeholders in our project are not IT specialists and like the rest of the world, have come to rely on their mobile phone. So when they say “why doesn’t it work like this?”, we’re immediately on the back foot defending the fact that Discourse is a web application and not a native one. I’ve asked whether I can start a separate thread on, for us, will be a key decision point.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2016年三月9日 15:34 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/18 "2016-03-09T15:34:57Z")

</div>

There are multiple threads on this, feel free to weigh in there

[https://meta.discourse.org/search?q=mobile%20discourse](https://meta.discourse.org/search?q=mobile%20discourse)

 ![](https://global.discourse-cdn.com/meta/original/3X/8/7/87e67a76960a131d1a94017d161b8b7842484113.png)

---

<div class="post-metadata">

### Author: ![Oskar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oskar/32/103381_2.png) [@Oskar](https://meta.discourse.org/u/Oskar)
#### Post date: [2016年四月3日 11:19 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/19 "2016-04-03T11:19:59Z")

</div>

I’d keep an eye on [ember-service-worker · Issue #45 · ember-cli/rfcs · GitHub](https://github.com/ember-cli/rfcs/issues/45) and [GitHub - html-next/skyrocket-original: Ambitious applications aren't single threaded. · GitHub](https://github.com/runspired/skyrocket) as well.

---

<div class="post-metadata">

### Author: ![dandv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dandv/32/169779_2.png) [@dandv](https://meta.discourse.org/u/dandv)
#### Post date: [2016年四月30日 22:11 UTC](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422/20 "2016-04-30T22:11:13Z")

</div>

Massive support signal from Google last week: the [2016 Founders’ Letter](https://googleblog.blogspot.com/2016/04/this-years-founders-letter.html) mentions [Progressive Web Apps](https://developers.google.com/web/progressive-web-apps?hl=en) as one of Google’s two investments in the mobile web:

> We also continue to invest in the mobile web […] Over this past year, Google has worked closely with publishers, developers, and others in the ecosystem to help make the mobile web a smoother, faster experience for users. A good example is […] **Progressive Web Apps (PWA)**, which combine the best of the web and the best of apps—allowing companies to build mobile sites that load quickly, send push notifications, have home screen icons, and much more.

Service Worker support is the cornerstone of PWA support.

As a Developer Advocate at Google helping with PWA and AMP efforts, please let me know if there’s anything we can do to help flesh out SW support - which I do see running at [https://meta.discourse.org](https://meta.discourse.org) when I use a mobile UA.

[下一頁](https://meta.discourse.org/t/exploring-serviceworkers-for-discourse/32422.md?page=2)
