Title: Service Workers
Published: July 15, 2026
Last modified: July 16, 2026

---

# Service Workers

[ Back to top](https://developer.wordpress.org/playground/developers/architecture/browser-service-workers/?output_format=md#wp--skip-link--target)

[A Service Worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers)
is used to handle the HTTP traffic using the in-browser [`PHPRequestHandler`](https://developer.wordpress.org/playground/developers/architecture/browser-concepts).

Imagine your PHP script renders the following page [in the iframe viewport](https://developer.wordpress.org/playground/developers/architecture/browser-iframe-rendering):

    ```html
    <html>
        <head>
            <title>John's Website</title>
        </head>
        <body>
            <a href="/">Homepage</a>
            <a href="/blog">Blog</a>
            <a href="/contact">Contact</a>
        </body>
    </html>
    ```

When the user clicks, say the `Blog` link, the browser would normally send a HTTP
request to the remote server to fetch the `/blog` page and then display it instead
of the current iframe contents. However, our app isn’t running on the remote server.
The browser would just display a 404 page.

Enter [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers)–
a tool to intercept the HTTP requests and handle them inside the browser:

![Service worker data flow](https://i0.wp.com/raw.githubusercontent.com/WordPress/
wordpress-playground/refs/heads/trunk/packages/docs/site/static/img/workers-diagram.
webp?ssl=1)

### Service Worker setup

The main application living in `/index.html` is responsible for registering the 
service worker.

Here’s the minimal setup:

**/app.js:**

    ```javascript
    function main() {
        await registerServiceWorker(
            phpClient,
            "default", // PHP instance scope
            "/sw.js",  // Must point to a valid Service Worker implementation.
            "1"        // Service worker version, used for reloading the script.
        );

    }
    ```

You will also need a separate `/service-worker.js` file that actually intercepts
and routes the HTTP requests. Here’s what a minimal implementation looks like:

**/service-worker.js**:

    ```javascript
    // Intercepts all HTTP traffic on the current domain and
    // passes it to the Worker Thread.
    initializeServiceWorker();
    ```

First published

July 15, 2026

Last updated

July 16, 2026

Edit article

[ Improve it on GitHub: Service Workers ](https://raw.githubusercontent.com/WordPress/wordpress-playground/trunk/packages/docs/site/docs/developers/23-architecture/12-browser-service-workers.md)

Changelog

[ See list of changes: Service Workers ](https://developer.wordpress.org/playground/developers/architecture/browser-service-workers/?output_format=md#)

[  Previous: PHP Worker Threads](https://developer.wordpress.org/playground/developers/architecture/browser-php-worker-threads/)

[  Next: Scopes](https://developer.wordpress.org/playground/developers/architecture/browser-scopes/)