簡體   English   中英

是否可以動態生成html5緩存清單?

[英]Is it possible to generate a html5 cache manifest dynamically?

是否可以動態生成html5緩存清單? 我已經嘗試過php(遵循本教程以及其他http://grinninggecko.com/dynamic-cache-manifest/ )但沒有成功。

很確定,但是讓我告訴你:讓HTML5離線內容與JavaScript applicationCache.update()等完美配合。 人。 如果你是新手,有點麻煩。 最終一切都有效......記錄在案! 但是CAVEAT LECTOR ......

無論如何,這是一個(希望)自我解釋的PHP專用示例,您需要一個.htaccess文件。 這將告訴您的服務器將cache.manifest解釋為PHP代碼(因為沒有.php擴展名,所以需要)。

您的.htaccss文件,以防您使用FCGI Wrapper:

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;

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM