Service Worker: Auto-recover after hard reload (#1707)

This commit is contained in:
Alexander Zinchuk 2022-02-11 15:13:15 +01:00
parent 94cdf97b0b
commit b366d22575
2 changed files with 13 additions and 1 deletions

View File

@ -43,7 +43,8 @@ const READABLE_ERROR_MESSAGES: Record<string, string> = {
MESSAGE_TOO_LONG: 'Message is too long',
// Non-API errors
SERVICE_WORKER_DISABLED: 'Service Worker is disabled. Please reload the page without holding <Shift> key.',
// eslint-disable-next-line max-len
SERVICE_WORKER_DISABLED: 'Service Worker is disabled. Streaming media may not be supported. Try reloading the page without holding <Shift> key',
// eslint-disable-next-line max-len
MESSAGE_TOO_LONG_PLEASE_REMOVE_CHARACTERS: 'The provided message is too long. Please remove {EXTRA_CHARS_COUNT} character{PLURAL_S}.',
// eslint-disable-next-line max-len

View File

@ -38,6 +38,17 @@ function subscribeToWorker() {
if (IS_SERVICE_WORKER_SUPPORTED) {
window.addEventListener('load', async () => {
try {
if (!navigator.serviceWorker.controller) {
const registrations = await navigator.serviceWorker.getRegistrations();
if (registrations.length) {
if (DEBUG) {
// eslint-disable-next-line no-console
console.log('[SW] Hard reload detected, re-enabling Service Worker');
}
await Promise.all(registrations.map(r => r.unregister()));
}
}
await navigator.serviceWorker.register(new URL('../serviceWorker.ts', import.meta.url));
if (DEBUG) {