Skip to content

Commit 8ed1f4a

Browse files
committed
Replace AppCache with a service worker
1 parent 5edbb16 commit 8ed1f4a

17 files changed

Lines changed: 144 additions & 144 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Contributions are welcome. Please read the [contributing guidelines](./.github/C
156156
* [Doc Browser](https://github.com/qwfy/doc-browser) is a native Linux app that supports DevDocs docsets
157157
* [GNOME Application](https://github.com/hardpixel/devdocs-desktop) GTK3 application with search integrated in headerbar
158158
* [macOS Application](https://github.com/dteoh/devdocs-macos)
159-
* [Android Application](https://github.com/Merith-TK/devdocs_webapp_kotlin) is a fully working, advanced WebView with AppCache enabled
159+
* [Android Application](https://github.com/Merith-TK/devdocs_webapp_kotlin) is a fully working, advanced WebView
160160

161161
## Copyright / License
162162

assets/javascripts/app/app.coffee

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@el = $('._app')
1515
@localStorage = new LocalStorageStore
16-
@appCache = new app.AppCache if app.AppCache.isEnabled()
16+
@serviceWorker = new app.ServiceWorker if app.ServiceWorker.isEnabled()
1717
@settings = new app.Settings
1818
@db = new app.DB()
1919

@@ -149,7 +149,7 @@
149149
saveDocs: ->
150150
@settings.setDocs(doc.slug for doc in @docs.all())
151151
@db.migrate()
152-
@appCache?.updateInBackground()
152+
@serviceWorker?.updateInBackground()
153153

154154
welcomeBack: ->
155155
visitCount = @settings.get('count')
@@ -169,14 +169,14 @@
169169
reload: ->
170170
@docs.clearCache()
171171
@disabledDocs.clearCache()
172-
if @appCache then @appCache.reload() else @reboot()
172+
if @serviceWorker then @serviceWorker.reload() else @reboot()
173173
return
174174

175175
reset: ->
176176
@localStorage.reset()
177177
@settings.reset()
178178
@db?.reset()
179-
@appCache?.update()
179+
@serviceWorker?.update()
180180
window.location = '/'
181181
return
182182

@@ -195,9 +195,9 @@
195195
return
196196

197197
indexHost: ->
198-
# Can't load the index files from the host/CDN when applicationCache is
198+
# Can't load the index files from the host/CDN when service worker is
199199
# enabled because it doesn't support caching URLs that use CORS.
200-
@config[if @appCache and @settings.hasDocs() then 'index_path' else 'docs_origin']
200+
@config[if @serviceWorker and @settings.hasDocs() then 'index_path' else 'docs_origin']
201201

202202
onBootError: (args...) ->
203203
@trigger 'bootError'

assets/javascripts/app/appcache.coffee

Lines changed: 0 additions & 42 deletions
This file was deleted.

assets/javascripts/app/config.coffee.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ app.config =
1313
version: <%= Time.now.to_i %>
1414
release: <%= Time.now.utc.httpdate.to_json %>
1515
mathml_stylesheet: '<%= App.cdn_origin %>/mathml.css'
16+
service_worker_path: '/service-worker.js'
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class app.ServiceWorker
2+
$.extend @prototype, Events
3+
4+
@isEnabled: ->
5+
!!navigator.serviceWorker
6+
7+
constructor: ->
8+
@registration = null
9+
@installingRegistration = null
10+
@notifyUpdate = true
11+
12+
navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
13+
.then((registration) => @updateRegistration(registration))
14+
.catch((error) -> console.error 'Could not register service worker:', error)
15+
16+
update: ->
17+
return unless @registration
18+
@notifyUpdate = true
19+
return @doUpdate()
20+
21+
updateInBackground: ->
22+
return unless @registration
23+
@notifyUpdate = false
24+
return @doUpdate()
25+
26+
reload: ->
27+
return @updateInBackground().then(() -> app.reboot())
28+
29+
doUpdate: ->
30+
return @registration.update().catch(->)
31+
32+
updateRegistration: (registration) ->
33+
$.off @registration, 'updatefound', @onUpdateFound if @registration
34+
$.off @installingRegistration, 'statechange', @onStateChange if @installingRegistration
35+
36+
@registration = registration
37+
@installingRegistration = null
38+
39+
$.on @registration, 'updatefound', @onUpdateFound
40+
return
41+
42+
onUpdateFound: () =>
43+
@installingRegistration = @registration.installing
44+
$.on @installingRegistration, 'statechange', @onStateChange
45+
return
46+
47+
onStateChange: () =>
48+
if @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
49+
@updateRegistration(@installingRegistration)
50+
@onUpdateReady()
51+
return
52+
53+
onUpdateReady: ->
54+
@trigger 'updateready' if @notifyUpdate
55+
return

assets/javascripts/app/update_checker.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ class app.UpdateChecker
33
@lastCheck = Date.now()
44

55
$.on window, 'focus', @onFocus
6-
app.appCache.on 'updateready', @onUpdateReady if app.appCache
6+
app.serviceWorker.on 'updateready', @onUpdateReady if app.serviceWorker
77

88
setTimeout @checkDocs, 0
99

1010
check: ->
11-
if app.appCache
12-
app.appCache.update()
11+
if app.serviceWorker
12+
app.serviceWorker.update()
1313
else
1414
ajax
1515
url: $('script[src*="application"]').getAttribute('src')

assets/javascripts/templates/pages/offline_tmpl.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ app.templates.offlinePage = (docs) -> """
2626
<dl>
2727
<dt>How does this work?
2828
<dd>Each page is cached as a key-value pair in <a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API">IndexedDB</a> (downloaded from a single file).<br>
29-
The app also uses <a href="proxy.php?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2F%3Cspan+class%3D"x x-first x-last">HTML/Using_the_application_cache">AppCache</a> and <a href="proxy.php?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FWeb_Storage_API">localStorage</a> to cache the assets and index files.
29+
The app also uses <a href="proxy.php?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2F%3Cspan+class%3D"x x-first x-last">API/Service_Worker_API/Using_Service_Workers">Service Workers</a> and <a href="proxy.php?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FWeb_Storage_API">localStorage</a> to cache the assets and index files.
3030
<dt>Can I close the tab/browser?
3131
<dd>#{canICloseTheTab()}
3232
<dt>What if I don't update a documentation?
@@ -41,10 +41,10 @@ app.templates.offlinePage = (docs) -> """
4141
"""
4242

4343
canICloseTheTab = ->
44-
if app.AppCache.isEnabled()
44+
if app.ServiceWorker.isEnabled()
4545
""" Yes! Even offline, you can open a new tab, go to <a href="//devdocs.io">devdocs.io</a>, and everything will work as if you were online (provided you installed all the documentations you want to use beforehand). """
4646
else
47-
""" No. AppCache isn't available in your browser (or is disabled), so loading <a href="proxy.php?url=https%3A%2F%2Fdevdocs.io">devdocs.io</a> offline won't work.<br>
47+
""" No. Service Workers aren't available in your browser (or are disabled), so loading <a href="proxy.php?url=https%3A%2F%2Fdevdocs.io">devdocs.io</a> offline won't work.<br>
4848
The current tab will continue to function even when you go offline (provided you installed all the documentations beforehand). """
4949

5050
app.templates.offlineDoc = (doc, status) ->

assets/javascripts/views/content/entry_page.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class app.views.EntryPage extends app.View
123123
@render @tmpl('pageLoadError')
124124
@resetClass()
125125
@addClass @constructor.errorClass
126-
app.appCache?.update()
126+
app.serviceWorker?.update()
127127
return
128128

129129
cache: ->

assets/javascripts/views/content/settings_page.coffee

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ class app.views.SettingsPage extends app.View
2222

2323
toggleDark: (enable) ->
2424
app.settings.set('dark', !!enable)
25-
app.appCache?.updateInBackground()
2625
return
2726

2827
toggleLayout: (layout, enable) ->
2928
app.settings.setLayout(layout, enable)
30-
app.appCache?.updateInBackground()
3129
return
3230

3331
toggleSmoothScroll: (enable) ->

assets/javascripts/views/layout/resizer.coffee

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class app.views.Resizer extends app.View
2626
newSize = "#{value}px"
2727
@style.innerHTML = @style.innerHTML.replace(new RegExp(@size, 'g'), newSize)
2828
@size = newSize
29-
if save
30-
app.settings.setSize(value)
31-
app.appCache?.updateInBackground()
29+
app.settings.setSize(value) if save
3230
return
3331

3432
onDragStart: (event) =>

0 commit comments

Comments
 (0)