2017-10-31 07:46:48 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-08-06 03:07:49 +08:00
|
|
|
importScripts("<%= "#{Discourse.asset_host}#{Discourse.base_path}/javascripts/workbox/workbox-sw.js" %>");
|
2019-07-16 00:05:55 +08:00
|
|
|
|
|
|
|
workbox.setConfig({
|
2019-08-06 03:07:49 +08:00
|
|
|
modulePathPrefix: "<%= "#{Discourse.asset_host}#{Discourse.base_path}/javascripts/workbox" %>",
|
2021-02-16 12:01:19 +08:00
|
|
|
debug: false
|
2019-07-16 00:05:55 +08:00
|
|
|
});
|
|
|
|
|
2022-01-18 19:27:01 +08:00
|
|
|
var authUrls = ["auth", "session/sso_login", "session/sso"].map(path => `<%= Discourse.base_path %>/${path}`);
|
2020-03-31 22:47:13 +08:00
|
|
|
|
2019-10-09 21:02:49 +08:00
|
|
|
var cacheVersion = "1";
|
2021-02-02 14:08:29 +08:00
|
|
|
var discourseCacheName = "discourse-" + cacheVersion;
|
|
|
|
var externalCacheName = "external-" + cacheVersion;
|
2019-07-16 00:05:55 +08:00
|
|
|
|
2022-01-19 03:41:25 +08:00
|
|
|
// Chrome 97 shipped with broken samesite cookie handling when proxying requests through service workers
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=1286367
|
|
|
|
var chromeVersionMatch = navigator.userAgent.match(/Chrome\/97.0.(\d+)/);
|
|
|
|
var isBrokenChrome97 = chromeVersionMatch && parseInt(chromeVersionMatch[1]) <= 4692;
|
2021-02-02 14:08:29 +08:00
|
|
|
|
2022-01-19 03:41:25 +08:00
|
|
|
// Cache all GET requests, so Discourse can be used while offline
|
2019-07-16 00:05:55 +08:00
|
|
|
workbox.routing.registerRoute(
|
2020-03-31 22:47:13 +08:00
|
|
|
function(args) {
|
2022-01-19 03:41:25 +08:00
|
|
|
return args.url.origin === location.origin && !authUrls.some(u => args.url.pathname.startsWith(u)) && !isBrokenChrome97;
|
2020-03-31 22:47:13 +08:00
|
|
|
}, // Match all except auth routes
|
2019-07-16 00:05:55 +08:00
|
|
|
new workbox.strategies.NetworkFirst({ // This will only use the cache when a network request fails
|
2021-02-02 14:08:29 +08:00
|
|
|
cacheName: discourseCacheName,
|
2019-07-16 00:05:55 +08:00
|
|
|
plugins: [
|
2021-02-02 14:08:29 +08:00
|
|
|
new workbox.cacheableResponse.Plugin({
|
|
|
|
statuses: [200] // opaque responses will return status code '0'
|
|
|
|
}), // for s3 secure media signed urls
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
|
|
|
|
maxEntries: 250,
|
|
|
|
purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
var cdnUrls = [];
|
|
|
|
|
|
|
|
<% if GlobalSetting.try(:cdn_cors_enabled) %>
|
|
|
|
cdnUrls = ["<%= "#{GlobalSetting.s3_cdn_url}" %>", "<%= "#{GlobalSetting.cdn_url}" %>"].filter(Boolean);
|
|
|
|
|
|
|
|
if (cdnUrls.length > 0) {
|
|
|
|
var cdnCacheName = "cdn-" + cacheVersion;
|
2021-02-03 04:52:01 +08:00
|
|
|
var cdnUrl = "<%= "#{GlobalSetting.cdn_url}" %>";
|
2021-02-02 14:08:29 +08:00
|
|
|
|
|
|
|
var appendQueryStringPlugin = {
|
|
|
|
requestWillFetch: function (args) {
|
|
|
|
var request = args.request;
|
|
|
|
|
2021-02-03 04:52:01 +08:00
|
|
|
if (request.url.startsWith(cdnUrl)) {
|
2021-02-02 14:08:29 +08:00
|
|
|
var url = new URL(request.url);
|
|
|
|
// Using this temporary query param to force browsers to redownload images from server.
|
|
|
|
url.searchParams.append('refresh', 'true');
|
|
|
|
return new Request(url.href, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
function(args) {
|
|
|
|
var matching = cdnUrls.filter(
|
|
|
|
function(url) {
|
|
|
|
return args.url.href.startsWith(url);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return matching.length > 0;
|
|
|
|
}, // Match all cdn resources
|
|
|
|
new workbox.strategies.NetworkFirst({ // This will only use the cache when a network request fails
|
|
|
|
cacheName: cdnCacheName,
|
|
|
|
fetchOptions: {
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'omit'
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
|
|
|
|
maxEntries: 250,
|
|
|
|
purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
|
|
|
|
}),
|
|
|
|
appendQueryStringPlugin
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
function(args) {
|
|
|
|
if (args.url.origin === location.origin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var matching = cdnUrls.filter(
|
|
|
|
function(url) {
|
|
|
|
return args.url.href.startsWith(url);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return matching.length === 0;
|
|
|
|
}, // Match all other external resources
|
|
|
|
new workbox.strategies.NetworkFirst({ // This will only use the cache when a network request fails
|
|
|
|
cacheName: externalCacheName,
|
|
|
|
plugins: [
|
|
|
|
new workbox.cacheableResponse.Plugin({
|
|
|
|
statuses: [200] // opaque responses will return status code '0'
|
|
|
|
}),
|
2019-07-16 00:05:55 +08:00
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
|
2021-02-02 14:08:29 +08:00
|
|
|
maxEntries: 250,
|
2019-07-27 22:52:21 +08:00
|
|
|
purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
|
2019-07-16 00:05:55 +08:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
2018-05-05 06:31:48 +08:00
|
|
|
|
2019-10-09 21:02:49 +08:00
|
|
|
var idleThresholdTime = 1000 * 10; // 10 seconds
|
2018-05-05 06:31:48 +08:00
|
|
|
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-05 06:42:48 +08:00
|
|
|
self.addEventListener('message', function(event) {
|
2018-05-05 06:31:48 +08:00
|
|
|
if('lastAction' in event.data){
|
|
|
|
lastAction = event.data.lastAction;
|
2021-02-08 23:09:52 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('pushsubscriptionchange', function(event) {
|
|
|
|
event.waitUntil(
|
|
|
|
Promise.all(
|
|
|
|
fetch('<%= Discourse.base_url %>/push_notifications/subscribe', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
|
|
|
body: new URLSearchParams({
|
|
|
|
"subscription[endpoint]": event.newSubscription.endpoint,
|
|
|
|
"subscription[keys][auth]": event.newSubscription.toJSON().keys.auth,
|
|
|
|
"subscription[keys][p256dh]": event.newSubscription.toJSON().keys.p256dh,
|
|
|
|
"send_confirmation": false
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
fetch('<%= Discourse.base_url %>/push_notifications/unsubscribe', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
|
|
|
body: new URLSearchParams({
|
|
|
|
"subscription[endpoint]": event.oldSubscription.endpoint,
|
|
|
|
"subscription[keys][auth]": event.oldSubscription.toJSON().keys.auth,
|
|
|
|
"subscription[keys][p256dh]": event.oldSubscription.toJSON().keys.p256dh
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-11-23 09:02:01 +08:00
|
|
|
<% DiscoursePluginRegistry.service_workers.each do |js| %>
|
|
|
|
<%=raw "#{File.read(js)}" %>
|
|
|
|
<% end %>
|