onUpdateAvailable and onInstall events. How do they work?

Viewed 32

How do the runtime.onUpdateAvailable and runtime.onInstall events follow each other?
(1°Q) First, can they coexist in the same background script?
The documentation states that onUpdateAvailable can be useful for postpone an update for example, when you are in the middle of some important operation that we don't want it to be interrupted.
If the extension has registered an event handler for onUpdateAvailable, then the update will be applied the next time the extension is reloaded.

This happens when:

  • we restart the browser
  • the extension is disabled and then re-enabled
  • the extension reloads itself by calling the runtime.reload method

So I imagine that the onUpdateAvailable event handler that will be executed is the one referring to the old version.
(2nd Q) Am I right?

Now I ask myself:

(3nd Q) When the extension is reloaded later in one of the above ways, will the onInstall event be honored or ignored?
(4th Q) Is there a way to test this behavior by myself, specifically I am referring to the onUpdateAvailable event?

1 Answers

Regardless of onUpdateAvailable's presence, the extension will always be updated when the background script is inactive (i.e. it doesn't run), in particular when it's reloaded or re-enabled.

An onUpdateAvailable listener was necessary for a persistent background script in ManifestV2 because without this listener Chrome would automatically terminate the extension and update it. Chrome did it to prevent an outdated extension continuously running for days/months.

In ManifestV3 the background script can't be persistent by design, so Chrome won't restart an extension even if it doesn't have this event listener. However, you may want to use this event anyway if you connect to a native app via chrome.runtime.connectNative in Chrome 105 or newer because it makes the background run forever (i.e. it becomes persistent) or if you prolong its lifetime artificially.

The onUpdateAvailable and onInstalled listeners can co-exist and are triggered independently.

  • onUpdateAvailable (of the current version) runs before updating,
  • onInstalled (of the new version) runs afterwards.

To test the event in case a newer version is present in the web store, press the Update button in chrome://extensions page or call chrome.runtime.requestUpdateCheck() in your extension.

To test the event independently of the web store, configure your extension to use local updates.

Related