WorkletSharedStorage
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
TheWorkletSharedStorage interface of theShared Storage API represents the shared storage for a particular origin within a worklet context.
WorkletSharedStorage is accessed viaSharedStorageWorkletGlobalScope.sharedStorage.
In this article
Instance properties
contextDeprecatedNon-standardContains contextual data passed into the shared storage worklet from the associated browsing context via the
FencedFrameConfig.setSharedStorageContext()method.
Instance methods
WorkletSharedStorage inherits properties from its parent interface,SharedStorage.
get()DeprecatedRetrieves a value from the shared storage.
length()DeprecatedReturns the number of entries currently stored in the shared storage for the current origin.
remainingBudget()DeprecatedReturns the remaining navigation budget for the current origin.
WorkletSharedStorage also includes the following methods because it has anasync iterator defined on it:
entries()DeprecatedReturns a new async iterator for the key-value pairs of a
WorkletSharedStorageobject instance's enumerable properties.keys()DeprecatedReturns a new async iterator containing the keys for each item in a
WorkletSharedStorageobject instance.WorkletSharedStorage[Symbol.asyncIterator]()DeprecatedReturns the
entries()function by default.
Examples
>Passing contextual data viasetSharedStorageContext()
You can use thePrivate Aggregation API to create reports that combine event-level data inside fenced frames with contextual data from the embedding document.setSharedStorageContext() can be used to pass contextual data from the embedder to shared storage worklets initiated by theProtected Audience API.
In this example, we store data from both the embedding page and the fenced frame usingshared storage.
On the embedding page, we set a mock event ID as the shared storage context usingsetSharedStorageContext():
const frameConfig = await navigator.runAdAuction({ resolveToConfig: true });// Data from the embedder that you want to pass to the shared storage workletframeConfig.setSharedStorageContext("some-event-id");const frame = document.createElement("fencedframe");frame.config = frameConfig;Within the fenced frame, after adding the worklet module withwindow.sharedStorage.worklet.addModule(), we send the event-level data into the shared storage worklet module usingwindow.sharedStorage.run() (this is unrelated to the contextual data from the embedding document):
const frameData = { // Data available only inside the fenced frame};await window.sharedStorage.worklet.addModule("reporting-worklet.js");await window.sharedStorage.run("send-report", { data: { frameData, },});In thereporting-worklet.js worklet, we read the embedding document's event ID fromsharedStorage.context and the frame's event-level data from the data object. We then report them through Private Aggregation:
class ReportingOperation { convertEventIdToBucket(eventId) { // … } convertEventPayloadToValue(info) { // … } async run(data) { // Data from the embedder const eventId = sharedStorage.context; // Data from the fenced frame const eventPayload = data.frameData; privateAggregation.sendHistogramReport({ bucket: convertEventIdToBucket(eventId), value: convertEventPayloadToValue(eventPayload), }); }}register("send-report", ReportingOperation);