Attempt to register sw.js on pageload

environments/review-vite-sw2-fx5w6x/deployments/3834
Alex Gleason 2023-09-15 14:07:59 -05:00
rodzic d34eaa8be2
commit 1c7b95f12a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
4 zmienionych plików z 18 dodań i 7 usunięć

Wyświetl plik

@ -8,7 +8,7 @@ import { HStack, Text, Stack } from 'soapbox/components/ui';
import { captureException } from 'soapbox/monitoring'; import { captureException } from 'soapbox/monitoring';
import KVStore from 'soapbox/storage/kv-store'; import KVStore from 'soapbox/storage/kv-store';
import sourceCode from 'soapbox/utils/code'; import sourceCode from 'soapbox/utils/code';
import { unregisterSw } from 'soapbox/utils/sw'; import { unregisterSW } from 'soapbox/utils/sw';
import SiteLogo from './site-logo'; import SiteLogo from './site-logo';
@ -96,7 +96,7 @@ class ErrorBoundary extends React.PureComponent<Props, State> {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
e.preventDefault(); e.preventDefault();
unregisterSw().then(goHome).catch(goHome); unregisterSW().then(goHome).catch(goHome);
} }
}; };

Wyświetl plik

@ -3,7 +3,7 @@ import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import List, { ListItem } from 'soapbox/components/list'; import List, { ListItem } from 'soapbox/components/list';
import { HStack, Text, Column, FormActions, Button, Stack, Icon } from 'soapbox/components/ui'; 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'; import Indicator from './components/indicator';
@ -99,7 +99,7 @@ const ServiceWorkerInfo: React.FC<IServiceWorkerInfo> = () => {
}; };
const handleRestart = async() => { const handleRestart = async() => {
await unregisterSw(); await unregisterSW();
window.location.reload(); window.location.reload();
}; };

Wyświetl plik

@ -25,13 +25,14 @@ import './precheck';
import { default as Soapbox } from './containers/soapbox'; import { default as Soapbox } from './containers/soapbox';
import * as monitoring from './monitoring'; import * as monitoring from './monitoring';
import ready from './ready'; import ready from './ready';
import { registerSW } from './utils/sw';
// Sentry // Sentry
monitoring.start(); monitoring.start();
// Print console warning
if (BuildConfig.NODE_ENV === 'production') { if (BuildConfig.NODE_ENV === 'production') {
printConsoleWarning(); printConsoleWarning();
registerSW('/sw.js');
} }
ready(() => { ready(() => {

Wyświetl plik

@ -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 */ /** Unregister the ServiceWorker */
// https://stackoverflow.com/a/49771828/8811886 // https://stackoverflow.com/a/49771828/8811886
const unregisterSw = async(): Promise<void> => { const unregisterSW = async(): Promise<void> => {
if (navigator.serviceWorker) { if (navigator.serviceWorker) {
// FIXME: this only works if using a single tab. // FIXME: this only works if using a single tab.
// Send a message to sw.js instead to refresh all tabs. // Send a message to sw.js instead to refresh all tabs.
@ -11,5 +20,6 @@ const unregisterSw = async(): Promise<void> => {
}; };
export { export {
unregisterSw, registerSW,
unregisterSW,
}; };