files/en-us/web/api/workletsharedstorage/index.md
{{APIRef("Shared Storage API")}}{{deprecated_header}}
The WorkletSharedStorage interface of the {{domxref("Shared Storage API", "Shared Storage API", "", "nocode")}} represents the shared storage for a particular origin within a worklet context.
WorkletSharedStorage is accessed via {{domxref("SharedStorageWorkletGlobalScope.sharedStorage")}}.
{{InheritanceDiagram}}
WorkletSharedStorage inherits properties from its parent interface, {{domxref("SharedStorage")}}.
WorkletSharedStorage also includes the following methods because it has an async iterator defined on it:
WorkletSharedStorage object instance's enumerable properties.WorkletSharedStorage object instance.WorkletSharedStorage[Symbol.asyncIterator]() {{Deprecated_Inline}}
setSharedStorageContext()You can use the Private 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 the Protected Audience API.
In this example, we store data from both the embedding page and the fenced frame using shared storage.
On the embedding page, we set a mock event ID as the shared storage context using setSharedStorageContext():
const frameConfig = await navigator.runAdAuction({ resolveToConfig: true });
// Data from the embedder that you want to pass to the shared storage worklet
frameConfig.setSharedStorageContext("some-event-id");
const frame = document.createElement("fencedframe");
frame.config = frameConfig;
Within the fenced frame, after adding the worklet module with {{domxref("Worklet.addModule","window.sharedStorage.worklet.addModule()")}}, we send the event-level data into the shared storage worklet module using {{domxref("WindowSharedStorage.run","window.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 the reporting-worklet.js worklet, we read the embedding document's event ID from sharedStorage.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);
{{Specifications}}
{{Compat}}