DEV Community

hemanth.hm
hemanth.hm

Posted on

navigator.storage

navigator.storage is a read-only property that returns a singleton StorageManager that will help use fetch the overall storage capabilities of the browser for the current context.

StorageManager helps us to estimate how much more space is available for local storage, it also helps to configure persistence of data stores.

IDL:

[SecureContext,
 Exposed=(Window,Worker)]
interface StorageManager {
  Promise<boolean> persisted();
  [Exposed=Window] Promise<boolean> persist();

  Promise<StorageEstimate> estimate();
};

dictionary StorageEstimate {
  unsigned long long usage;
  unsigned long long quota;
};
Enter fullscreen mode Exit fullscreen mode

Usage:

Checks for storage API existence.

const hasStorage = navigator.storage;
const hasPersist = hasStorage && navigator.storage.persist;

Enter fullscreen mode Exit fullscreen mode

Create an stroageManager instance.

const storageManager = hasStorage && navigator.storage;
Enter fullscreen mode Exit fullscreen mode

Estimate the available storage space.


const estimate = await storageManager.estimate();

/*
Would give us something like:

{
  quota: 32571287142, 
  usage: 3351594
}
*/
Enter fullscreen mode Exit fullscreen mode

Can persist?

const canPersist = hasPersist && await navigator.storage.persist();

// ^ Will be true of false.

/*

true -> Storage will not be cleared until explicitly cleared. 

false -> Storage might be cleared based on UA need.
*/

Enter fullscreen mode Exit fullscreen mode
const persisted = hasPersisted && await navigator.storage.persisted();

/*

true -> box mode is persistent for the site's storage.

A box, the primitive these APIs store their data in.
A way of making that box persistent.
A way of getting usage and quota estimates for an origin.

*/

Enter fullscreen mode Exit fullscreen mode

GIF FTW!

navigator.storage.gif

Top comments (0)