 True or false? Indexed DB is limited to 25 megs. False. Gone are the days of tiny storage quotas. True or false? Local storage should be avoided. True. It's synchronous and may cause performance issues by blocking the main thread. Alright, here's another one. True or false? Cookies are a great way to store data. False. They've got their uses but should never be used for storage. How about this one? AppCache is a great way to make your app work offline. Yeah, trick question. Absolutely false. AppCache is awful and it's going away soon, thankfully. So how should we be storing data and caching our critical app resources on the client? How much can we store? How does the browser deal with eviction? And be sure to stick around to the end and I'll tell you how you can start Chrome with only a tiny storage limit so you can test what happens when you exceed your storage quota. I'm Pete LaPage. Let's dive in to storage on the web. Modern storage makes it possible to store more than just small chunks of data on the user's device. Even in perfect wireless environments, caching and other storage techniques can substantially improve performance, reliability, and most importantly, the user experience. With the Cache Storage API, you can cache your static app resources like HTML, JavaScript, CSS, ensuring that they're always instantly available. And with IndexedDB, you can store all kinds of data, article content, user's documents, settings, and more. IndexedDB and the Cache Storage API are supported in every modern browser. They're both asynchronous and will not block the main thread. They're accessible from the window object, web workers, and service workers, making it easy to use them anywhere in your code. There are several other storage mechanisms that are available in the browser, but they've got limited use and may cause significant performance issues. If you're concerned about storing large amounts of data on the client, don't be. Unless you're trying to store several gigs, modern browsers typically won't even bat an eye. And even then, it really comes down to the amount of disk space available on the device. Of course, implementations vary by browsers. Firefox allows an origin to store up to two gigs. Safari allows an origin to use up to one gig, and when that limit is reached, Safari is currently the only browser that'll prompt the user to increase that limit. And Chrome, well, look, it's a little complex, but stick with me here. Chrome and most other Chromium-based browsers limit storage to 80% of the total disk space, and each origin can only use 75% of that. For example, if you had a 10 gig hard disk, Chrome would limit its storage to 8 gigs, then each origin would be limited to 6 gigs. Essentially, each origin will be allowed to use up to 60% of the total disk space. It sounds complex, but there's an easy way to see what's available. In many browsers, you can use the Storage Manager API to determine the amount of storage that's available to the origin and how much storage that you're already using. It reports the total number of bytes used and makes it possible to calculate the approximate bytes remaining. Unfortunately, the Storage Manager API isn't implemented in all browsers yet, so you must use feature detection before using it. But even when it is available, you still need to catch over quota errors. In some cases, and I'm looking at you, Chrome, it's possible for the available quota to exceed the actual amount of storage available. Most Chromium-based browsers factor in the amount of free space when reporting the available quota. Chrome does not, though, and it will always report 60% of the actual disk size. This helps to reduce the ability to determine the size of stored cross-origin resources. So what should you do when you go over quota? Most importantly, you should always catch and handle right errors, whether it's a quota exceeded error or something else. Then, depending on your app design, decide how to handle it. For example, delete content that hasn't been accessed in a long time, or remove data based on its size, or provide a way for users to choose what they want to delete. Both IndexedDB and the Cache API throw a DOM error named QuotaExceededError when you've exceeded the quota available. For IndexedDB, the transactions on a board handler will be called passing an event. That event will include a DOM exception in the error property, and if you check the name of the error, it'll return QuotaExceededError. For the Cache API, rights will reject with a QuotaExceededError DOM exception. Data stored in the browser can be cleared in a couple of ways. It's most commonly initiated by the user choosing to clear data in the browser's site setting panel. But it can also happen when faced with storage pressure like low disk space. In this case, browsers typically automatically delete data from the least recently used origins, and continue to delete that until the storage pressure has been relieved. If the app hasn't synced data with the server, it will cause data loss and means that the app won't have the resources needed to run, both of which can lead to a negative user experience. Thankfully, research by the Chrome team shows that this doesn't happen very often, and it's far more common for users to manually clear storage. Thus, if a user visits your site often, the chances are small that data will be deleted. Let's take a look at a specific example of how automatic eviction might happen in Chrome. Origin A is the least recently visited site. Origin B is the next least recently visited site, and so on. Origin E and Origin K are getting close to their quota limits, but they haven't reached it yet. And the overall usage is less than the total quota, so nothing is going to be evicted. Origin B has a star next to it because it was granted persistent storage, meaning that it can only be deleted by the user. Check out my article on web.dev for more info about persistent storage, when you should be using it, and how to request it. Now, let's say the user visits Origin N again, which happens to be a music playing site. The user saves a few more songs for offline listening. Now, each origin is still within its quota limit, but Chrome has exceeded the overall limit. To get back under the overall limit, Chrome will start evicting stored data from the least recently used origin first and continue until it's back under the total limit. Firefox and other Chromium-based browsers work in essentially the same way. Safari is a little different. When it's out of storage, it will prevent anything new from being written. But they recently implemented a new seven-day cap on all writable storage, including IndexedDB, Service Worker Registrations, and the Cache API. This means that after using Safari for seven days and not interacting with the site, it will evict all content for that site. This eviction policy does not apply to progressive web apps that have been added to the home screen, essentially installed PWAs. Check out the WebKit blog linked in the description for complete details. Modern computers typically have large hard drives, which makes it hard to test the over quota failures. So here's a little pro tip. Create a small RAM disk. Here, I've created a 500-meg RAM disk on my Mac. Then, start Chrome using the user-data-dir flag. This tells Chrome to store the user profile and user data on the RAM disk. Chrome now thinks my disk is only 500 megs, thus is going to limit my storage quota to only 300 megs, which I can quickly fill. This makes it much easier to verify that my code behaves properly when it hits those quota exceeded errors. Chrome DevTools also have helpful features for understanding what's going on with the data that you've stored. In the application panel, the Clear Storage panel will show you how much storage you're using for the origin and makes it easy to clear some or all of that data that you've got stored. The Storage panel lets you see what's in local and session storage, as well as what's in IndexedDB, including the actual databases, and even the individual entries. And the Cash Storage panel shows you what's stored in Cash Storage. Gone are the days of limited storage and prompting the user to store more and more data. Using the Cash Storage API and IndexedDB, you can effectively store all the resources that your app needs to run. Be sure to check out my article, Storage for the Web, where I've got additional details and info on some of the not-so-good storage mechanisms. Then, check out my article on Persistent Storage to learn how you can protect your data from being blown away, even when the device is facing storage pressure. See you soon!