DEV Community

hemanth.hm
hemanth.hm

Posted on

5 2

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay