-->

Check if offline application cache disabled by bro

2019-08-03 23:15发布

问题:

So I've got an application that stores offline data using an ApplicationCache. Long story short, it's silently dying for users who don't have offline data enabled, most commonly because they've got history turned off or are using private browsing, and I'd like to catch this and give the user a clear error.

It appears that this can be detected by checking the cache's state, which will return 0 UNCACHED when there is no cache... but I'm not sure if that means that there is no cache yet, or that there's no cache guaranteed (and because there should be, something's wrong). The official definition is less than useful:

UNCACHED (numeric value 0)
The ApplicationCache object's cache host is not associated with an application
cache at this time.

In practice, though, aborting on cache.status==0 seems to work fine at least on Firefox. Am I doing it right, or is there a better way? Extra points if somebody can dig up a state diagram.

Update: Nope, it also returns 0/UNCACHED if the user has never been to that page before. Gar!

回答1:

Listen for the error event on applicationCache. When this fires and applicationCache.status===applicationCache.UNCACHED, one reason among many is that caching is disabled. The message to the user may suggest that caching is turned off.