From 0ca3b392e532a92f3ddcc57e34981fd5ca20b768 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Fri, 19 Jan 2024 14:58:27 -0800 Subject: [PATCH] Fix service worker scope --- register-service-worker.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/register-service-worker.js b/register-service-worker.js index 738703a..0674adb 100644 --- a/register-service-worker.js +++ b/register-service-worker.js @@ -1,10 +1,9 @@ (async () => { if ('serviceWorker' in navigator) { - // Register a service worker hosted at the root of the - // site using the default scope. try { - const registration = - await navigator.serviceWorker.register('service-worker.js'); + const thisFolder = location.pathname.replace(/\/index.html$/, '/'); + const registration = await navigator.serviceWorker.register( + 'service-worker.js', {scope: thisFolder}); console.log('Service worker registration succeeded:', registration); } catch (error) { console.error(`Service worker registration failed: ${error}`);