References:
1. Lifecycle
The design doc and blog say:
The page will have a lifetime mechanism similar to event pages in Manifest V2, in that it will be torn down when it stops performing actions. Additionally, the user agent may place further restrictions on the lifetime specific to the purpose specified.
But I tried the offscreen api yesterday. In my test on Chrome 109 stable, the offscreen document has never been terminated (over 12 hours, it still exists). My code is
await chrome.offscreen.createDocument({
url: 'off_screen.html',
reasons: ['DOM_SCRAPING'],
justification: 'reason for needing the document',
});
in offscreen document, it only has a message listener
chrome.runtime.onMessage.addListener(...);
What is its life cycle? persistent until calling closeDocument() by developers?
2. Single or multiple
The blog says:
For implementation ease, the first version of this API only supports a single page per-extension, per-profile at a time. In future versions, we may relax this to support multiple pages.
I wrote a response to how to detect if the offscreen doc exists when createDocument() in
another thread. At present, createDocument() will throw an error if it already exists. When it supports multiple documents, it will not throw errors?
Thus, 1) developers should not use "try catch" to create documents. 2) the signature of chrome.offscreen.hasDocument() method should accept a url as parameter and return an array of boolean.
3. Forward and backward compatibility
The blog says:
Additionally, as DOM functionality and APIs are added to the service worker, the list of reasons to create a document will be added to or reduced depending on the current state of the service worker, and reasons to use offscreen documents.
Offscreen Documents allow extensions that require DOM or window interaction access that cannot be achieved in service workers currently. It also provides a flexible approach, where new use cases can be added and future-solved use cases can be removed.
What I understand is that if some feature is added to service worker in the future then the offscreen api might remove some "Reason". My question is how do developers write code that is both forward and backward compatible? Developers may die due to illness or accidents, so the code needs to be as forward compatible as possible.