title: Lazy loading non-HTML pages in HTML5 offline cache
body: <p>The HTML5 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#some-sample-manifests" rel="nofollow">Offline Web application spec</a> gives an example of how to lazily load HTML pages into the cache:</p>
  
  <pre><code>CACHE MANIFEST
  FALLBACK:
  / /offline.html
  NETWORK:
  *
  </code></pre>
  
  <p>This is explained in <a href="http://fortuito.us/diveintohtml5/offline.html#fallback" rel="nofollow">Dive Into HTML5</a> with the example of Wikipedia: You certainly don't want to cache the entire of Wikipedia when the website loads, but you want any page the user visits to become cached. Any page the user visits while offline should display a custom error page.</p>
  
  <p>The trick of this approach is that each HTML page explicitly includes the manifest. Any page that includes the manifest is automatically included into the cache, without having to be explicitly mentioned. So this example will load HTML pages from the network, and insert them into the cache, and if offline, any page in the cache will work, and any non-cached page will default to the offline.html page.</p>
  
  <p>The problem is for non-HTML files, which have no ability to include the manifest. Specifically, I am writing a game in JavaScript and it has a bunch of music tracks. I have the following requirements:</p>
  
  <ul>
  <li>I don't want the user to have to download all the music when the game loads,</li>
  <li>If the user encounters a new music track while online, that track should be downloaded and cached,</li>
  <li>If the user encounters a new music track while offline, it doesn't matter if they hear silence.</li>
  <li>If the user encounters a music track that they have already heard while offline, it should be played from the cache.</li>
  </ul>
  
  <p>Were the music files HTML, I could have used the above technique, and given them a <code>manifest=...</code> attribute to get them into the cache when they are first loaded. But they are not HTML, so I can't do that. Is there any way to get non-HTML resources to be saved to the cache when they are loaded, but not ahead of time?</p>
  
  <p>Note: I'm not very familiar with the traditional HTTP caching mechanism. It's possible that it can be used instead, but in my experience, even if files are cached by the browser, they don't work if the browser is offline. If it's possible to do it this way, how should I configure the cache headers?</p>