【问题标题】:Is it possible to generate a html5 cache manifest dynamically?是否可以动态生成 html5 缓存清单?
【发布时间】:2014-01-19 06:12:09
【问题描述】:

是否可以动态生成 html5 缓存清单?我已经尝试过 php(按照本教程以及其他 http://grinninggecko.com/dynamic-cache-manifest/ 进行操作),但完全没有成功。

【问题讨论】:

  • 是的。只需确保格式和响应标头正确。您具体遇到了什么问题?
  • 请不要为同一个问题创建多个问题;相反,请使用任何新信息更新您的原始问题。
  • 好吧,抱歉,我只是觉得我的第一个问题太狭窄而无法引起注意,而且这种思路似乎奏效了,因为至少你在这里停下来处理这个标题更广泛的新问题 =p 我'会尽量保持谦虚。

标签: php html dynamic cache-manifest


【解决方案1】:

非常肯定,但让我告诉你:让 HTML5 离线内容与 JavaScript applicationCache.update() 等很好地协同工作。人。如果您是新手,那会有些混乱。最后,一切都按照……记录在案!但是警告讲师...

无论如何,这是一个(希望如此)自我解释的仅限 PHP 的示例,您需要一个 .htaccess 文件。这将告诉您的服务器将 cache.manifest 解释为 PHP 代码(需要,因为没有 .php 扩展名)。

如果您使用 FCGI Wrapper,您的 .htaccss 文件:

AddType text/cache-manifest .manifest

<FilesMatch "\.(manifest)$">
  SetHandler fcgid-script
  FcgidWrapper /folder/to/your/php-fcgi-starter .manifest
  Options +ExecCGI
</FilesMatch>

您的.htaccess 文件,以防您使用 apache php 模块(大多数情况下,这将是默认情况):

AddType text/cache-manifest .manifest

<FilesMatch "\.(manifest)$">
    SetHandler application/x-httpd-php
</FilesMatch>

您的cache.manifest 文件:

<?php

// only cache files in the following folders (avoids other stuff like "app/")
$folders = array('js', 'lib', 'views', 'styles');
$files = array('index.html');

// recursive function
function append_filelist(&$files, $folder) {
  if ($dh = opendir($folder)) {
    while (($file = readdir($dh)) !== false) {
      if ( ! in_array($file, array('.', '..', '.svn')) &&
             (substr($file, -4) != ".swp")) {
        if (is_dir($folder."/".$file))
          append_filelist($files, $folder."/".$file);
        else
          //$files[] = $folder."/".$file."?hash=".md5_file($folder."/".$file);
          $files[] = $folder."/".$file;
      } // if
    } // while
  } // if
}

// init
foreach ($folders as $folder)
  if (is_dir($folder))
    append_filelist($files, $folder);

// generate output
$body = "CACHE MANIFEST\n\nCACHE:\n";
foreach ($files as $file)
  $body .= $file."\n";
$body .= "\nNETWORK:\n*\n";

// render output (the 'Content-length' header avoids the automatic creation of a 'Transfer-Encoding: chunked' header)
header('Content-type: text/cache-manifest');
header('Content-length: '.strlen($body));
echo $body;

祝你好运!

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多