From f91cd415f27e55d350b1d89248ee69aef912aae4 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 3 Dec 2022 16:13:39 -0500 Subject: [PATCH] add error for non-whitelisted http: locations --- public/register-sw.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/public/register-sw.js b/public/register-sw.js index fcf2435..a0f6df1 100644 --- a/public/register-sw.js +++ b/public/register-sw.js @@ -4,11 +4,22 @@ */ const stockSW = "/uv/sw.js"; +/** + * List of hostnames that are allowed to run serviceworkers on http: + */ +const swAllowedHostnames = ["localhost", "127.0.0.1"]; + /** * Global util * Used in 404.html and index.html */ async function registerSW() { + if ( + location.protocol !== "https:" && + !swAllowedHostnames.includes(location.hostname) + ) + throw new Error("Service workers cannot be registered without https."); + if (!navigator.serviceWorker) throw new Error("Your browser doesn't support service workers.");