Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,34 @@ setBaseUrl(client.baseURL)
.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
* https://stackoverflow.com/questions/66618136
* Keep the service worker alive using Offscreen API to prevent Chrome's termination.
*/
if (import.meta.env.VITE_TARGET_BROWSER === 'chrome') {
function setupKeepAlive(): void {
console.debug(
'Setting up keep-alive ping to prevent service worker termination',
)
async function setupOffscreen() {
const _chrome = (globalThis as any).chrome
if (typeof _chrome === 'undefined' || !_chrome.offscreen) return

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)
}
if (await _chrome.offscreen.hasDocument()) return

// Start the keep-alive mechanism
setupKeepAlive()
try {
await _chrome.offscreen.createDocument({
url: 'src/offscreen.html',
reasons: ['BLOBS'],
justification: 'Keep service worker alive for heartbeat packets',
})
} catch (e) {
console.error('Failed to create offscreen document:', e)
}
}

browser.runtime.onMessage.addListener((message: any) => {
if (message.type === 'KEEP_ALIVE') {
return Promise.resolve({ status: 'ok' })
}
return undefined
})

// Initialize on startup and installation
browser.runtime.onStartup.addListener(setupOffscreen)
browser.runtime.onInstalled.addListener(setupOffscreen)

setupOffscreen()
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"alarms",
"notifications",
"activeTab",
"offscreen",
"storage"
],
"{{safari}}.permissions": [
Expand Down
9 changes: 9 additions & 0 deletions src/offscreen.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script type="module" src="./offscreen.ts"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions src/offscreen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import browser from 'webextension-polyfill'

// Send a message to the background every 20 seconds to keep the Service Worker alive
function keepAlive() {
browser.runtime.sendMessage({ type: 'KEEP_ALIVE' }).catch(() => {
// Expected during reload
})
}

setInterval(keepAlive, 20 * 1000)
keepAlive()
6 changes: 5 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export default defineConfig({
plugins: [
webExtension({
manifest: generateManifest,
additionalInputs: ['src/consent/index.html', 'src/consent/main.ts'],
additionalInputs: [
'src/consent/index.html',
'src/consent/main.ts',
'src/offscreen.html',
],
browser: process.env.VITE_TARGET_BROWSER,
}),
],
Expand Down
Loading