Back to Content

WindowSharedStorage

files/en-us/web/api/windowsharedstorage/index.md

latest2.6 KB
Original Source

{{APIRef("Shared Storage API")}}{{deprecated_header}}

The WindowSharedStorage interface of the {{domxref("Shared Storage API", "Shared Storage API", "", "nocode")}} represents the shared storage for a particular origin within a standard browsing context.

WindowSharedStorage is accessed via {{domxref("Window.sharedStorage")}}.

{{InheritanceDiagram}}

Instance properties

  • {{domxref("WindowSharedStorage.worklet", "worklet")}} {{deprecated_inline}}
    • : Contains the {{domxref("SharedStorageWorklet")}} instance representing the shared storage worklet for the current origin. SharedStorageWorklet includes the {{domxref("Worklet.addModule", "addModule()")}} method, which is used to add a module to the shared storage worklet.

Instance methods

WindowSharedStorage inherits properties from its parent interface, {{domxref("SharedStorage")}}.

  • {{domxref("WindowSharedStorage.run", "run()")}} {{Deprecated_Inline}}
    • : Executes a Run output gate operation that has been registered in a module added to the {{domxref("SharedStorageWorklet")}} of the current origin.
  • {{domxref("WindowSharedStorage.selectURL", "selectURL()")}} {{Deprecated_Inline}}
    • : Executes a URL Selection output gate operation that has been registered in a module added to the {{domxref("SharedStorageWorklet")}} of the current origin.

Examples

js
// Randomly assigns a user to a group 0 or 1
function 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 the Shared Storage API landing page for a walkthrough of this example and links to other examples.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also