Managing HTML5 Offline Storage

Published on Updated on

Caution

Important: Chrome will be removing support for Chrome Apps on all platforms. Chrome browser and the Chrome Web Store will continue to support extensions. Read the announcement and learn more about migrating your app.

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 1/3 of the 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) * .333 .

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 1/3 (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 half 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 other offline APIs like IndexedDB and Application Cache.

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.

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.

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 as well as hosted and installed web apps.
PermissionNone. 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 or extension.
User experience at first useInvisible 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 or extension.

User experience at subsequent requests for increased storageNot 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 spaceUp 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 caseCaching.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)

Note: Web storage APIs like LocalStorage and SessionStorage remain fixed at 5 MB.

File System API

Offline APIs

  • App Cache
  • File System
  • IndexedDB
  • WebSQL (deprecated)

Note: Web storage APIs like LocalStorage and SessionStorage remain fixed at 5 MB.

Managing your quota

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

The API is implemented with the global object window.webkitStorageInfo.

For the reference documentation, see the next section.

Querying storage usage and availability

To query the storage size that is being used and the available space left for the host, call queryUsageAndQuota() with the following:

  • Type of storage you want to check
  • Success callback

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 capacity left
// Choose either Temporary or Persistent
navigator.webkitTemporaryStorage.queryUsageAndQuota (
function(usedBytes, grantedBytes) {
console.log('we are using ', usedBytes, ' of ', grantedBytes, 'bytes');
},
function(e) { console.log('Error', e); }
);

If you want to ask for the status of persistent storage, simply replace webkitStorageInfo.TEMPORARY with webkitStorageInfo.PERSISTENT. The enum is also in the window object (global namespace), so you can also use window.PERSISTENT and window.TEMPORARY.

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 requestQuota() with the following:

  • Type of storage
  • Size
  • Success callback

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 requestQuota() 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)
var requestedBytes = 1024*1024*10; // 10MB

navigator.webkitPersistentStorage.requestQuota (
requestedBytes, function(grantedBytes) {
window.requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);

}, function(e) { console.log('Error', e); }
);
});

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.

API reference

This section documents the methods of the Quota Management API.

Constants

The following are webkitStorageInfo constants, which indicate the type of storage.

ConstantValueDescription
TEMPORARY0Temporary storage.
PERSISTENT1Persistent storage.

Method overview

Methods

queryUsageAndQuota

Check the storage size that is being used and the available space left for the host.

 // you could also use it from webkitPersistentStorage
navigator.webkitTemporaryStorage.queryUsageAndQuota(
successCallback,
errorCallback);
  • successCallback: Optional callback with two parameters:

    • The current number of bytes the app is using.
    • The number of bytes left in the quota.
  • errorCallback: Optional error callback.

requestQuota

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.

 // you could also use it from webkitTemporaryStorage
navigator.webkitPersistentStorage.requestQuota (
newQuotaInBytes,
quotaCallback,
errorCallback);
Parameters
  • newQuotaInBytes: The amount of bytes you want in your storage quota.
  • successCallback: Optional callback that passes the amount of bytes granted.
  • errorCallback: Optional error callback.

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.

Updated on Improve article

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.