StorageManager: phương thức persist()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2021.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Phương thức persist() của giao diện StorageManager yêu cầu quyền dùng bộ nhớ lưu trữ bền vững, và trả về một Promise được giải quyết thành true nếu quyền được cấp và bucket mode là persistent, và false trong các trường hợp khác. Trình duyệt có thể chấp nhận hoặc từ chối yêu cầu này, tùy theo các quy tắc riêng của từng trình duyệt. (Để biết thêm chi tiết, xem hướng dẫn Storage quotas and eviction criteria.)

Note: Phương thức này không khả dụng trong Web Workers, mặc dù giao diện StorageManager thì có.

Cú pháp

js
persist()

Tham số

Không có.

Giá trị trả về

Một Promise được giải quyết thành một Boolean.

Ngoại lệ

TypeError

Được ném ra nếu không thể lấy shelf lưu trữ cục bộ. Ví dụ, nếu origin hiện tại là opaque origin hoặc nếu người dùng đã vô hiệu hóa lưu trữ.

Ví dụ

js
if (navigator.storage && navigator.storage.persist) {
  navigator.storage.persist().then((persistent) => {
    if (persistent) {
      console.log("Storage will not be cleared except by explicit user action");
    } else {
      console.log("Storage may be cleared by the UA under storage pressure.");
    }
  });
}

Thông số kỹ thuật

Specification
Storage
# dom-storagemanager-persist

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