From 19321a96c8e42f0fd5b7fb336a6dee568961fbbc Mon Sep 17 00:00:00 2001 From: BelKed <66956532+BelKed@users.noreply.github.com> Date: Wed, 12 Mar 2025 14:58:54 +0100 Subject: [PATCH 1/2] fix: implement keep-alive mechanism for Chrome service worker to prevent termination --- src/background/main.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/background/main.ts b/src/background/main.ts index ef9537c..e2a596a 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -75,3 +75,30 @@ setBaseUrl(client.baseURL) .then(waitForEnabled) .then(() => sendInitialHeartbeat(client)) .then(() => console.info('Started successfully')) + +/** + * Keep the service worker alive to prevent Chrome's 5-minute inactivity termination + * This is a workaround for Chrome's behavior of terminating inactive service workers + */ +if (import.meta.env.VITE_TARGET_BROWSER === 'chrome') { + function setupKeepAlive(): void { + console.debug( + 'Setting up keep-alive ping to prevent service worker termination', + ) + + setInterval( + () => { + console.debug('Keep-alive ping') + // Force some minimal activity + browser.alarms + .get(config.heartbeat.alarmName) + .then(() => console.debug('Keep-alive ping completed')) + .catch((err) => console.error('Keep-alive ping failed:', err)) + }, + 4 * 60 * 1000, + ) // 4 minutes (less than Chrome's ~5 minute timeout) + } + + // Start the keep-alive mechanism + setupKeepAlive() +} From 8f38a69cef387d9ab119b36eaae04208246bf648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 12 Mar 2025 15:07:15 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- src/background/main.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/background/main.ts b/src/background/main.ts index e2a596a..a1b092b 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -79,6 +79,7 @@ setBaseUrl(client.baseURL) /** * Keep the service worker alive to prevent Chrome's 5-minute inactivity termination * This is a workaround for Chrome's behavior of terminating inactive service workers + * https://stackoverflow.com/questions/66618136 */ if (import.meta.env.VITE_TARGET_BROWSER === 'chrome') { function setupKeepAlive(): void {