mirror of https://github.com/freeCodeCamp/devdocs
Merge pull request #1022 from jmerle/service-worker
Replace the App Cache with a Service Workerpull/1028/head
commit
6138f05bd7
@ -1,42 +0,0 @@
|
|||||||
class app.AppCache
|
|
||||||
$.extend @prototype, Events
|
|
||||||
|
|
||||||
@isEnabled: ->
|
|
||||||
try
|
|
||||||
applicationCache and applicationCache.status isnt applicationCache.UNCACHED
|
|
||||||
catch
|
|
||||||
|
|
||||||
constructor: ->
|
|
||||||
@cache = applicationCache
|
|
||||||
@notifyUpdate = true
|
|
||||||
@onUpdateReady() if @cache.status is @cache.UPDATEREADY
|
|
||||||
|
|
||||||
$.on @cache, 'progress', @onProgress
|
|
||||||
$.on @cache, 'updateready', @onUpdateReady
|
|
||||||
|
|
||||||
update: ->
|
|
||||||
@notifyUpdate = true
|
|
||||||
@notifyProgress = true
|
|
||||||
try @cache.update() catch
|
|
||||||
return
|
|
||||||
|
|
||||||
updateInBackground: ->
|
|
||||||
@notifyUpdate = false
|
|
||||||
@notifyProgress = false
|
|
||||||
try @cache.update() catch
|
|
||||||
return
|
|
||||||
|
|
||||||
reload: ->
|
|
||||||
$.on @cache, 'updateready noupdate error', -> app.reboot()
|
|
||||||
@notifyUpdate = false
|
|
||||||
@notifyProgress = true
|
|
||||||
try @cache.update() catch
|
|
||||||
return
|
|
||||||
|
|
||||||
onProgress: (event) =>
|
|
||||||
@trigger 'progress', event if @notifyProgress
|
|
||||||
return
|
|
||||||
|
|
||||||
onUpdateReady: =>
|
|
||||||
@trigger 'updateready' if @notifyUpdate
|
|
||||||
return
|
|
@ -0,0 +1,49 @@
|
|||||||
|
class app.ServiceWorker
|
||||||
|
$.extend @prototype, Events
|
||||||
|
|
||||||
|
@isEnabled: ->
|
||||||
|
!!navigator.serviceWorker and app.config.service_worker_enabled
|
||||||
|
|
||||||
|
constructor: ->
|
||||||
|
@registration = null
|
||||||
|
@notifyUpdate = true
|
||||||
|
|
||||||
|
navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
|
||||||
|
.then(
|
||||||
|
(registration) => @updateRegistration(registration),
|
||||||
|
(error) -> console.error('Could not register service worker:', error)
|
||||||
|
)
|
||||||
|
|
||||||
|
update: ->
|
||||||
|
return unless @registration
|
||||||
|
@notifyUpdate = true
|
||||||
|
return @registration.update().catch(->)
|
||||||
|
|
||||||
|
updateInBackground: ->
|
||||||
|
return unless @registration
|
||||||
|
@notifyUpdate = false
|
||||||
|
return @registration.update().catch(->)
|
||||||
|
|
||||||
|
reload: ->
|
||||||
|
return @updateInBackground().then(() -> app.reboot())
|
||||||
|
|
||||||
|
updateRegistration: (registration) ->
|
||||||
|
@registration = registration
|
||||||
|
$.on @registration, 'updatefound', @onUpdateFound
|
||||||
|
return
|
||||||
|
|
||||||
|
onUpdateFound: =>
|
||||||
|
$.off @installingRegistration, 'statechange', @onStateChange() if @installingRegistration
|
||||||
|
@installingRegistration = @registration.installing
|
||||||
|
$.on @installingRegistration, 'statechange', @onStateChange
|
||||||
|
return
|
||||||
|
|
||||||
|
onStateChange: =>
|
||||||
|
if @installingRegistration and @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
|
||||||
|
@installingRegistration = null
|
||||||
|
@onUpdateReady()
|
||||||
|
return
|
||||||
|
|
||||||
|
onUpdateReady: ->
|
||||||
|
@trigger 'updateready' if @notifyUpdate
|
||||||
|
return
|
After Width: | Height: | Size: 31 KiB |
@ -1,14 +0,0 @@
|
|||||||
CACHE MANIFEST
|
|
||||||
# <%= app_theme %> <%= app_size %> <%= app_layout %>
|
|
||||||
|
|
||||||
CACHE:
|
|
||||||
/
|
|
||||||
<%= manifest_asset_urls.join "\n" %>
|
|
||||||
<%= doc_index_urls.join "\n" %>
|
|
||||||
|
|
||||||
NETWORK:
|
|
||||||
/s/
|
|
||||||
*
|
|
||||||
|
|
||||||
FALLBACK:
|
|
||||||
/ /
|
|
@ -0,0 +1,49 @@
|
|||||||
|
<%# The name of the cache to store responses in %>
|
||||||
|
<%# If the cache name changes DevDocs is assumed to be updated %>
|
||||||
|
const cacheName = '<%= service_worker_cache_name %>';
|
||||||
|
|
||||||
|
<%# Url's to cache when the service worker is installed %>
|
||||||
|
const urlsToCache = [
|
||||||
|
'/',
|
||||||
|
'/favicon.ico',
|
||||||
|
'/manifest.json',
|
||||||
|
'<%= service_worker_asset_urls.join "',\n '" %>',
|
||||||
|
'<%= doc_index_urls.join "',\n '" %>',
|
||||||
|
];
|
||||||
|
|
||||||
|
<%# Set-up the cache %>
|
||||||
|
self.addEventListener('install', event => {
|
||||||
|
self.skipWaiting();
|
||||||
|
|
||||||
|
event.waitUntil(
|
||||||
|
caches.open(cacheName).then(cache => cache.addAll(urlsToCache)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
<%# Remove old caches %>
|
||||||
|
self.addEventListener('activate', event => {
|
||||||
|
event.waitUntil((async () => {
|
||||||
|
const keys = await caches.keys();
|
||||||
|
const jobs = keys.map(key => key !== cacheName ? caches.delete(key) : Promise.resolve());
|
||||||
|
return Promise.all(jobs);
|
||||||
|
})());
|
||||||
|
});
|
||||||
|
|
||||||
|
<%# Handle HTTP requests %>
|
||||||
|
self.addEventListener('fetch', event => {
|
||||||
|
event.respondWith((async () => {
|
||||||
|
const cachedResponse = await caches.match(event.request);
|
||||||
|
if (cachedResponse) return cachedResponse;
|
||||||
|
|
||||||
|
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/offline or devdocs.io/javascript/global_objects/array/find %>
|
||||||
|
<%# The index page will handle the routing %>
|
||||||
|
if (url.origin === location.origin && !url.pathname.includes('.')) {
|
||||||
|
const cachedIndex = await caches.match('/');
|
||||||
|
if (cachedIndex) return cachedIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(event.request);
|
||||||
|
})());
|
||||||
|
});
|
Loading…
Reference in new issue