Implementation:Microsoft Playwright CrServiceWorker
| Knowledge Sources | |
|---|---|
| Domains | Chromium, ServiceWorker |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing Chromium service worker targets with JavaScript execution, network interception, and console message support provided by the Playwright library.
Description
The `CRServiceWorker` class extends `Worker` and represents a Chromium service worker target. It creates a `CRExecutionContext` when the Runtime execution context is established, optionally sets up a `CRNetworkManager` for network inspection (unless disabled via `PLAYWRIGHT_DISABLE_SERVICE_WORKER_NETWORK`), and forwards console API calls as `ConsoleMessage` events on the browser context. The class handles service worker script loading detection (with version-gated behavior for Chromium 143+), network interception setup (request interception, extra HTTP headers, credentials, offline mode), and exception reporting.
Usage
Use CrServiceWorker when Playwright needs to interact with service workers in Chromium, including evaluating JavaScript, intercepting network requests, and capturing console output.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/chromium/crServiceWorker.ts
Signature
export class CRServiceWorker extends Worker {
readonly browserContext: CRBrowserContext;
constructor(browserContext: CRBrowserContext, session: CRSession, url: string);
}
Import
import { CRServiceWorker } from '../server/chromium/crServiceWorker';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| browserContext | CRBrowserContext | Yes | The browser context owning this service worker |
| session | CRSession | Yes | CDP session for the service worker target |
| url | string | Yes | The URL of the service worker script |
Outputs
| Name | Type | Description |
|---|---|---|
| (events) | ConsoleMessage | Console messages emitted on the browser context |
| executionContext | CRExecutionContext | JavaScript execution context for evaluation |
Usage Examples
import { CRServiceWorker } from '../server/chromium/crServiceWorker';
const worker = new CRServiceWorker(browserContext, crSession, 'https://example.com/sw.js');
// Worker automatically sets up execution context and network interception
// Console messages from the service worker are emitted on browserContext