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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️