2017-10-30 19:46:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
2023-06-09 06:14:11 -04:00
|
|
|
<%
|
|
|
|
base = if GlobalSetting.use_s3? && GlobalSetting.s3_cdn_url
|
|
|
|
GlobalSetting.s3_asset_cdn_url.presence || GlobalSetting.s3_cdn_url
|
|
|
|
elsif GlobalSetting.cdn_url
|
|
|
|
GlobalSetting.cdn_url + Discourse.base_path
|
|
|
|
else
|
|
|
|
Discourse.base_path
|
|
|
|
end
|
|
|
|
|
|
|
|
workbox_base = "#{base}/assets/#{EmberCli.workbox_dir_name}"
|
|
|
|
%>
|
|
|
|
|
|
|
|
importScripts("<%= "#{workbox_base}/workbox-sw.js" %>");
|
2019-07-15 12:05:55 -04:00
|
|
|
|
|
|
|
workbox.setConfig({
|
2023-06-09 06:14:11 -04:00
|
|
|
modulePathPrefix: "<%= workbox_base %>",
|
2021-02-15 23:01:19 -05:00
|
|
|
debug: false
|
2019-07-15 12:05:55 -04:00
|
|
|
});
|
|
|
|
|
2023-06-23 12:32:34 -04:00
|
|
|
var authUrls = ["auth", "session/sso_login", "session/sso", "srv/status"].map(path => `<%= Discourse.base_path %>/${path}`);
|
2022-11-11 10:30:21 -05:00
|
|
|
var chatRegex = /\/chat\/channel\/(\d+)\//;
|
|
|
|
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
|
2020-03-31 10:47:13 -04:00
|
|
|
|
2019-10-09 09:02:49 -04:00
|
|
|
var cacheVersion = "1";
|
2021-02-02 01:08:29 -05:00
|
|
|
var discourseCacheName = "discourse-" + cacheVersion;
|
|
|
|
var externalCacheName = "external-" + cacheVersion;
|
2019-07-15 12:05:55 -04:00
|
|
|
|
2022-01-18 14:41:25 -05: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;
|
2023-05-09 11:36:20 -04:00
|
|
|
var isApple = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
2021-02-02 01:08:29 -05:00
|
|
|
|
2022-01-18 14:41:25 -05:00
|
|
|
// Cache all GET requests, so Discourse can be used while offline
|
2019-07-15 12:05:55 -04:00
|
|
|
workbox.routing.registerRoute(
|
2020-03-31 10:47:13 -04:00
|
|
|
function(args) {
|
2023-01-30 11:23:19 -05:00
|
|
|
return args.url.origin === location.origin && !authUrls.some(u => args.url.pathname.startsWith(u)) && !isBrokenChrome97 && !isApple;
|
2020-03-31 10:47:13 -04:00
|
|
|
}, // Match all except auth routes
|
2019-07-15 12:05:55 -04:00
|
|
|
new workbox.strategies.NetworkFirst({ // This will only use the cache when a network request fails
|
2021-02-02 01:08:29 -05:00
|
|
|
cacheName: discourseCacheName,
|
2019-07-15 12:05:55 -04:00
|
|
|
plugins: [
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.cacheableResponse.CacheableResponsePlugin({
|
2021-02-02 01:08:29 -05:00
|
|
|
statuses: [200] // opaque responses will return status code '0'
|
2022-09-28 19:24:33 -04:00
|
|
|
}), // for s3 secure uploads signed urls
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.expiration.ExpirationPlugin({
|
2021-02-02 01:08:29 -05:00
|
|
|
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-02 15:52:01 -05:00
|
|
|
var cdnUrl = "<%= "#{GlobalSetting.cdn_url}" %>";
|
2021-02-02 01:08:29 -05:00
|
|
|
|
|
|
|
var appendQueryStringPlugin = {
|
|
|
|
requestWillFetch: function (args) {
|
|
|
|
var request = args.request;
|
|
|
|
|
2021-02-02 15:52:01 -05:00
|
|
|
if (request.url.startsWith(cdnUrl)) {
|
2021-02-02 01:08:29 -05: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: [
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.cacheableResponse.CacheableResponsePlugin({
|
2023-05-05 11:33:47 -04:00
|
|
|
statuses: [200] // opaque responses will return status code '0'
|
|
|
|
}),
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.expiration.ExpirationPlugin({
|
2021-02-02 01:08:29 -05:00
|
|
|
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: [
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.cacheableResponse.CacheableResponsePlugin({
|
2021-02-02 01:08:29 -05:00
|
|
|
statuses: [200] // opaque responses will return status code '0'
|
|
|
|
}),
|
2023-06-14 09:39:04 -04:00
|
|
|
new workbox.expiration.ExpirationPlugin({
|
2019-07-15 12:05:55 -04:00
|
|
|
maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
|
2021-02-02 01:08:29 -05:00
|
|
|
maxEntries: 250,
|
2019-07-27 10:52:21 -04:00
|
|
|
purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
|
2019-07-15 12:05:55 -04:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
);
|
2018-05-04 18:31:48 -04:00
|
|
|
|
2019-10-09 09:02:49 -04:00
|
|
|
var idleThresholdTime = 1000 * 10; // 10 seconds
|
2018-05-04 18:31:48 -04: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
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if (chatRegex.test(url)) {
|
|
|
|
notificationOptions['actions'] = [{
|
|
|
|
action: "reply",
|
|
|
|
title: "Reply",
|
|
|
|
placeholder: "reply",
|
|
|
|
type: "text",
|
|
|
|
icon: inlineReplyIcon
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
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;
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if (event.action === "reply") {
|
|
|
|
let csrf;
|
|
|
|
fetch("/session/csrf", {
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error("Network response was not OK");
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
csrf = data.csrf;
|
|
|
|
|
|
|
|
let chatTest = url.match(chatRegex);
|
|
|
|
if (chatTest.length > 0) {
|
|
|
|
let chatChannel = chatTest[1];
|
|
|
|
|
|
|
|
fetch(`${baseUrl}/chat/${chatChannel}.json`, {
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
|
|
"X-CSRF-Token": csrf,
|
|
|
|
},
|
|
|
|
body: `message=${event.reply}`,
|
|
|
|
method: "POST",
|
|
|
|
mode: "cors",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// 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) {
|
2018-05-04 18:31:48 -04:00
|
|
|
client.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if ("postMessage" in client && "focus" in client) {
|
2018-05-04 18:31:48 -04:00
|
|
|
client.focus();
|
|
|
|
client.postMessage({ url: url });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-11-11 10:30:21 -05:00
|
|
|
if (!reusedClientWindow && clients.openWindow)
|
|
|
|
return clients.openWindow(baseUrl + url);
|
2018-05-04 18:31:48 -04:00
|
|
|
})
|
2022-11-11 10:30:21 -05:00
|
|
|
);
|
|
|
|
}
|
2018-05-04 18:31:48 -04:00
|
|
|
});
|
|
|
|
|
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;
|
2021-02-08 10:09:52 -05: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-22 20:02:01 -05:00
|
|
|
<% DiscoursePluginRegistry.service_workers.each do |js| %>
|
|
|
|
<%=raw "#{File.read(js)}" %>
|
|
|
|
<% end %>
|