Need more advice on non-persistence

The background scripts article has some advice on coding for non-persistence, but not quite enough. I have a few questions:

  1. I’m not sure what the right event is for initializing code whenever the extension starts. At the moment I just use a bare init() function in the background script. Is that fine?

  2. My extension currently loads settings from storage at startup, which is then imported by various modules for their functions to refer to. Is the non-persistent way really having functions directly load relevant settings every time they are needed?

  3. Is dynamically importing modules for optional functionality worth doing for performance?

  1. you should use browser.runtime.onInstalled event to initially setup things like context menus and Alarms and they should persist even after browser restarts. That is, in theory and probably only in MV3. It’s not exactly well documented…

  2. yes, the background script will be completely reloaded, like if you just reopened closed page.
    It’s important to note that browsers are extremely fast these days so reading data from storage and executing some hundreds lines of javascript can be done in few milliseconds.
    So the benefit of not consuming any RAM while being “idle” is a big plus and re-running background script several times per day is actually a good tradeoff.

  3. it depends, but unless you are importing huge libraries, it’s probably not worth your time. Also it can be challenging in MV3…

And lastly, Firefox doesn’t support yet non-persistent background scripts in the release channel version, so unless you want to help with testing, you can just wait about 1 year when next ESR 114 is released.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background#browser_compatibility
Only persistent pages are supported. As of version 101, extension developers can turn on event pages for testing in Manifest V2 using the extensions.eventPages.enabled preference and in Manifest V3 using the extensions.manifestV3.enabled preference.

1 Like

Let me get this straight, browser.runtime.onInstalled is every time the extension starts e.g. when Firefox starts, not when it’s “installed” e.g. from AMO?

To be more specific, at initialization my extension needs to take in the current state of the browser’s windows and tabs.

Edit:

Would browser.runtime.onStartup be the correct event in this case?

That was my reaction too!

But no, these things “persists” like a storage, across browser / PC restarts!
So in your on-install event you should set your periodic alarm and it will stay registered forever. And it’s same with context menus and some other API.

The onStartup may be problematic since it’s not really related to extension. I think if you disable and enable your extension, it won’t fire. Also if the browser keeps some instance running (maybe like an Edge), it may not fire if you close and reopen browser (I’m not sure where I read this).

So as you can see, things are pretty complicated and currently there is no “on-extension-start” event. But there are some workarounds with storage.session.

See also:
Best-practices for “event pages”
http://web.archive.org/web/20140302025634/https://developer.chrome.com/extensions/event_pages#best-practices

MV3 service workers: Running a function once at the start
https://groups.google.com/a/chromium.org/g/chromium-extensions/c/QjuCv-LRK2c/m/qYx70RYhAwAJ

1 Like

storage.session is not supported by Firefox though. What can we do?

Well, not yet, but it’s planned and with priority P2 it sounds promising to come out soon: