views:

407

answers:

2

Hi,

i'm building a iphone app with jqtouch and i use a cachemanifest to cache all the static files (images, css, javascript) to make it load faster. However the page uses php for the dynamic content and i don't want to cache that. So i'm generating the cachemanifest with this php-script(manifest.php):

<?php
header('Content-Type: text/cache-manifest');
echo "CACHE MANIFEST\n";

$hashes = "";
$lastFileWasDynamic = FALSE;

$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file) {
  if ($file->IsFile() && $file != "./manifest.php" &&
    substr($file->getFilename(), 0, 1) != ".") {
    if(preg_match('/.php$/', $file)) {
      if(!$lastFileWasDynamic) {
        echo "\n\nNETWORK:\n";
      }
      $lastFileWasDynamic = TRUE;
    } else {
      if($lastFileWasDynamic) {
        echo "\n\nCACHE:\n";
        $lastFileWasDynamic = FALSE;
      }
    }
    echo $file . "\n";
    $hashes .= md5_file($file);
  }
}

echo "\nNETWORK:\nhttp://chart.apis.google.com/\n\n# Hash: " . md5($hashes) . "\n";
?>

This actually works really good except for one irritating thing:

From what i read somewhere the file that calls the cachemanifest is automaticly included in the manifest and is beeing cached. Wich means that my start-page index.php, where i call the cachemanifest is beeing cached. This leads to very irritating problems.

is there any way to deal with this or any smart workaround? The page is in the cachemanifest listed as NETWORK, but it looks like this is beeing overruled by the fact that the cachemanifest is called from the file.

+1  A: 

I have the same experience, but have the following possible workaround on my todo-list:

  • create a manifest with all static assets
  • include a reference to that manifest in only one html-page (buildCache.php)
  • check if window.applicationCache is supported and if so:
    • redirect once per session to cache.html to create/check/update the cache
    • have buildCache.php display some info about what is being done (using the applicationCache eventlisteners)
    • have buildCache.php redirect back to normal index (where the manifest is not defined)

I hope (and someone claimed this is the case in a comment on my blog) that all pages on the same domain will use the static assets in the applicationCache, even if the manifest is not referenced in all of them.

futtta
thanks for your input.i've tried to create a amnifest with only the static content, but it haven't worked if i don't include every page in the manifest file. and it will also automaticly include the file that asks for it, wich is the main problem.tried to make special page to cache the content, but it haven't worked like i wanted it to yet, will continue to experiment on this one.
Volmar
ok, keep us posted, i'm very interested in the outcome! :)
futtta
+1  A: 

futta's idea is right, but what you will probably find is that only one section of your frontpage changes often. Leave that empty, then let the rest of the page be cached and don't worry about it. When you visit the page, the cached version is called up instantly, and you can run a script to grab the dynamic page fragment from the server and set it with innerHTML to complete the page. The effect is that there is still one HTTP request (plus one for the manifest), so it is no slower, and it addition you can show part of your app while the dynamic section is being downloaded. If you ever want to refresh the whole page, have a comment in the manifest marking the version, and increment that to reload the whole app.

Clean and neat. I think that is how the system is intended to be used, without trying to avoid a bit of javascript, since that is after all the only way you can play around with the offline and do useful things with the app when offline.

Nicholas Wilson
sounds like a smart way to do it! will try it out and report back here..
Volmar