What happens on the web stays on the web, right? Unfortunately, wrong. We know about web cookies, which sound better than they taste. When you visit a web site in a browser such as Safari, the site may store bits of data called cookies on your disk. Cookies are often abused for tracking but remain a necessary evil to allow you to remain logged into web site accounts. What you may not realize is that cookies are just the tip of the iceberg. We're talking hundreds of megabytes or even gigabytes of iceberg, enough to put a laptop's disk underwater. I thought the purpose of a web browser was to browse the web, didn't you? If I wanted to browse my file system, I'd use Finder. For the purpose of this blog post I did browse my file system, and I'll tell you what I found.
Safari saves data from the web in several locations on disk. The folder ~/Library/Safari/LocalStorage
contains data from the Web Storage API, which allows web sites to store key/value pairs on your disk. Web Storage is accessed via window.localStorage
in JavaScript. The folder ~/Library/Safari/Databases
contains data from the IndexedDB API, which allows web sites to save entire databases! These databases are accessed via window.indexedDB
in JavaScript. There used to be yet another API called WebSQL that allowed web sites to store databases on your disk, but WebSQL was deprecated and removed. Safari version 13 and later should no longer store WebSQL data on your disk, so fortunately it's not something we need to worry about anymore.
If you're a regular reader of my blog, you may remember that I've mentioned the folder ~/Library/Safari/LocalStorage
before. It's involved in a macOS privacy protections bypass that I discovered (and that remains unfixed). The files in the folder are named after the web sites you visit, which allows the File System Events API to determine your web browsing history. Hence, this is another reason besides disk space that you might not want Web Storage data saved.
The folder ~/Library/Containers/com.apple.Safari/Data/Library/Caches
is perhaps the biggest (waste) of them all. It stores files that Safari downloads from the web, such as HTML, JavaScript, and images. It also stores Service Worker registrations, which are… I don't really know what they are, to be honest. Apparently they're little web worker bees that do stuff in the background, like… make web honey for the web cookies? Whatever they are, they bug me.
Web browsers cache downloaded files to disk so that the files can be reloaded faster. However, this practice began way back when the internet was generally much slower. When people still connected to the internet via dial-up! Nowadays with ubiquitous broadband, web site caching seems less necessary. Moreover, caching leads to the problem of the stale cache, where the content of the web site has changed since the files were last cached, and thus the cached files are no longer identical to what would be downloaded anew from the web. Again, isn't the purpose of a web browser to browse the web, not your disk?
On the subject of web site caches, let's talk for a moment about the "Reload Page" menu item in Safari under the "View" menu, keyboard shortcut ⌘R. It turns out that "Reload Page" does not actually reload the page in the way you expect. I'm not sure exactly what "Reload Page" does, but it still seems to rely on the disk cache. If you hold down the option key, you see "Reload Page" replaced in the menu by "Reload Page From Origin", which is the reload you expect, the one that ignores the disk cache and loads everything again from the web. (There's another mysterious menu item "Reload With Flash", which I can't explain at all. Perhaps it has something to do with Flash Player??) I always want "Reload Page From Origin" rather than "Reload Page", so for convenience I remapped the keyboard shortcuts in System Preferences, Keyboard, Shortcuts, App Shortcuts:
Returning to the original topic, how do you stop Safari from caching web data to disk? As far as I know, there are no hidden preferences to control this, though I haven't done an extensive examination of the WebKit open source. Fortunately, there's still an easy way to stop it:
~/Library/Safari/LocalStorage
~/Library/Safari/LocalStorage
~/Library/Safari/Databases
and ~/Library/Containers/com.apple.Safari/Data/Library/Caches
What do I mean by "lock the folder"? If you open the "Get Info" window (keyboard shortcut ⌘I) for the folder in Finder, you'll see a "Locked" checkbox in the "General" section. Just check that box, and Safari will no longer be able to save files inside the folder. Problem solved!
I can't guarantee that locking the cache files won't break anything. In my testing it doesn't break anything, but certain web apps may rely on the Web Storage and/or IndexedDB API. Caveat emptor. If something important breaks, you can just unlock the folder again in Finder.
There's one more place I know of where Safari caches files: in the $TMPDIR/com.apple.Safari/WebKit
folder. Safari seems to store mostly video files here. However, this folder, and indeed everything in $TMPDIR
, is automatically deleted on reboot, so I personally don't worry about it much.
Postscript on Cookies:
I wish that Safari would take a cue from Firefox and Google Chrome in allowing fine-grained control over cookies. Safari has per-site preferences for Auto-Play, Downloads, Notifications, etc., but it doesn't have per-site preferences for cookies. Compare with Firefox and Google Chrome shown below. The best feature they have is to clear cookies when you quit the app, a feature I wish that Safari would adopt too.