Scopes

Scopes keep your app working when you open it in two different browser tabs.

The Service Worker passes the intercepted HTTP requests to the PHPRequestHandler for rendering. Technically, it sends a message through a BroadcastChannel which then gets delivered to every browser tab where the application is open. This is undesirable, slow, and leads to unexpected behaviors.

Unfortunately, the Service Worker cannot directly communicate with the relevant Worker Thread – see PR #31 and issue #9 for more details.

Scopes enable each browser tab to:

Technically, a scope is a string included in the PHPRequestHandler.absoluteUrl. For example:

The service worker is aware of this concept and will attach the /scope: found in the request URL to the related BroadcastChannel communication.

A worker thread initiated with a scoped absoluteUrl is said to be scoped:

import {
    PHP,
    setURLScope,
    exposeAPI,
    parseWorkerStartupOptions,
} from '@php-wasm/web';

// Don't use the absoluteURL directly:
const absoluteURL = 'http://127.0.0.1'

// Instead, set the scope first:
const scope = Math.random().toFixed(16)
const scopedURL = setURLScope(absoluteURL, scope).toString()

const { phpVersion } = parseWorkerStartupOptions<{ phpVersion?: string }>();
const php = await PHP.load('8.0', {
    requestHandler: {
        documentRoot: '/',
        absoluteUrl: scopedSiteUrl
    }
});

// Expose the API to app.ts:
const [setApiReady, ] = exposeAPI( php );
setApiReady();

版本号 #2
由 壳壳虫 创建于 2026-08-29 15:36:35 UTC
由 壳壳虫 更新于 2026-08-29 15:36:35 UTC