mirror of https://github.com/freeCodeCamp/devdocs
parent
5edbb16c1b
commit
8ed1f4ace1
@ -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,55 @@
|
||||
class app.ServiceWorker
|
||||
$.extend @prototype, Events
|
||||
|
||||
@isEnabled: ->
|
||||
!!navigator.serviceWorker
|
||||
|
||||
constructor: ->
|
||||
@registration = null
|
||||
@installingRegistration = null
|
||||
@notifyUpdate = true
|
||||
|
||||
navigator.serviceWorker.register(app.config.service_worker_path, {scope: '/'})
|
||||
.then((registration) => @updateRegistration(registration))
|
||||
.catch((error) -> console.error 'Could not register service worker:', error)
|
||||
|
||||
update: ->
|
||||
return unless @registration
|
||||
@notifyUpdate = true
|
||||
return @doUpdate()
|
||||
|
||||
updateInBackground: ->
|
||||
return unless @registration
|
||||
@notifyUpdate = false
|
||||
return @doUpdate()
|
||||
|
||||
reload: ->
|
||||
return @updateInBackground().then(() -> app.reboot())
|
||||
|
||||
doUpdate: ->
|
||||
return @registration.update().catch(->)
|
||||
|
||||
updateRegistration: (registration) ->
|
||||
$.off @registration, 'updatefound', @onUpdateFound if @registration
|
||||
$.off @installingRegistration, 'statechange', @onStateChange if @installingRegistration
|
||||
|
||||
@registration = registration
|
||||
@installingRegistration = null
|
||||
|
||||
$.on @registration, 'updatefound', @onUpdateFound
|
||||
return
|
||||
|
||||
onUpdateFound: () =>
|
||||
@installingRegistration = @registration.installing
|
||||
$.on @installingRegistration, 'statechange', @onStateChange
|
||||
return
|
||||
|
||||
onStateChange: () =>
|
||||
if @installingRegistration.state == 'installed' and navigator.serviceWorker.controller
|
||||
@updateRegistration(@installingRegistration)
|
||||
@onUpdateReady()
|
||||
return
|
||||
|
||||
onUpdateReady: ->
|
||||
@trigger 'updateready' if @notifyUpdate
|
||||
return
|
@ -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,64 @@
|
||||
<%# 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 %>
|
||||
const cacheName = '<%= javascript_path('application', asset_host: false).scan(/application-([^\.]+)\.js/).last&.first || 'app' %>';
|
||||
|
||||
<%# Paths to cache when the service worker is installed %>
|
||||
const cachePaths = [
|
||||
'/',
|
||||
'/favicon.ico',
|
||||
'/manifest.json',
|
||||
'/images/webapp-icon-32.png',
|
||||
'/images/webapp-icon-60.png',
|
||||
'/images/webapp-icon-80.png',
|
||||
'/images/webapp-icon-128.png',
|
||||
'/images/webapp-icon-256.png',
|
||||
'/images/webapp-icon-512.png',
|
||||
'<%= manifest_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(cachePaths)),
|
||||
);
|
||||
});
|
||||
|
||||
<%# Remove old caches %>
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil(
|
||||
caches.keys().then(keys => Promise.all(
|
||||
keys.map(key => {
|
||||
if (key !== cacheName) {
|
||||
return caches.delete(key);
|
||||
}
|
||||
})
|
||||
))
|
||||
);
|
||||
});
|
||||
|
||||
<%# Handle HTTP requests %>
|
||||
self.addEventListener('fetch', event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(response => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
return fetch(event.request)
|
||||
.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…
Reference in new issue