Service worker refactoring

pull/1022/head
Jasper van Merle 6 years ago
parent 8ed1f4ace1
commit ec8616e289

@ -6,7 +6,6 @@ class app.ServiceWorker
constructor: -> constructor: ->
@registration = null @registration = null
@installingRegistration = null
@notifyUpdate = true @notifyUpdate = true
navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'}) navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
@ -16,37 +15,30 @@ class app.ServiceWorker
update: -> update: ->
return unless @registration return unless @registration
@notifyUpdate = true @notifyUpdate = true
return @doUpdate() return @registration.update().catch(->)
updateInBackground: -> updateInBackground: ->
return unless @registration return unless @registration
@notifyUpdate = false @notifyUpdate = false
return @doUpdate() return @registration.update().catch(->)
reload: -> reload: ->
return @updateInBackground().then(() -> app.reboot()) return @updateInBackground().then(() -> app.reboot())
doUpdate: ->
return @registration.update().catch(->)
updateRegistration: (registration) -> updateRegistration: (registration) ->
$.off @registration, 'updatefound', @onUpdateFound if @registration
$.off @installingRegistration, 'statechange', @onStateChange if @installingRegistration
@registration = registration @registration = registration
@installingRegistration = null
$.on @registration, 'updatefound', @onUpdateFound $.on @registration, 'updatefound', @onUpdateFound
return return
onUpdateFound: () => onUpdateFound: () =>
$.off @installingRegistration, 'statechange', @onStateChange() if @installingRegistration
@installingRegistration = @registration.installing @installingRegistration = @registration.installing
$.on @installingRegistration, 'statechange', @onStateChange $.on @installingRegistration, 'statechange', @onStateChange
return return
onStateChange: () => onStateChange: () =>
if @installingRegistration.state == 'installed' and navigator.serviceWorker.controller if @installingRegistration and @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
@updateRegistration(@installingRegistration) @installingRegistration = null
@onUpdateReady() @onUpdateReady()
return return

@ -1,4 +1,4 @@
<%# Use the hash of the application.js file as cache name, or 'app' if not running in production %> <%# Use the hash of the application.js file as cache name or 'app' if not running in production %>
<%# This ensures that the cache is always updated if the hash of the application.js file changes %> <%# This ensures that the cache is always updated if the hash of the application.js file changes %>
const cacheName = '<%= javascript_path('application', asset_host: false).scan(/application-([^\.]+)\.js/).last&.first || 'app' %>'; const cacheName = '<%= javascript_path('application', asset_host: false).scan(/application-([^\.]+)\.js/).last&.first || 'app' %>';
@ -28,37 +28,33 @@ self.addEventListener('install', event => {
<%# Remove old caches %> <%# Remove old caches %>
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil( event.waitUntil((async () => {
caches.keys().then(keys => Promise.all( const keys = await caches.keys();
keys.map(key => { const jobs = keys.map(key => key !== cacheName ? caches.delete(key) : Promise.resolve());
if (key !== cacheName) { return await Promise.all(jobs);
return caches.delete(key); })());
}
})
))
);
}); });
<%# Handle HTTP requests %> <%# Handle HTTP requests %>
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
event.respondWith( event.respondWith((async () => {
caches.match(event.request).then(response => { const cachedResponse = await caches.match(event.request);
if (response) { if (cachedResponse) return cachedResponse;
return response;
try {
const response = await fetch(event.request);
return response;
} catch (err) {
const url = new URL(event.request.url);
<%# Attempt to return the index page from the cache if the user is visiting a url like devdocs.io/javascript/global_objects/array/find %>
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
if (url.origin === location.origin && !url.pathname.includes('.')) {
const cachedIndex = await caches.match('/');
if (cachedIndex) return cachedIndex;
} }
return fetch(event.request) throw err;
.catch(err => { }
const url = new URL(event.request.url); })());
<%# Return the index page from the cache if the user is visiting a url like devdocs.io/javascript/global_objects/array/find %>
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
if (url.origin === location.origin && !url.pathname.includes('.')) {
return caches.match('/').then(response => response || err);
}
return err;
});
})
);
}); });

Loading…
Cancel
Save