mirror of https://github.com/freeCodeCamp/devdocs
commit
8a71af9c2f
@ -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
|
@ -1,28 +1,32 @@
|
|||||||
try {
|
try {
|
||||||
if (app.config.env == 'production') {
|
if (app.config.env === 'production') {
|
||||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
if (Cookies.get('analyticsConsent') === '1') {
|
||||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||||
ga('create', 'UA-5544833-12', 'devdocs.io');
|
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
||||||
page.track(function() {
|
ga('create', 'UA-5544833-12', 'devdocs.io');
|
||||||
ga('send', 'pageview', {
|
page.track(function() {
|
||||||
page: location.pathname + location.search + location.hash,
|
ga('send', 'pageview', {
|
||||||
dimension1: app.router.context && app.router.context.doc && app.router.context.doc.slug_without_version
|
page: location.pathname + location.search + location.hash,
|
||||||
|
dimension1: app.router.context && app.router.context.doc && app.router.context.doc.slug_without_version
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
page.track(function() {
|
page.track(function() {
|
||||||
if (window._gauges)
|
if (window._gauges)
|
||||||
_gauges.push(['track']);
|
_gauges.push(['track']);
|
||||||
else
|
else
|
||||||
(function() {
|
(function() {
|
||||||
var _gauges=_gauges||[];!function(){var a=document.createElement("script");
|
var _gauges=_gauges||[];!function(){var a=document.createElement("script");
|
||||||
a.type="text/javascript",a.async=!0,a.id="gauges-tracker",
|
a.type="text/javascript",a.async=!0,a.id="gauges-tracker",
|
||||||
a.setAttribute("data-site-id","51c15f82613f5d7819000067"),
|
a.setAttribute("data-site-id","51c15f82613f5d7819000067"),
|
||||||
a.src="https://secure.gaug.es/track.js";var b=document.getElementsByTagName("script")[0];
|
a.src="https://secure.gaug.es/track.js";var b=document.getElementsByTagName("script")[0];
|
||||||
b.parentNode.insertBefore(a,b)}();
|
b.parentNode.insertBefore(a,b)}();
|
||||||
})();
|
})();
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
resetAnalytics();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch(e) { }
|
} catch(e) { }
|
||||||
|
@ -1 +0,0 @@
|
|||||||
[]
|
|
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