Skip to content

Commit ec8616e

Browse files
committed
Service worker refactoring
1 parent 8ed1f4a commit ec8616e

2 files changed

Lines changed: 29 additions & 41 deletions

File tree

assets/javascripts/app/serviceworker.coffee

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class app.ServiceWorker
66

77
constructor: ->
88
@registration = null
9-
@installingRegistration = null
109
@notifyUpdate = true
1110

1211
navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
@@ -16,37 +15,30 @@ class app.ServiceWorker
1615
update: ->
1716
return unless @registration
1817
@notifyUpdate = true
19-
return @doUpdate()
18+
return @registration.update().catch(->)
2019

2120
updateInBackground: ->
2221
return unless @registration
2322
@notifyUpdate = false
24-
return @doUpdate()
23+
return @registration.update().catch(->)
2524

2625
reload: ->
2726
return @updateInBackground().then(() -> app.reboot())
2827

29-
doUpdate: ->
30-
return @registration.update().catch(->)
31-
3228
updateRegistration: (registration) ->
33-
$.off @registration, 'updatefound', @onUpdateFound if @registration
34-
$.off @installingRegistration, 'statechange', @onStateChange if @installingRegistration
35-
3629
@registration = registration
37-
@installingRegistration = null
38-
3930
$.on @registration, 'updatefound', @onUpdateFound
4031
return
4132

4233
onUpdateFound: () =>
34+
$.off @installingRegistration, 'statechange', @onStateChange() if @installingRegistration
4335
@installingRegistration = @registration.installing
4436
$.on @installingRegistration, 'statechange', @onStateChange
4537
return
4638

4739
onStateChange: () =>
48-
if @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
49-
@updateRegistration(@installingRegistration)
40+
if @installingRegistration and @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
41+
@installingRegistration = null
5042
@onUpdateReady()
5143
return
5244

views/service-worker.js.erb

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%# Use the hash of the application.js file as cache name, or 'app' if not running in production %>
1+
<%# Use the hash of the application.js file as cache name or 'app' if not running in production %>
22
<%# This ensures that the cache is always updated if the hash of the application.js file changes %>
33
const cacheName = '<%= javascript_path('application', asset_host: false).scan(/application-([^\.]+)\.js/).last&.first || 'app' %>';
44

@@ -28,37 +28,33 @@ self.addEventListener('install', event => {
2828

2929
<%# Remove old caches %>
3030
self.addEventListener('activate', event => {
31-
event.waitUntil(
32-
caches.keys().then(keys => Promise.all(
33-
keys.map(key => {
34-
if (key !== cacheName) {
35-
return caches.delete(key);
36-
}
37-
})
38-
))
39-
);
31+
event.waitUntil((async () => {
32+
const keys = await caches.keys();
33+
const jobs = keys.map(key => key !== cacheName ? caches.delete(key) : Promise.resolve());
34+
return await Promise.all(jobs);
35+
})());
4036
});
4137

4238
<%# Handle HTTP requests %>
4339
self.addEventListener('fetch', event => {
44-
event.respondWith(
45-
caches.match(event.request).then(response => {
46-
if (response) {
47-
return response;
40+
event.respondWith((async () => {
41+
const cachedResponse = await caches.match(event.request);
42+
if (cachedResponse) return cachedResponse;
43+
44+
try {
45+
const response = await fetch(event.request);
46+
return response;
47+
} catch (err) {
48+
const url = new URL(event.request.url);
49+
50+
<%# Attempt to return the index page from the cache if the user is visiting a url like devdocs.io/javascript/global_objects/array/find %>
51+
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
52+
if (url.origin === location.origin && !url.pathname.includes('.')) {
53+
const cachedIndex = await caches.match('/');
54+
if (cachedIndex) return cachedIndex;
4855
}
4956

50-
return fetch(event.request)
51-
.catch(err => {
52-
const url = new URL(event.request.url);
53-
54-
<%# Return the index page from the cache if the user is visiting a url like devdocs.io/javascript/global_objects/array/find %>
55-
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
56-
if (url.origin === location.origin && !url.pathname.includes('.')) {
57-
return caches.match('/').then(response => response || err);
58-
}
59-
60-
return err;
61-
});
62-
})
63-
);
57+
throw err;
58+
}
59+
})());
6460
});

0 commit comments

Comments
 (0)