webext-bridge icon indicating copy to clipboard operation
webext-bridge copied to clipboard

Question: Extension context invalidated

Open Bighamster opened this issue 2 years ago • 2 comments

Within content-script I keep alive service-worker setInterval(() => sendMessage("WORKER_KEEP_ALIVE_MESSAGE", ''),5000);

When the extension is updated, the content-script loses connection to the background and generates an error "Extension context invalidated".

How can I fix it?

Bighamster avatar Oct 12 '22 13:10 Bighamster

What version of webext-bridge are you using?

zikaari avatar Oct 20 '22 19:10 zikaari

Plus I don't think any of the "keepalive" techniques advocated across the internet actually work reliably.

Here is how I confirmed if keepalives were working (they were not after all):

// background.js

// if your keepalive is really working, then this value should always be the same indefinitely 
const startTime = Date.now()

// this rig is just to have the value printable in the console by some content script
chrome.runtime.onMessage.addListener(() => chrome.runtime.sendMessage(startTime))

// ... whatever keepalive method you are using
// content-script

// should indefinitely print the same value, even hours later 
chrome.runtime.onMessage.addListener((v) => console.log(v))

setInterval(() => chrome.runtime.sendMessage(''), 3000)

// ... whatever keepalive method you are using

You can read about my experience with this issue here.

zikaari avatar Oct 20 '22 20:10 zikaari