Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force applicationCache to reload cached files

I'm using the HTML5 applicationCache to store many Javascript, CSS, image, etc. files for a page. If I update one of those files, the browser never reloads it. I have tried the following:

  • Calling applicationCache.update() on page load
  • Listening for applicationCache's updateready event, and calling swapCache() and window.location.reload()
  • Adding a timestamp comment to the manifest file itself to force the browser to realize the manifest has changed

Surely this can't be this hard. How do I convince the browser to re-request some cached file?

like image 756
Ben Dilts Avatar asked Feb 22 '11 19:02

Ben Dilts


People also ask

What is cache manifest file?

The cache manifest file is a simple text file that lists the resources the browser should cache for offline access.

What event is fired by the AppCache object when the cache download is complete?

If it finds a different version of the manifest file on the server than it previously encountered, it will download a new cache based on this new manifest. Once that happens, an updateready event will be fired, stating that an updated copy of the cache has finished downloading and is ready to be used.

How does application cache work?

How does Caching work? The data in a cache is generally stored in fast access hardware such as RAM (Random-access memory) and may also be used in correlation with a software component. A cache's primary purpose is to increase data retrieval performance by reducing the need to access the underlying slower storage layer.

Which of the following is not a valid section of AppCache manifest?

Which is not the section of manifest? Explanation: If the files are not in cache they come from a list of the files in the network. Cache is the default section.


1 Answers

To force any new (or changed) file to be downloaded, you must update the manifest file (add a version number comment or any change will do). What's probably happening is that you're getting an error. The most common one is that you might not be serving the manifest with the right mime type (text/cache-manifest). Did you configure your server correctly? The easiest way to check this is to open the page in Chrome and look in the console and the resources tab under AppCache to see if there is an error (it will complain about the file being served up incorrectly. You can also test it with the curl -I command:

curl -I $manifest_file_URL

It is also possible that your manifest file is getting cached (you can set the expires headers for it to expire rightaway). Also keep the reloading sequence in mind: your page will load from AppCache first (if it is there), then the browser checks if the manifest file is updated. If it is, download and place in the new version of the cache, but this will not automatically update the page (nor will swapCache()), you will have to refresh the page (at least) once more.

See also this presentation for more information on the topic.

like image 87
Peter Lubbers Avatar answered Oct 20 '22 16:10

Peter Lubbers