Origin private file system

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2023⁩.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

The origin private file system (OPFS) is a storage endpoint provided as part of the File System API, which is private to the origin of the page and not visible to the user like the regular file system. It provides access to a special kind of file that is highly optimized for performance and offers in-place write access to its content.

Working with files using the File System Access API

The File System Access API, which extends the File System API, provides access to files using picker methods. For example:

  1. Window.showOpenFilePicker() allows the user to choose a file to access, which results in a FileSystemFileHandle object being returned.
  2. FileSystemFileHandle.getFile() is called to get access to the file's contents, the content is modified using FileSystemFileHandle.createWritable() / FileSystemWritableFileStream.write().
  3. FileSystemHandle.requestPermission({mode: 'readwrite'}) is used to request the user's permission to save the changes.
  4. If the user accepts the permission request, the changes are saved back to the original file.

This works, but it has some restrictions. These changes are being made to the user-visible file system, so there are a lot of security checks in place (for example, safe browsing in Chrome) to guard against malicious content being written to that file system. These writes are not in-place, and instead use a temporary file. The original is not modified unless it passes all the security checks.

As a result, these operations are fairly slow. It is not so noticeable when you are making small text updates, but the performance suffers when making more significant, large-scale file updates such as SQLite database modifications.

How does the OPFS solve such problems?

The OPFS offers low-level, byte-by-byte file access, which is private to the origin of the page and not visible to the user. As a result, it doesn't require the same series of security checks and permission grants and is therefore faster than File System Access API calls. It also has a set of synchronous calls available (other File System API calls are asynchronous) that can be run inside web workers only so as not to block the main thread.

To summarize how the OPFS differs from the user-visible file system:

  • The OPFS is subject to browser storage quota restrictions, just like any other origin-partitioned storage mechanism (for example IndexedDB API). You can access the amount of storage space the OPFS is using via navigator.storage.estimate().
  • Clearing storage data for the site deletes the OPFS.
  • Permission prompts and security checks are not required to access files in the OPFS.
  • Browsers persist the contents of the OPFS to disk somewhere, but you cannot expect to find the created files matched one-to-one. The OPFS is not intended to be visible to the user.

How do you access the OPFS?

To access the OPFS in the first place, you call the