Skip to content

Instantly share code, notes, and snippets.

@agektmr
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agektmr/9247242 to your computer and use it in GitHub Desktop.
Save agektmr/9247242 to your computer and use it in GitHub Desktop.

Managing HTML5 Offline Storage

HTML5 introduced many storage APIs that let you store a large amount of data locally in your users' browsers. But the amount of space allocated for each app is, by default, restricted to a few megabytes. Google Chrome lets you ask for a larger storage quota, beyond the previous limit of just 5 MB.

This document introduces you to the basic concepts around the types of storage used in Chrome and describes the experimental Quota Management API, which lets you manage your storage quota. The document assumes that you are already familiar with the general concepts of client-side storage and know how to use offline APIs.

Contents

  1. Types of storage

    1. Temporary
    2. Persistent
    3. Unlimited
    4. Comparing Storage Types
  2. Managing your quota

    1. Query storage usage and availability
    2. Ask for more storage
    3. Reset quota for testing
  3. API reference

    1. Constants
    2. Method overview
    3. Methods
  4. Future development

Types of storage

In Google Chrome, you can ask for three types of storage:

These storage types are described in greater detail in the following sections and compared with each other in the table below.

Temporary storage

Temporary storage is transient storage that is available to any web app. Chrome automatically gives your app temporary storage, so you do not need to request allocation.

Sharing the pool

Temporary storage is shared among all web apps running in the browser. The shared pool can be up to one third of available disk space. Storage already used by apps is included in the calculation of the shared pool; that is to say, the calculation is based on (available storage space + storage being used by apps) / 3 .

Each app can have up to 20% of the shared pool. As an example, if the total available disk space is 60 GB, the shared pool is 20 GB, and the app can have up to 4 GB. This is calculated from 20% (up to 4 GB) of one third (up to 20 GB) of the available disk space (60 GB).

Asking for more space

Although you can query for the amount of storage space available for your app and the amount of data already stored for your app, you cannot ask for more temporary storage space. If an app exceeds the allocated quota, an error is thrown.

Running out of storage

Once the storage quota for the entire pool is exceeded, the entire data stored for the least recently used host gets deleted. The browser, however, will not expunge the data in LocalStorage and SessionStorage. For data stored in other offline APIs, the browser deletes the data in whole and not in part so that app data doesn't get corrupted in unexpected ways. 

As each app is limited to a maximum of 20% of the storage pool, deletion is likely only if the user is actively running more than five offline apps that are each using the maximum storage.

However, available storage space can shrink as users add more files on their hard drives. When the available disk space gets tight (Remember, the shared pool only gets one third of the current available disk space), the browser deletes all the data stored for the least recently used host.

Persistent storage

Persistent storage is storage that stays in the browser unless the user expunges it. It is available only to apps that use the File System API, but will eventually be available to IndexedDB.

An application can have a larger quota for persistent storage than temporary storage, but you must request storage using the Quota Management API and the user must grant you permission to use more space. Chrome presents an info bar that prompts the user to grant the app more local storage space.

Chrome presents an info bar for users to grant an app permission to have more storage space.

Unlimited storage

Unlimited storage is similar to persistent storage, but it is available only to Chrome apps and extensions (.crx files). The size of unlimited storage is limited only by the availability of space in the user's hard drive. You can ask for the unlimitedStorage permission in the manifest file for an app or extension. At installation, the user is informed of permissions required by the app or extension. By proceeding with the installation, the user implicitly grants permission for all pages whose URLs are listed in the manifest.json file.

To learn more, see the respective developer guides for apps and extensions.

Back to top

Comparing Storage Types

The following table describes the differences among the three types of storage.

 Temporary storagePersistent storageUnlimited storage
**Basic description**Transient storage that is available to any web app.

It is automatic and does not need to be requested.

Permanent storage that must be requested through the Quota Management API and granted by users.Permanent storage for Chrome extensions and apps.

It is set in the manifest file and must be granted by users.

