Skip to content

Commit 23f34ba

Browse files
BelKedErikBjare
andauthored
fix: prevent termination of Chrome service worker (#175)
* fix: implement keep-alive mechanism for Chrome service worker to prevent termination * Apply suggestions from code review --------- Co-authored-by: Erik Bjäreholt <[email protected]>
1 parent febd76a commit 23f34ba

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/background/main.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,31 @@ setBaseUrl(client.baseURL)
7575
.then(waitForEnabled)
7676
.then(() => sendInitialHeartbeat(client))
7777
.then(() => console.info('Started successfully'))
78+
79+
/**
80+
* Keep the service worker alive to prevent Chrome's 5-minute inactivity termination
81+
* This is a workaround for Chrome's behavior of terminating inactive service workers
82+
* https://stackoverflow.com/questions/66618136
83+
*/
84+
if (import.meta.env.VITE_TARGET_BROWSER === 'chrome') {
85+
function setupKeepAlive(): void {
86+
console.debug(
87+
'Setting up keep-alive ping to prevent service worker termination',
88+
)
89+
90+
setInterval(
91+
() => {
92+
console.debug('Keep-alive ping')
93+
// Force some minimal activity
94+
browser.alarms
95+
.get(config.heartbeat.alarmName)
96+
.then(() => console.debug('Keep-alive ping completed'))
97+
.catch((err) => console.error('Keep-alive ping failed:', err))
98+
},
99+
4 * 60 * 1000,
100+
) // 4 minutes (less than Chrome's ~5 minute timeout)
101+
}
102+
103+
// Start the keep-alive mechanism
104+
setupKeepAlive()
105+
}

0 commit comments

Comments
 (0)