huabin
February 14, 2024, 9:30am
1
Hello guys,
I use background service worker in my add-ons, but I met an error when I uploaded it to add-ons market, please check the image.
I need service worker in this add-ons, any suggestions? Thank you.
The service workers in extensions are not yet supported in Firefox, more info:
See also this proposal for defining background script in a common way that would work in all browsers:
opened 06:31AM - 27 Sep 22 UTC
topic: service worker
implemented: chrome
implemented: safari
implemented: firefox
proposal
> Edit by @Rob--W on 2023-02-24: The spirit of this feature request is to enable… cross-browser development with a single manifest file. Chrome [opposed the initial proposal](https://github.com/w3c/webextensions/issues/282#issuecomment-1309088865) that is described below, but after [proposing an alternative solution](https://github.com/w3c/webextensions/issues/282#issuecomment-1442169628) that consists of allowing `service_worker` and `scripts` to co-exist (with the latter being ignored in Chrome), the position changed to [Chrome being supportive](https://github.com/w3c/webextensions/issues/282#issuecomment-1443396585).
Since the group currently does not agree what the future of background scripting should look like Safari and Firefox with [limited event pages](https://github.com/w3c/webextensions/issues/134), Chrome with [serviceWorkers](https://developer.chrome.com/docs/extensions/mv3/service_workers/ )
To make sure developers can define their background scripts in both situations I am proposing the following syntax:
```json
"background": {
"scripts": ["script1.js", "script2.js"]
}
```
**Browsers only supporting serviceWorkers** would use the script directly as serviceWorker if only one script is defined. If multiple script files are listed, it will create a generated serviceWorker named `_generated_service_worker.js` just like browsers currently generate a `_generated_background_page.html`. This serviceWorker will import all scripts using [importScripts()](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts):
```js
importScripts(
'script1.js',
'script2.js'
);
```
**Browsers only supporting limited event pages** would generate a `_generated_background_page.html` which imports the scripts using script tags:
```html
<head></head><body>
<script src="script1.js"></script>
<script src="script2.js"></script>
</body>
```
**Browsers supporting both serviceWorkers and limited event pages** would choose based on their own preference. We can let developers set `preferred_environment` to `serviceWorker` or `page` which browser vendors can ignore as such:
```json
"background": {
"scripts": ["script1.js", "script2.js"],
"preferred_environment": "serviceWorker"
}
```
Why is this important? This will lead to a more optimal contract between the developer and the browser. In which the developer is able to communicate their preferred environment while allowing browsers to decide what environment extensions should be using. This also makes sure browsers can change their preferred environment over time and have their own approach towards background script handling.
This also has the added benefit of allowing browsers to support both mv2 and mv3 like Safari does right now.
### `"type": "module"`
This syntax works perfectly fine with type module as well by using imports in the serviceWorker, or the type module attribute for the limited event page.
In the case of `"type": "module"`, the `_generated_service_worker.js` would look like this:
```js
import "./script1.js";
import "./script2.js";
```
In the case of [limited event pages](https://github.com/w3c/webextensions/issues/134), we can actually also start supporting `"type": "module"` by adding it to the `_generated_background_page.html` as such:
```html
<head></head><body>
<script type="module" src="script1.js"></script>
<script type="module" src="script2.js"></script>
</body>
```
See https://github.com/w3c/webextensions/issues/289
huabin
February 14, 2024, 12:57pm
3
@juraj.masiar
I see, thank you so much.
dotproto
(Simeon Vincent)
February 20, 2024, 11:08pm
4
@huabin , can you share more about why you you need a service worker in your add-on? As @juraj.masiar noted they aren’t yet supported in stable releases of Firefox, but we may be able to offer additional recommendations depending on what you’re trying to do.
2 Likes