demo · v134
Shared Storage + Web Locks
Shared Storage worklets can now request a Web Lock for serial access to a key. Pre-134, two concurrent worklet invocations of sharedStorage.run() could race on a read-modify-write. The probe below races N concurrent updates with and without a real navigator.locks.request() call to show the difference.
Heads up
The actual integration is between Shared Storage worklets and Web Locks (Privacy Sandbox enrollment required). The page below uses navigator.locks directly to demonstrate the locking semantics — same end-to-end race, fewer enrollment hurdles.
probing navigator.locks + sharedStorage...
Try it
without lock (race)
0
expected: 0
with lock
0
expected: 0
The code
// Inside a Shared Storage worklet (v134+):
class IncWithLock {
async run(args) {
await sharedStorage.run("inc", { lock: "counter" });
}
}
registerOperation("inc", IncWithLock);
// (Or, equivalently, use navigator.locks directly to serialise:)
await navigator.locks.request("counter", async () => {
const v = +(await sharedStorage.get("count") || "0");
await sharedStorage.set("count", String(v + 1));
});