docs.rodeo

MDN Web Docs mirror

storage.sync

{{AddonSidebar}} 

Represents the sync storage area. Items in sync storage are synced by the browser. The data is then available on all instances of the browser the user is logged into (for example, when using a Mozilla account on desktop versions of Firefox or a Google account on Chrome) across different devices.

For desktop Firefox, a user must have Add-ons selected in the “Sync” section in "about:preferences". Firefox for Android does not synchronize data with the user’s account. See Firefox bug 1625257.

The implementation of storage.sync in Firefox relies on the Add-on ID. If you use storage.sync, you must set an ID for your extension using the browser_specific_settings manifest.json key.

The main use case of this API is to store preferences about your extension and allow the user to sync them to different profiles.

Storage quotas for sync data

The browser enforces limits on the amount of data each extension is allowed to store in the sync area:

Name Description Value in bytes
Maximum total size The maximum total amount of data that each extension is allowed to store in the sync storage area, as measured by the JSON stringification of every value plus every key's length. 102400
Maximum item size The maximum size of any one item that each extension is allowed to store in the sync storage area, as measured by the JSON stringification of the item's value plus the length of its key. 8192
Maximum number of items The maximum number of items that each extension can store in the sync storage area.

512

If an extension attempts to store items that exceed these limits, calls to {{WebExtAPIRef("storage.StorageArea.set()", "storage.sync.set()")}}  are rejected with an error. An extension can use {{WebExtAPIRef("storage.StorageArea.getBytesInUse()", "storage.sync.getBytesInUse()")}}  to find out how much of its quota is in use.

Synchronization process

In Firefox, extension data is synced every 10 minutes or whenever the user selects Sync Now (in Settings > Sync or from the Mozilla account icon). When the browser performs a sync, for each key stored, it:

This means that, for each key, a change on the server takes precedence over a change in the browser’s sync storage.

This mechanism is generally OK for data such as user preferences or other global settings changed by the user.

However, a key’s value can be updated on one browser and synchronized then updated on a second browser before the second browser is synchronized, resulting in the local update being overwritten during sync. This mechanism is, therefore, not ideal for data aggregated across devices, such as a count of page views or how many times an option is used. To handle such cases, use {{WebExtAPIRef("storage.StorageArea.onChanged", "storage.sync.onChanged")}}  to listen for sync updates from the server (for example, a count of page views on another browser instance). Then adjust the value locally to take the remote value into account (for example, update the total views based on the remote count and new local count).

Methods

The sync object implements the methods defined on the {{WebExtAPIRef("storage.StorageArea")}}  type:

Events

The sync object implements the events defined on the {{WebExtAPIRef("storage.StorageArea")}}  type:

{{WebExtExamples}} 

Browser compatibility

{{Compat}} 

[!NOTE] This API is based on Chromium’s chrome.storage API. This documentation is derived from storage.json in the Chromium code.

In this article

View on MDN