SharedStorageWorklet

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 the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Giao diện SharedStorageWorklet của Shared Storage API đại diện cho shared storage worklet của nguồn gốc hiện tại.

SharedStorageWorklet không có thuộc tính hoặc phương thức riêng. Thay vào đó, nó kế thừa phương thức addModule() từ giao diện Worklet. Phương thức này được dùng để thêm module.

Không giống như Worklet thông thường:

  • Nếu trang gọi không bao gồm Shared Storage API trong quy trình đăng ký privacy sandbox, các lệnh gọi sharedStorageWorklet.addModule() sẽ bị từ chối.
  • SharedStorageWorklet chỉ cho phép thêm một module duy nhất, vì lý do quyền riêng tư. Ngay cả khi đăng ký thành công, các lệnh gọi lặp lại addModule() trên cùng shared storage worklet sẽ bị từ chối.

SharedStorageWorklet được truy cập qua WindowSharedStorage.worklet.

Worklet SharedStorageWorklet

Ví dụ

js
// Ngẫu nhiên gán người dùng vào nhóm 0 hoặc 1
function getExperimentGroup() {
  return Math.round(Math.random());
}

async function injectContent() {
  // Thêm module vào shared storage worklet
  await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");

  // Gán người dùng vào nhóm ngẫu nhiên (0 hoặc 1) và lưu trong shared storage
  window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
    ignoreIfPresent: true,
  });

  // Chạy thao tác URL selection
  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 URL đã chọn vào fenced frame
  document.getElementById("content-slot").config = fencedFrameConfig;
}

injectContent();

Xem trang chủ Shared Storage API để biết hướng dẫn về ví dụ này và các liên kết đến các ví dụ khác.

Thông số kỹ thuật

Thông số kỹ thuật
Shared Storage API
# sharedstorageworklet

Tương thích trình duyệt

Xem thêm