From 21f577d471e3e90785b45ae5b02521214493e48e Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 11 Feb 2021 16:51:35 -0700 Subject: [PATCH] refactor: add handleRegisterServiceWorker fn --- src/browser/register.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/browser/register.ts b/src/browser/register.ts index c773f776f..765580374 100644 --- a/src/browser/register.ts +++ b/src/browser/register.ts @@ -16,8 +16,23 @@ export async function registerServiceWorker(navigator: Navigator, path: string, } } -if (typeof navigator !== "undefined" && "serviceWorker" in navigator) { - const options = getOptions() - const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`) - registerServiceWorker(navigator, path, options) +interface HandleServiceWorkerRegistration { + getOptions: () => Options + normalize: (url: string, keepTrailing?: boolean) => string + registerServiceWorker: (navigator: Navigator, path: string, options: Options) => Promise } + +export function handleServiceWorkerRegistration({ + getOptions, + normalize, + registerServiceWorker, +}: HandleServiceWorkerRegistration): void { + if (typeof navigator !== "undefined" && "serviceWorker" in navigator) { + const options = getOptions() + const path = normalize(`${options.csStaticBase}/dist/serviceWorker.js`) + registerServiceWorker(navigator, path, options) + } +} + +// Written this way so that it's easier to test +handleServiceWorkerRegistration({ getOptions, normalize, registerServiceWorker })