Icon not found
Background scripts are JavaScript files running in the background of an extension, providing persistent functionality such as handling events, storing data, and managing state. Extension.js supports background scripts in both background.scripts and background.service_worker formats, providing full control over long-lived processes within your extension.
Extension.js handles background scripts with the following features:
background.scripts, allowing faster feedback without restarting the extension.background.service_worker, the background script will run as a service worker, aligning with Manifest V3's requirements.The following fields in manifest.json are used to declare background scripts:
| Manifest Field | File Type Expected | HMR Support |
|---|---|---|
background.service_worker |
.js, .ts, .mjs | No |
background.scripts |
.js, .ts, .mjs | Yes |
Below is an example of how to declare a background script within the manifest.json file:
This configuration defines background-service-worker.js as a service worker, which will persist and respond to events as needed by your extension.
Place any additional background scripts that are not directly declared in manifest.json into the /scripts folder. Extension.js processes these files for inclusion in the build.
Example Usage:
Background scripts will be output in the following directory structure:
background.service_worker to ensure compatibility./scripts Folder: Maintain a structured /scripts folder for modularity and code clarity.