mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
b50b0d57fe
Former-commit-id: 3bb6cc662da9e9255bd61fef42430c271002fd49 [formerly eaf1785c4f85522e4eb66d00a6ae9dd9ecc4fcb4] [formerly addd3ffe1396e6df84cdc3e8787d57ffb2be3dc6 [formerly 800693ad49
]]
Former-commit-id: 6c24d30f26529457202f470620a0ea1d31772b13 [formerly 384d2af17fe100b9db91462eb41337f9dff855f4]
Former-commit-id: 94f4933e12f97ee7468c884f041612498e07ba32
56 lines
2.3 KiB
JavaScript
56 lines
2.3 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
// Check to make sure service workers are supported in the current browser,
|
|
// and that the current page is accessed from a secure origin. Using a
|
|
// service worker from an insecure origin will trigger JS console errors.
|
|
const isLocalhost = Boolean(window.location.hostname === 'localhost' ||
|
|
// [::1] is the IPv6 localhost address.
|
|
window.location.hostname === '[::1]' ||
|
|
// 127.0.0.1/8 is considered localhost for IPv4.
|
|
window.location.hostname.match(
|
|
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
|
)
|
|
);
|
|
|
|
window.addEventListener('load', function() {
|
|
if ('serviceWorker' in navigator &&
|
|
(window.location.protocol === 'https:' || isLocalhost)) {
|
|
navigator.serviceWorker.register('{{ .BaseURL }}/sw.js')
|
|
.then(function(registration) {
|
|
// updatefound is fired if service-worker.js changes.
|
|
registration.onupdatefound = function() {
|
|
// updatefound is also fired the very first time the SW is installed,
|
|
// and there's no need to prompt for a reload at that point.
|
|
// So check here to see if the page is already controlled,
|
|
// i.e. whether there's an existing service worker.
|
|
if (navigator.serviceWorker.controller) {
|
|
// The updatefound event implies that registration.installing is set
|
|
const installingWorker = registration.installing;
|
|
|
|
installingWorker.onstatechange = function() {
|
|
switch (installingWorker.state) {
|
|
case 'installed':
|
|
// At this point, the old content will have been purged and the
|
|
// fresh content will have been added to the cache.
|
|
// It's the perfect time to display a "New content is
|
|
// available; please refresh." message in the page's interface.
|
|
break;
|
|
|
|
case 'redundant':
|
|
throw new Error('The installing ' +
|
|
'service worker became redundant.');
|
|
|
|
default:
|
|
// Ignore
|
|
}
|
|
};
|
|
}
|
|
};
|
|
}).catch(function(e) {
|
|
console.error('Error during service worker registration:', e);
|
|
});
|
|
}
|
|
});
|
|
})();
|