**Availability**All web apps. All web apps.Unique to [Chrome extensions](http://code.google.com/chrome/extensions/index.html) as well as hosted and installed [web apps](http://code.google.com/chrome/apps/index.html).
**Permission**None. You can use it without explicitly requesting it.You have to request more storage using the Quota Management API. You can ask for the `unlimitedStorage`permission in the manifest file for the [app](https://developers.google.com/chrome/apps/docs/developers_guide?hl=ja#manifest)or [extension](http://code.google.com/chrome/extensions/manifest.html#permissions).
**User experience at first use**Invisible to the user. The app just runs.Chrome displays an info bar that prompts the user to either accept or decline the storage request.

But if the amount of quota you request is actually less than the app's current allocation, no prompt is shown. The larger quota is kept.

At installation, the user is informed of permissions required by the app or extension. By proceeding with the installation, the user implicitly grants permission for all pages whose URLs are listed in the manifest.json file for [app](https://developers.google.com/chrome/apps/docs/developers_guide?hl=ja#manifest) or[extension](http://code.google.com/chrome/extensions/manifest.html).
**User experience at subsequent requests for increased storage**Not applicable. You cannot ask for more temporary storage.Chrome prompts the user again.

 

Chrome does not prompt the user after installation, regardless of the requests for increased quota by the app or extension.
**Persistence of data**Transient. The browser can delete the data. Persistent. The browser doesn't delete the data unless the user instructs it to. Data is available in subsequent accesses.

Do not assume that the data is permanent, because the user can delete it.

Same as persistent storage.

 

**Default storage space**Up to 20% of the shared pool. 0 MB. You have to explicitly ask for a specific storage space.0 MB. You have to explicitly ask for`unlimitedStorage` in the manifest file.

If you do not specify your storage requirements, Chrome allocates storage to the app from the shared pool of temporary storage.

**Maximum storage space**Up to 20% of the shared pool.As large as the available space on the hard drive. It has no fixed pool of storage.As large as the available space on the hard drive.
**Recommended use case**Caching.Apps that work offline or have a large number of assets.Apps that were designed to run in Google Chrome.
**APIs that can use it**Offline APIs
  • App Cache
  • File System
  • IndexedDB
  • WebSQL (deprecated since November 18, 2010)
File System APIOffline APIs
  • App Cache
  • File System
  • IndexedDB
  • WebSQL (deprecated)

Back to top

Managing your quota

You need to turn on Enable experimental Web Platform feature (chrome://flags/#enable-experimental-web-platform-features) in chrome://flags/ page to use the quota APIs as of Chrome 35.

With the Quota Management API, which was introduced in Chrome 13, revised in Chrome 34, you can do the following:

The API is implemented with the global object navigator.storageQuota.

For the reference documentation, see the next section.

Querying storage usage and quota

To query the storage size that is being used and the total size for the host, call queryInfo() with the following:

  • Type of storage you want to check
  • Chain then() method as Promise

The usage reported by the API might not match with the actual size of the user data, as each storage might need some extra bytes to store its metadata. Also, status updates can lag, resulting in the API not reflecting the most recent storage status. 

The following code snippet shows how you can ask about storage space:

// Request storage usage and total size
navigator.storageQuota.queryInfo('temporary').then( //the type can be either 'temporary' or 'persistent'
  function onSuccess(storageInfo) {
    console.log("Used quota: " + storageInfo.usage + ", Total quota: " + storageInfo.quota);
  },
  function onError(e) {
    console.log('Error', e.message);
  }
);

If you want to ask for the status of persistent storage, simply replace temporary with persistent.

Back to top

Asking for more storage

You don't need to ask for more temporary storage as the allocation is automatic, and you can't get beyond the maximum limit (as described in the table).

For persistent storage for File System API, the default quota is 0, so you need to explicitly request storage for your application. Call requestPersistentQuota() with the following:

  • Size
  • Chain then() method as Promise

Depending on what you ask for, the following happens:

  • If you ask for a larger quota, the browser presents an info bar to the user and prompts them to either grant or deny permission for increased quota. In some cases, the request might be silently rejected, and the current quota or smaller quota is returned.
  • If the amount of quota you request is less than the app's current allocation, no prompt is shown.
  • If you ask for more storage than what is allowed, you get an error (QUOTA_EXCEEDED_ERR). 
  • If you call requestPersistentQuota() again after the user has already granted permission, nothing happens. So don't bother calling the method again.

The following shows how you can ask for more storage space:

// Request Quota (only for File System API)    
navigator.storageQuota.requestPersistentQuota(1024*1024).then(
  function onSuccess(storageInfo) {
    window.webkitRequestFileSystem(PERSISTENT, storageInfo.quota, onInitFs, errorHandler);
  },
  function onError(e) {
    console.log('Error', e.message);
  }
);

Back to top

Detecting storage changes

This feature is not implemented yet as of Chrome 35.

To detect changes of storage usage, you can use StorageWatcher to fire storagechange events.

// Set up an watcher event for temporary storage changes
var watcher = new StorageWatcher('temporary');
watcher.addEventListener('storagechange', function(event) {
  console.log("Used quota: " + event.usage + ", Total quota: " + event.quota);
});

You can also check storage size at regular intervals.

// Set up an watcher event for persistent storage changes that fires every 60 seconds
var watcher = new StorageWatcher('persistent', 60);
watcher.onstoragechange = function(event) {
  console.log("Used quota: " + event.usage + ", Total quota: " + event.quota);
};

By using StorageWatcher, you can observe usage of storage and take action before going over quota.

Resetting quota for testing

When you are testing storage in your app, you might want to clear the stored data so that you can test quota management afresh in your app. To do so:

  1. Enter chrome://settings/cookies in the omnibox (the address bar).
  2. Search for your app.
  3. Select your app.
  4. Click the X on the right side of the highlighted selection.

Back to top

API reference

This section documents the methods of the Quota Management API.

StorageInfo

Attributes

Name Type Description
quota unsinged long long The current upper limit of the storage space that can be used by the application for a given storage type.
usage unsigned long long The total amount of data (in bytes) stored by the application for a given storage type.

StorageQuota

Methods

queryInfo()

Check the storage size that is being used and the total size of the host.

navigator.storageQuota.queryInfo(
  'temporary',   // or 'persistent'
).then(
  successCallback,
  errorCallback
);
storageType
The type of storage of interest in string. The value could either be `temporary` or `persistent`.
successCallback
Optional callback with `StorageInfo` parameter
errorCallback
Optional error callback.

Attributes

Name Type Description
supportedTypes StorageType[] List of all storage types supported by the UA.

Back to top


requestPersistentQuota()

Ask for more storage. The browser presents an info bar to prompt user to grant or deny the app the permission to have more storage.

navigator.storageQuota.requestPersistentQuota(
  newQuota
).then(
  successCallback,
  errorCallback
);
newQuota
The amount of bytes you want in your persistent storage quota.
successCallback
Optional callback with `StorageInfo` parameter.
errorCallback
Optional error callback.

StorageWatcher

Methods

constructor()
var watcher = new StorageWatcher(
  storageType,
  rate
);
storageType
The type of storage of interest in string. The value could either be `temporary` or `persistent`.
rate
Optional frequency in second(s) of events fired. If ommited, events will be fired as UA detects storage usage changes.
close()

Explicitly closes the watcher. Once close() is called the UA never fires events on this instance.

Attributes

Name Type Description
type storageType type value which this watcher is constructed with and is monitoring changes on.
rate unsigned long rate value which this value is constructed with.

Future development

The plan is to put all HTML5 offline storage APIs—including IndexedDB, Application Cache, File System, and other APIs that might be specified—under the Quota Management API. You will be able to manage all storage allocation with it. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment