google-chrome-extensionbrowser-extension

onUpdateAvailable and onInstall events. How do they work?


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:

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?


Solution

  • 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 can simply wait for it to terminate, i.e. an extension won't be forcefully restarted even if its background script doesn't have this event listener. However, you may want to use this event anyway and explicitly call chrome.runtime.reload() inside if you prolong the lifetime of the background script artificially or connect to a native app via chrome.runtime.connectNative in Chrome 105 or newer which makes the background script "persistent" as long as the port is connected.

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

    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.