FEATURE: disable service worker for all browsers except for android

Service worker is still quite experimental, only enable on android
where it provides many benefits
This commit is contained in:
Sam 2018-01-24 12:03:08 +11:00
parent c42bb94362
commit 7ba06de0d6
2 changed files with 40 additions and 2 deletions

View File

@ -1,12 +1,44 @@
function deRegister() {
if (navigator.serviceWorker.getRegistrations) {
navigator.serviceWorker.getRegistrations().then(r => {
r.forEach(reg => {
if (reg.active && reg.active.scriptURL.indexOf('service-worker.js') > 0) {
reg.unregister();
}
});
});
}
}
export default {
name: 'register-service-worker',
initialize() {
initialize(container) {
const siteSettings = container.lookup('site-settings:main');
const isSecure = (document.location.protocol === 'https:') ||
(location.hostname === "localhost");
if (isSecure && ('serviceWorker' in navigator)) {
let agents = siteSettings.service_worker_user_agents.split('|');
let allowed = false;
if (agents[0].length > 0) {
const userAgent = (navigator.userAgent || "unknown").toLowerCase();
for(let i=0; i<agents.length; i++) {
allowed = userAgent.indexOf(agents[i]) !== -1;
if (allowed) { break; }
}
}
if (allowed) {
// TODO no need to register if we already registered recently
// consider using local storage to figure out when last registered
navigator.serviceWorker.register(`${Discourse.BaseUri}/service-worker.js`);
} else {
deRegister();
}
}
}
};

View File

@ -1268,6 +1268,12 @@ uncategorized:
summary_percent_filter: 20
summary_max_results: 100
service_worker_user_agents:
hidden: true
default: 'android'
type: list
client: true
# View heat thresholds
topic_views_heat_low:
client: true