开发者

How to not cache a php file where a cachemanifest is beeing called?

开发者 https://www.devze.com 2022-12-26 08:04 出处:网络
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'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.


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.


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.


Another solution would be to keep your index.php as a blank loading page or splash screen of some sort, then redirecting the user to the actual dynamic php page. Since the manifest is in index.php and index.php redirects to real-index.php the problem might be less anoying.

0

精彩评论

暂无评论...
验证码 换一张
取 消