only throw if navigator.serviceWorker is undefined

This commit is contained in:
David Reed 2023-06-20 22:39:40 +03:00
parent f1ff584176
commit efeae66746
No known key found for this signature in database

View file

@ -5,7 +5,7 @@
const stockSW = "/uv/sw.js"; const stockSW = "/uv/sw.js";
/** /**
* List of hostnames that are allowed to run serviceworkers on http: * List of hostnames that are allowed to run serviceworkers on http://
*/ */
const swAllowedHostnames = ["localhost", "127.0.0.1"]; const swAllowedHostnames = ["localhost", "127.0.0.1"];
@ -14,14 +14,15 @@ const swAllowedHostnames = ["localhost", "127.0.0.1"];
* Used in 404.html and index.html * Used in 404.html and index.html
*/ */
async function registerSW() { async function registerSW() {
if (!navigator.serviceWorker) {
if ( if (
location.protocol !== "https:" && location.protocol !== "https:" &&
!swAllowedHostnames.includes(location.hostname) !swAllowedHostnames.includes(location.hostname)
) )
throw new Error("Service workers cannot be registered without https."); throw new Error("Service workers cannot be registered without https.");
if (!navigator.serviceWorker)
throw new Error("Your browser doesn't support service workers."); throw new Error("Your browser doesn't support service workers.");
}
// Ultraviolet has a stock `sw.js` script. // Ultraviolet has a stock `sw.js` script.
await navigator.serviceWorker.register(stockSW, { await navigator.serviceWorker.register(stockSW, {