SharedStorageWorklet
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
TheSharedStorageWorklet
interface of theShared Storage API represents the shared storage worklet for the current origin.
SharedStorageWorklet
does not have its own properties or methods. Rather, it inherits theaddModule()
method from theWorklet
interface. This method is used for adding a module.
Unlike a regularWorklet
:
- If the calling site has not included the Shared Storage API in aprivacy sandbox enrollment process, calls to
sharedStorageWorklet.addModule()
will be rejected. SharedStorageWorklet
allows only a single module to be added, for privacy reasons. Even with a successful enrollment, repeated calls toaddModule()
on the same shared storage worklet will be rejected.
SharedStorageWorklet
is accessed viaWindowSharedStorage.worklet
.
In this article
Examples
js
// Randomly assigns a user to a group 0 or 1function getExperimentGroup() { return Math.round(Math.random());}async function injectContent() { // Add the module to the shared storage worklet await window.sharedStorage.worklet.addModule("ab-testing-worklet.js"); // Assign user to a random group (0 or 1) and store it in shared storage window.sharedStorage.set("ab-testing-group", getExperimentGroup(), { ignoreIfPresent: true, }); // Run the URL selection operation const fencedFrameConfig = await window.sharedStorage.selectURL( "ab-testing", [ { url: `https://your-server.example/content/default-content.html` }, { url: `https://your-server.example/content/experiment-content-a.html` }, ], { resolveToConfig: true, }, ); // Render the chosen URL into a fenced frame document.getElementById("content-slot").config = fencedFrameConfig;}injectContent();
See theShared Storage API landing page for a walkthrough of this example and links to other examples.
Specifications
Specification |
---|
Shared Storage API> # sharedstorageworklet> |
Browser compatibility
Loading…