shared-worker-fallback
v0.1.0
Published
Run SharedWorker-style code with a dedicated worker fallback.
Readme
shared-worker-fallback
shared-worker-fallback lets browser applications use the Shared Worker programming model when only dedicated workers are available.
Use shared-worker-fallback/client to install the fallback globally, or import SharedWorker from shared-worker-fallback/client/ponyfill to leave the global unchanged. Import shared-worker-fallback/worker before the worker implementation. These examples assume a bundler that resolves npm package subpaths.
When SharedWorker is unavailable, either client entrypoint creates a dedicated Worker and transfers a MessagePort to it. The worker adapter then dispatches a Shared Worker-style connect event. Native Shared Worker implementations are used when available. Environments loading a client entrypoint are expected to support dedicated workers.
Installation
npm install shared-worker-fallbackThe client and worker subpaths are side-effect-only modules and export no values. The client/ponyfill subpath exports SharedWorker without modifying globalThis.
Compatibility model
The fallback provides API compatibility so application and worker code can be written around SharedWorker, SharedWorkerGlobalScope, onconnect, and MessagePort in both environments.
It does not reproduce Shared Worker identity or sharing semantics. Each fallback SharedWorker instance owns a separate dedicated worker, so global state is not shared between instances, tabs, windows, or other browsing contexts. Code using this fallback should communicate through the provided port and must not depend on multiple constructors connecting to the same worker process.
The fallback forwards the shared worker's URL, name, type, and credentials to the dedicated worker. Shared-worker-only lifetime behavior such as extendedLifetime cannot be emulated.
Application: global fallback
Load the client adapter before any application code reads or constructs SharedWorker. The worker is a module because its entrypoint uses a static import:
import "shared-worker-fallback/client";
const worker = new SharedWorker(new URL("./worker.js", import.meta.url), {
name: "application-worker",
type: "module",
});
worker.port.addEventListener("message", ({ data }) => {
console.log(data);
});
worker.port.start();Application: ponyfill
Import the constructor directly when the application should not modify globalThis.SharedWorker:
import { SharedWorker } from "shared-worker-fallback/client/ponyfill";
const worker = new SharedWorker(new URL("./worker.js", import.meta.url), {
name: "application-worker",
type: "module",
});Worker
Load the worker adapter first in the worker entrypoint:
import "shared-worker-fallback/worker";
self.addEventListener("connect", ({ ports: [port] }) => {
port.addEventListener("message", ({ data }) => {
port.postMessage(data);
});
port.start();
});The same worker implementation can run in a native SharedWorkerGlobalScope or in the adapted dedicated-worker scope.
For a classic worker, bundle the worker adapter and implementation into one classic script instead of using the static import shown above.
