Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ Support for the `scripts`, `page`, and `service_worker` properties varies betwee
- when both are specified, Safari uses `background.scripts` (or `background.page`) unless `preferred_environment` is set to `service_worker`.
- when `preferred_environment` is set to `service_worker` and `background.service_worker` isn't specified, Safari generates a service worker from `background.scripts` if present.

To illustrate, this is an example of a cross-browser extension that supports `scripts` and `service_worker`. The example has this manifest.json file:
### Cross-browser Manifest V3 background scripts

To support browsers with different Manifest V3 background script implementations, specify both `scripts` and `service_worker` in the `background` key. Browsers that support extension background service workers use `service_worker`, while browsers that use event pages for this case use `scripts`.

You do not need to include `preferred_environment` for this fallback behavior. Use `preferred_environment` only when you want Safari, or another browser that supports more than one background environment, to prefer `service_worker` where available.

This example shows the relevant parts of a manifest that includes both `scripts` and `service_worker`:

```json
{
Expand All @@ -206,20 +212,16 @@ To illustrate, this is an example of a cross-browser extension that supports `sc
And, background.js contains:
Comment thread
danyalahmed1995 marked this conversation as resolved.

```js
if (typeof browser === "undefined") {
// Chrome does not support the browser namespace yet.
Comment thread
danyalahmed1995 marked this conversation as resolved.
globalThis.browser = chrome;
}
browser.runtime.onInstalled.addListener(() => {
browser.tabs.create({ url: "http://example.com/first-run.html" });
});
```

When the extension is executed, this happens:
With this `background` configuration, this happens:

- in Chrome, the `service_worker` property is used, and a service worker starts that opens the tab because, in a Manifest V3 extension, Chrome only supports service workers for background scripts.
- in Firefox, the `scripts` property is used, and a script starts that opens the tab because Firefox only supports scripts for background scripts.
- in Safari, the `service_worker` property is used, and a service worker starts that opens the tab because Safari gives priority to using service workers for background scripts.
- in Chrome, the `service_worker` property is used, and a service worker starts because, in a Manifest V3 extension, Chrome only supports service workers for background scripts.
- in Firefox, the `scripts` property is used, and an event page starts because Firefox only supports scripts for background scripts.
- in Safari, the `scripts` property is used by default.
Comment thread
danyalahmed1995 marked this conversation as resolved.
Outdated

## Examples

Expand Down
Loading