From 1c7b95f12ace6ab0743e03825096be0e80478542 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 15 Sep 2023 14:07:59 -0500 Subject: [PATCH] Attempt to register sw.js on pageload --- app/soapbox/components/error-boundary.tsx | 4 ++-- .../features/developers/service-worker-info.tsx | 4 ++-- app/soapbox/main.tsx | 3 ++- app/soapbox/utils/sw.ts | 14 ++++++++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/soapbox/components/error-boundary.tsx b/app/soapbox/components/error-boundary.tsx index 795596eb7..3934a2adc 100644 --- a/app/soapbox/components/error-boundary.tsx +++ b/app/soapbox/components/error-boundary.tsx @@ -8,7 +8,7 @@ import { HStack, Text, Stack } from 'soapbox/components/ui'; import { captureException } from 'soapbox/monitoring'; import KVStore from 'soapbox/storage/kv-store'; import sourceCode from 'soapbox/utils/code'; -import { unregisterSw } from 'soapbox/utils/sw'; +import { unregisterSW } from 'soapbox/utils/sw'; import SiteLogo from './site-logo'; @@ -96,7 +96,7 @@ class ErrorBoundary extends React.PureComponent { if ('serviceWorker' in navigator) { e.preventDefault(); - unregisterSw().then(goHome).catch(goHome); + unregisterSW().then(goHome).catch(goHome); } }; diff --git a/app/soapbox/features/developers/service-worker-info.tsx b/app/soapbox/features/developers/service-worker-info.tsx index 4f33b12ab..d5c8e22da 100644 --- a/app/soapbox/features/developers/service-worker-info.tsx +++ b/app/soapbox/features/developers/service-worker-info.tsx @@ -3,7 +3,7 @@ import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import List, { ListItem } from 'soapbox/components/list'; import { HStack, Text, Column, FormActions, Button, Stack, Icon } from 'soapbox/components/ui'; -import { unregisterSw } from 'soapbox/utils/sw'; +import { unregisterSW } from 'soapbox/utils/sw'; import Indicator from './components/indicator'; @@ -99,7 +99,7 @@ const ServiceWorkerInfo: React.FC = () => { }; const handleRestart = async() => { - await unregisterSw(); + await unregisterSW(); window.location.reload(); }; diff --git a/app/soapbox/main.tsx b/app/soapbox/main.tsx index 96f82e54c..7045b0637 100644 --- a/app/soapbox/main.tsx +++ b/app/soapbox/main.tsx @@ -25,13 +25,14 @@ import './precheck'; import { default as Soapbox } from './containers/soapbox'; import * as monitoring from './monitoring'; import ready from './ready'; +import { registerSW } from './utils/sw'; // Sentry monitoring.start(); -// Print console warning if (BuildConfig.NODE_ENV === 'production') { printConsoleWarning(); + registerSW('/sw.js'); } ready(() => { diff --git a/app/soapbox/utils/sw.ts b/app/soapbox/utils/sw.ts index 910d7189a..931c5b60c 100644 --- a/app/soapbox/utils/sw.ts +++ b/app/soapbox/utils/sw.ts @@ -1,6 +1,15 @@ +/** Register the ServiceWorker. */ +function registerSW(path: string) { + if ('serviceWorker' in navigator) { + window.addEventListener('load', () => { + navigator.serviceWorker.register(path, { scope: '/' }); + }); + } +} + /** Unregister the ServiceWorker */ // https://stackoverflow.com/a/49771828/8811886 -const unregisterSw = async(): Promise => { +const unregisterSW = async(): Promise => { if (navigator.serviceWorker) { // FIXME: this only works if using a single tab. // Send a message to sw.js instead to refresh all tabs. @@ -11,5 +20,6 @@ const unregisterSw = async(): Promise => { }; export { - unregisterSw, + registerSW, + unregisterSW, }; \ No newline at end of file