HTML 5 오프라인 캐 시

참고 문서:http://www.w3cschool.cc/html/html5-app-cache.html
HTML5 응용 프로그램 캐 시 는 오프라인 캐 시 라 고도 부 릅 니 다. 끊 겨 도 새로 고침 후에 원래 페이지 를 캐 시 합 니 다. 404 가 되 지 않 습 니 다.
우선, 문서 의 < html > 탭 에 manifest 속성 을 포함 하 십시오:
<!DOCTYPE HTML>
<html manifest="page.appcache"> <!--   page.appcache     ,      -->
...
</html>

Apache 나. htaccess 파일 에 추가 하기
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond $1 !^(index\.php|robots\.txt|views|plugins|backup|upload|image|runtime|install)
    RewriteRule ^(.*)$ index.php/$1 [L]
#     
AddType text/cache-manifest manifest   
 </IfModule>

다음은. appcache 파일 생 성 입 니 다.격식
CACHE MANIFEST
# 2012-02-21 v1.0.0
/theme.css
/logo.gif
/main.js

NETWORK:
login.php

FALLBACK:
/html/ /offline.html

그러나 한 항목 에 이 정도 의 파일 만 있 는 것 이 아니 라 더 많은 자원 파일 을...css. js. img 등 을 불 러 오기 때문에 디 렉 터 리 를 옮 겨 다 니 는 방법 이 필요 합 니 다.다음은 내 가 고 쳐 쓴 방법 이다.
<?php

/**
 *          
 * @param string $folder   
 * @param int $levels         
 * @return array  
 * @author lixianghui
 */
function list_files($folder = '', $levels = 100) { if (empty($folder) || !$levels) { return false; } $files = array(); //     if ($dir = @opendir($folder)) { //       while (($file = readdir($dir) ) !== false) { //       if (in_array($file, array('.', '..'))) continue; //     if (preg_match("/[\x7f-\xff]/", $file)) continue; //       if (is_dir($folder . '/' . $file)) { //       $files2 = list_files($folder . '/' . $file, $levels - 1); //           if ($files2) $files = array_merge($files, $files2); else $files[] = $folder . '/' . $file . '/'; } else { //   //        if (in_array(pathinfo($file)['extension'], array('css', 'js', 'png', 'jpg', 'gif'))) { $files[] = $folder . '/' . $file; } } } } @closedir($dir); return $files; } /** *      * @return void * @author lixianghui */ function offline_cache(){ $list = array(); $file_str = "";
   // $dir_upload = list_files('upload'); $dir_default = list_files('views/default'); $file_array=array_merge($dir_upload,$dir_default); foreach ($file_array as $val) { $file_str.=$val . "
"; }   // appcache $cache_str = "CACHE MANIFEST
"; $cache_str.="#" . date("Y-m-d H:i:s") . "
"; $cache_str.=$file_str; $cache_str.="NETWORK:


"; $cache_str.="FALLBACK:
nomore.html"; file_put_contents("page.appcache", $cache_str); }

자, 이제 콘 솔 테스트 를 열 면 캐 시 자원 이 모두 불 러 오 는 것 을 볼 수 있 습 니 다.저장 이 나타 나 면 자원 이 저장 되 었 는 지, 존재 하 는 지 확인 하 십시오.

좋은 웹페이지 즐겨찾기