2023-04-18 19:07:58 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/pull/8012
|
2024-05-30 22:30:50 +08:00
|
|
|
export const registerServiceWorker = (
|
|
|
|
|
scriptURL: string,
|
|
|
|
|
options: RegistrationOptions = {
|
|
|
|
|
scope: "/",
|
|
|
|
|
type: "classic",
|
|
|
|
|
updateViaCache: "all",
|
|
|
|
|
},
|
|
|
|
|
) => {
|
|
|
|
|
|
|
|
|
|
if (!("serviceWorker" in window.navigator)
|
|
|
|
|
|| !("caches" in window)
|
|
|
|
|
|| !("fetch" in window)
|
|
|
|
|
|| navigator.serviceWorker == null
|
|
|
|
|
) {
|
2023-04-19 18:55:50 +08:00
|
|
|
return;
|
2023-04-18 19:07:58 +08:00
|
|
|
}
|
2024-05-30 22:30:50 +08:00
|
|
|
|
2023-04-19 15:43:24 +08:00
|
|
|
// REF https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
|
|
|
|
|
window.navigator.serviceWorker
|
2024-05-30 22:30:50 +08:00
|
|
|
.register(scriptURL, options)
|
|
|
|
|
.then(registration => {
|
|
|
|
|
registration.update();
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
console.debug(`Registration failed with ${e}`);
|
|
|
|
|
});
|
2023-04-18 19:07:58 +08:00
|
|
|
};
|