如何不缓存正​​在调用cachemanifest的php文件?

发布于 2024-08-28 13:48:35 字数 1177 浏览 12 评论 0原文

我正在使用 jqtouch 构建一个 iphone 应用程序,并使用 cachemanifest 来缓存所有静态文件(图像、css、javascript)以使其加载速度更快。然而,该页面使用 php 来存储动态内容,我不想缓存它。所以我用这个php脚本(manifest.php)生成cachemanifest:

<?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";
?>

这实际上工作得很好,除了一件令人恼火的事情:

从我在某处读到的内容来看,调用cachemanifest的文件自动包含在清单中并且正在被缓存。这意味着我的起始页index.php(我称之为cachemanifest)正在被缓存。这会导致非常恼人的问题。

有什么办法可以解决这个问题或者有什么聪明的解决方法吗?该页面在缓存清单中列为“网络”,但看起来这被从文件调用缓存清单这一事实所推翻。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装不在乎 2024-09-04 13:48:35

futta 的想法是正确的,但您可能会发现您的首页只有一个部分经常更改。将其留空,然后让页面的其余部分被缓存,不用担心。当您访问该页面时,会立即调用缓存的版本,您可以运行脚本从服务器抓取动态页面片段并使用innerHTML对其进行设置以完成页面。效果是仍然有一个 HTTP 请求(加上一个清单请求),因此速度并不慢,而且您可以在下载动态部分时显示应用程序的一部分。如果您想刷新整个页面,请在清单中添加注释来标记版本,然后增加该注释以重新加载整个应用程序。

干净整洁。我认为这就是系统的使用方式,而不是试图避免一些 JavaScript,因为这毕竟是您可以离线使用并在离线时使用应用程序执行有用操作的唯一方法。

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.

噩梦成真你也成魔 2024-09-04 13:48:35

我有相同的经历,但我的待办事项列表中有以下可能的解决方法:

  • 创建一个包含所有静态资产的清单,
  • 仅在一个 html 页面 (buildCache.php) 中包含对该清单的引用,
  • 检查是否支持 window.applicationCache 以及如果是这样:
    • 每个会话重定向一次到cache.html以创建/检查/更新缓存
    • 让 buildCache.php 显示一些有关正在执行的操作的信息(使用 applicationCache 事件侦听器)
    • 将 buildCache.php 重定向回正常索引(未定义清单的位置)

我希望(有人在我的博客评论中声称这是这种情况),同一域上的所有页面都将使用 applicationCache 中的静态资源,即使并非所有页面都引用清单。

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.

决绝 2024-09-04 13:48:35

另一个解决方案是将你的index.php保留为空白加载页面或某种启动屏幕,然后将用户重定向到实际的动态php页面。由于清单位于index.php 中,并且index.php 重定向到real-index.php,因此问题可能不那么烦人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文