2017-10-30 19:46:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Incrementing CACHE_VERSION will kick off the install event and force previously cached
|
|
|
|
// resources to be cached again.
|
|
|
|
const CACHE_VERSION = 1;
|
2017-11-22 20:02:01 -05:00
|
|
|
|
|
|
|
const CURRENT_CACHES = {
|
2017-10-30 19:46:48 -04:00
|
|
|
offline: 'offline-v' + CACHE_VERSION
|
|
|
|
};
|
2017-11-22 20:02:01 -05:00
|
|
|
|
2017-10-30 19:46:48 -04:00
|
|
|
const OFFLINE_URL = 'offline.html';
|
|
|
|
|
|
|
|
function createCacheBustedRequest(url) {
|
2018-02-13 00:19:43 -05:00
|
|
|
var headers = new Headers({
|
|
|
|
'Discourse-Track-View': '0'
|
|
|
|
});
|
|
|
|
|
|
|
|
var request = new Request(url, {cache: 'reload', headers: headers});
|
2017-10-30 19:46:48 -04:00
|
|
|
// See https://fetch.spec.whatwg.org/#concept-request-mode
|
|
|
|
// This is not yet supported in Chrome as of M48, so we need to explicitly check to see
|
|
|
|
// if the cache: 'reload' option had any effect.
|
|
|
|
if ('cache' in request) {
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If {cache: 'reload'} didn't have any effect, append a cache-busting URL parameter instead.
|
2017-11-28 03:07:27 -05:00
|
|
|
var bustedUrl = new URL(url, self.location.href);
|
|
|
|
bustedUrl.search += (bustedUrl.search ? '&' : '') + 'cachebust=' + Date.now();
|
2018-02-13 00:19:43 -05:00
|
|
|
return new Request(bustedUrl, {headers: headers});
|
2017-10-30 19:46:48 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 03:07:27 -05:00
|
|
|
self.addEventListener('install', function(event) {
|
2017-10-30 19:46:48 -04:00
|
|
|
event.waitUntil(
|
|
|
|
// We can't use cache.add() here, since we want OFFLINE_URL to be the cache key, but
|
|
|
|
// the actual URL we end up requesting might include a cache-busting parameter.
|
|
|
|
fetch(createCacheBustedRequest(OFFLINE_URL)).then(function(response) {
|
|
|
|
return caches.open(CURRENT_CACHES.offline).then(function(cache) {
|
|
|
|
return cache.put(OFFLINE_URL, response);
|
|
|
|
});
|
2018-05-14 15:23:54 -04:00
|
|
|
}).then(function(cache) {
|
|
|
|
self.skipWaiting();
|
2017-10-30 19:46:48 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-11-28 03:07:27 -05:00
|
|
|
self.addEventListener('activate', function(event) {
|
2017-10-30 19:46:48 -04:00
|
|
|
// Delete all caches that aren't named in CURRENT_CACHES.
|
|
|
|
// While there is only one cache in this example, the same logic will handle the case where
|
|
|
|
// there are multiple versioned caches.
|
2017-11-28 03:07:27 -05:00
|
|
|
var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
|
2017-10-30 19:46:48 -04:00
|
|
|
return CURRENT_CACHES[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
event.waitUntil(
|
2017-11-28 03:07:27 -05:00
|
|
|
caches.keys().then(function(cacheNames) {
|
2017-10-30 19:46:48 -04:00
|
|
|
return Promise.all(
|
2017-11-28 03:07:27 -05:00
|
|
|
cacheNames.map(function(cacheName) {
|
2017-10-30 19:46:48 -04:00
|
|
|
if (expectedCacheNames.indexOf(cacheName) === -1) {
|
|
|
|
// If this cache name isn't present in the array of "expected" cache names,
|
|
|
|
// then delete it.
|
|
|
|
return caches.delete(cacheName);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2018-05-14 15:23:54 -04:00
|
|
|
}).then(function() {
|
|
|
|
self.clients.claim()
|
2017-10-30 19:46:48 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-11-28 03:07:27 -05:00
|
|
|
self.addEventListener('fetch', function(event) {
|
2018-05-15 17:18:45 -04:00
|
|
|
// Bypass service workers if this is a url with a token param
|
|
|
|
if(/\?.*token/i.test(event.request.url)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-30 19:46:48 -04:00
|
|
|
// We only want to call event.respondWith() if this is a navigation request
|
|
|
|
// for an HTML page.
|
|
|
|
// request.mode of 'navigate' is unfortunately not supported in Chrome
|
|
|
|
// versions older than 49, so we need to include a less precise fallback,
|
|
|
|
// which checks for a GET request with an Accept: text/html header.
|
|
|
|
if (event.request.mode === 'navigate' ||
|
|
|
|
(event.request.method === 'GET' &&
|
|
|
|
event.request.headers.get('accept').includes('text/html'))) {
|
|
|
|
event.respondWith(
|
2017-11-28 03:07:27 -05:00
|
|
|
fetch(event.request).catch(function(error) {
|
2017-10-30 19:46:48 -04:00
|
|
|
// The catch is only triggered if fetch() throws an exception, which will most likely
|
|
|
|
// happen due to the server being unreachable.
|
|
|
|
// If fetch() returns a valid HTTP response with an response code in the 4xx or 5xx
|
|
|
|
// range, the catch() will NOT be called. If you need custom handling for 4xx or 5xx
|
|
|
|
// errors, see https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker/fallback-response
|
2018-04-22 22:21:51 -04:00
|
|
|
if (!navigator.onLine) {
|
|
|
|
return caches.match(OFFLINE_URL);
|
|
|
|
} else {
|
2018-06-05 10:43:45 -04:00
|
|
|
throw new Error(error);
|
2018-04-22 22:21:51 -04:00
|
|
|
}
|
2017-10-30 19:46:48 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our if() condition is false, then this fetch handler won't intercept the request.
|
|
|
|
// If there are any other fetch handlers registered, they will get a chance to call
|
|
|
|
// event.respondWith(). If no fetch handlers call event.respondWith(), the request will be
|
|
|
|
// handled by the browser as if there were no service worker involvement.
|
|
|
|
});
|
2017-11-22 20:02:01 -05:00
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
|
|
|
|
const idleThresholdTime = 1000 * 10; // 10 seconds
|
|
|
|
var lastAction = -1;
|
|
|
|
|
|
|
|
function isIdle() {
|
|
|
|
return lastAction + idleThresholdTime < Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
function showNotification(title, body, icon, badge, tag, baseUrl, url) {
|
|
|
|
var notificationOptions = {
|
|
|
|
body: body,
|
|
|
|
icon: icon,
|
|
|
|
badge: badge,
|
|
|
|
data: { url: url, baseUrl: baseUrl },
|
|
|
|
tag: tag
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.registration.showNotification(title, notificationOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('push', function(event) {
|
|
|
|
var payload = event.data.json();
|
|
|
|
if(!isIdle() && payload.hide_when_active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
self.registration.getNotifications({ tag: payload.tag }).then(function(notifications) {
|
|
|
|
if (notifications && notifications.length > 0) {
|
|
|
|
notifications.forEach(function(notification) {
|
|
|
|
notification.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return showNotification(payload.title, payload.body, payload.icon, payload.badge, payload.tag, payload.base_url, payload.url);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('notificationclick', function(event) {
|
|
|
|
// Android doesn't close the notification when you click on it
|
|
|
|
// See: http://crbug.com/463146
|
|
|
|
event.notification.close();
|
|
|
|
var url = event.notification.data.url;
|
|
|
|
var baseUrl = event.notification.data.baseUrl;
|
|
|
|
|
|
|
|
// This looks to see if the current window is already open and
|
|
|
|
// focuses if it is
|
|
|
|
event.waitUntil(
|
|
|
|
clients.matchAll({ type: "window" })
|
|
|
|
.then(function(clientList) {
|
|
|
|
var reusedClientWindow = clientList.some(function(client) {
|
|
|
|
if (client.url === baseUrl + url && 'focus' in client) {
|
|
|
|
client.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('postMessage' in client && 'focus' in client) {
|
|
|
|
client.focus();
|
|
|
|
client.postMessage({ url: url });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!reusedClientWindow && clients.openWindow) return clients.openWindow(baseUrl + url);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2018-05-04 18:42:48 -04:00
|
|
|
self.addEventListener('message', function(event) {
|
2018-05-04 18:31:48 -04:00
|
|
|
if('lastAction' in event.data){
|
|
|
|
lastAction = event.data.lastAction;
|
|
|
|
}});
|
2017-11-22 20:02:01 -05:00
|
|
|
<% DiscoursePluginRegistry.service_workers.each do |js| %>
|
|
|
|
<%=raw "#{File.read(js)}" %>
|
|
|
|
<% end %>
|