runtime.onInstalled event not firing when loading unpacked extension?

172 views
Skip to first unread message

Scott Salisbury

unread,
May 31, 2024, 9:19:04 AM5/31/24
to Chromium Extensions
Hello,

I'm encountering some very weird behavior when trying to set up a chrome.runtime.onInstalled listener in my service worker script and then test it by loading my unpacked extension.

When I load the unpacked extension (or click the "reload" icon for my extension in the extension management page), the console.error() call on line 29 gets executed (because it shows up in the "Errors" list for the extension), but the console.error() call on line 31 does not get executed (based on checking the extension's "Errors" list).  I really can't figure out how that would happen, especially since this  says that the onInstalled event would fire when an unpacked extension was reloaded (albeit with the "update" reason).

Also, it's weird that my "loglevel"-based logger calls (which in this case simply delegate to console.warn() ) don't actually seem to work when the service worker script is being loaded on extension install/reload. Is there documentation/explanation somewhere of why it works that way (I've tried to find anything relevant in the extension dev docs)?


service worker script onInstalled.png

P.S. here's the relevant part of my manifest.json
```json
"background": {
"service_worker": "src/background.js",
"type": "module"
},
```
Does the `"type": "module"` part matter?

Scott Salisbury

unread,
May 31, 2024, 9:36:51 AM5/31/24
to Chromium Extensions, Scott Salisbury
Update- when I comment out a bunch of stuff above the onInstalled listener, the onInstalled listener does run.
service worker script onInstalled runs when earlier stuff commented out.png

Scott Salisbury

unread,
May 31, 2024, 9:51:26 AM5/31/24
to Chromium Extensions, Scott Salisbury
I hoped this would simply be a matter of putting almost everything below the chrome.runtime.onInstalled listener setup (and associated helper methods), but it turns out that I can only get this to fire if literally everything else is commented out.

What in the world is wrong?
I've attached the current version of the full file (it gets converted to js by webpack)
background.ts

Scott Salisbury

unread,
May 31, 2024, 9:59:44 AM5/31/24
to Chromium Extensions, Scott Salisbury
One thing comes to mind- is there a limit on how long the background.js script file can be and still have its runtime.onInstalled listener fire? I have a ton of application logic in my service worker, and webpack jams all of that into the background.js file

woxxom

unread,
May 31, 2024, 10:18:31 AM5/31/24
to Chromium Extensions, Scott Salisbury
onInstalled is always fired when reloading an unpacked extension, so there must be a problem in your code. The most common problem is that the listener is not registered in the first turn of the JS event loop. Looking at your code I see it uses a top-level await, but it's not allowed in a service worker exactly because using it delays execution and prevents registration of wake-up event handlers such as `chrome` events or `fetch` event in the web platform.

Scott Salisbury

unread,
May 31, 2024, 10:31:20 AM5/31/24
to Chromium Extensions, woxxom, Scott Salisbury
Yep, that's it. Thank you so much!
Is the "no top level awaits in service workers" thing mentioned somewhere in the developer docs?

Reply all
Reply to author
Forward
0 new messages