Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify a wildcard in the HTML5 cache manifest to load all images in a directory?

I have a lot of images in a folder that are used in the application. When using the cache manifest it would be easier maintenance wise if I could specify a wild card to load all the images or files in a certain directory to be cached.

E.g.

CACHE MANIFEST
# 2011-11-3-v0.1.8
#--------------------------------
# Pages
#--------------------------------
../index.html
../edit.html
#--------------------------------
# JavaScript
#--------------------------------
../js/jquery.js
../js/main.js
#--------------------------------
# Images
#--------------------------------
../img/*.png

Can this be done? Have tried it in a few browsers with ../img/* as well but it doesn't seem to work.

like image 616
zuallauz Avatar asked Nov 03 '11 20:11

zuallauz


People also ask

What is a manifest file in HTML5?

The manifest file is a simple text file that lists the resources the browser should cache for offline access. To learn more about how to create the manifest file, please read our HTML5 Application Cache chapter.

What does the section cache do in a manifest file?

The CACHE section contains files you want to load and store on the user's machine. The first time the user loads your app, the browser will store all the files in the CACHE section of your app cache manifest. The NETWORK section contains all the resources that require a network connection to be loaded.

What is the use of the manifest attribute in HTML?

The manifest attribute of the <html> element specifies a URL of an application cache manifest that is downloaded in the early stages of page load. Note: manifest-based caching mechanism has been deprecated.


4 Answers

It would be easier, but how's it going to work? The manifest file is something which is parsed and acted upon in the browser, which has no special knowledge of files on your server other than what you've told it. If the browser sees this:

../img/*.png

What is the first image the browser should request from the server? Let's start with these:

../img/1.png
../img/2.png
../img/3.png
../img/4.png
...
../img/2147483647.png

That's all the images that might exist with a numeric name, stopping semi-arbitrarily at 231-1. How many of those 2 billion files exist in your img directory? Do you really want a browser making all those requests only to get 2 billion 404s? For completeness the browser would probably also want to request all the zero-filled equivalents:

../img/01.png
../img/02.png
../img/03.png
../img/04.png
...
../img/001.png
../img/002.png
../img/003.png
../img/004.png
...
../img/0001.png
../img/0002.png
../img/0003.png
../img/0004.png
...

Now the browser's made more than 4 billion HTTP requests for files which mostly aren't there, and it's not yet even got on to letters or punctuation in constructing the possible filenames which might exist on the server. This is not a feasible way for the manifest file to work. The server is where the files in the img directory are known, so it's on the server that the list of files has to be constructed.

like image 176
robertc Avatar answered Oct 14 '22 05:10

robertc


I don't think it works that way. You'll have to specify all of the images one by one, or have a simple PHP script to loop through the directory and output the file (with the correct text/cache-manifest header of course).

like image 23
Madara's Ghost Avatar answered Oct 14 '22 06:10

Madara's Ghost


It would be a big security issue if browsers could request folder listings - that's why Tomcat turns that capability off by default now.

But, the browser could locate all matches to the wildcards referenced by the pages it caches. This approach would still be problematic (like, what about images not initially used but set dynamically by JavaScript, etc., and it would require that all cached items not only be downloaded but parsed as well).

like image 37
stevec Avatar answered Oct 14 '22 07:10

stevec


If you are trying automate this process, instead of manually doing it. Use a script, or as I do I use manifestR. It will output your manifest/appcache file and all you have to do is copy and paste. I've used it successfully and usually only have to make a few changes.

Also, I recommend using the network header with the wild card:

NETWORK:
*

This allows all assets from other linked domains via JSON, for instance, to download into the cache. I believe that this is the only header where you can specify a wildcard. Like the others have said here, it's for security reasons.

like image 30
Paul Avatar answered Oct 14 '22 07:10

Paul