HTML5有一个相当诱人的特性,就是 Application Cache,即离线存储。
可以在没有网络的情况下,不会给用户一个“网络不可用”这种很不友好的用户提示。当然,更主要的是,我们希望可以在没有网络的状态下,依然带给用户较高的用户体验。
使用离线存储,除了需要在html tag标签增加<manifest>,列出需要缓存的清单,还必须在服务器端进行相应的配置。
由于开发中使用Ngnix服务器,这里我以Ngnix服务器配置为例。说明如果进行服务器的配置,支持HTML5 Application Cache特性。
1. 找到Ngnix服务器配置文件mime.types
root@mdev usr # cd /etc/httpd/ root@mdev httpd # ll total 28K 8.0K drwxr-xr-x 2 root root 4.0K Apr 10 2010 conf 8.0K drwxr-xr-x 2 root root 4.0K Apr 10 2010 conf.d 4.0K lrwxrwxrwx 1 root root 19 Apr 10 2010 logs -> ../../var/log/httpd 4.0K lrwxrwxrwx 1 root root 29 Apr 10 2010 modules -> ../../usr/lib64/httpd/modules 4.0K lrwxrwxrwx 1 root root 13 Apr 10 2010 run -> ../../var/run root@mdev httpd # cd conf root@mdev conf # ll total 60K 40K -rw-r--r-- 1 root root 33K Sep 4 2009 httpd.conf 20K -rw-r--r-- 1 root root 13K Sep 4 2009 magic root@mdev conf # cd .. root@mdev httpd # cd conf.d/ root@mdev conf.d # ll total 24K 8.0K -rw-r--r-- 1 root root 566 Sep 4 2009 proxy_ajp.conf 8.0K -rw-r--r-- 1 root root 392 Sep 4 2009 README 8.0K -rw-r--r-- 1 root root 299 Sep 4 2009 welcome.conf root@mdev conf.d # cd /usr/local/nginx/conf/
2. 编辑mime.types
root@mdev conf # vi mime.types
3. 添加manifest文件映射
types { text/html html htm shtml; text/css css; text/xml xml rss; image/gif gif; image/jpeg jpeg jpg; application/x-Javascript js; application/atom+xml atom; text/mathml mml; text/plain txt; text/vnd.sun.j2me.app-descriptor jad; text/vnd.wap.wml wml; text/x-component htc; image/png png; image/tiff tif tiff; image/vnd.wap.wbmp wbmp; image/x-icon ico; image/x-jng jng; image/x-ms-bmp bmp; image/svg+xml svg; application/java-archive jar war ear; application/mac-binhex40 hqx; application/msword doc; application/pdf pdf; application/postscript ps eps ai; application/rtf rtf; application/vnd.ms-excel xls; application/vnd.ms-powerpoint ppt; application/vnd.wap.wmlc wmlc; application/vnd.wap.xhtml+xml xhtml; application/x-cocoa cco; application/x-java-archive-diff jardiff; application/x-java-jnlp-file jnlp; application/x-makeself run; application/x-perl pl pm; application/x-pilot prc pdb; application/x-rar-compressed rar; application/x-RedHat-package-manager rpm; application/x-sea sea; application/x-shockwave-flash swf; application/x-stuffit sit; application/x-tcl tcl tk; application/x-x509-ca-cert der pem crt; application/x-xpinstall xpi; application/zip zip; application/octet-stream bin exe dll; application/octet-stream deb; application/octet-stream dmg; application/octet-stream eot; application/octet-stream iso img; application/octet-stream msi msp msm; audio/midi mid midi kar; audio/mpeg mp3; audio/x-realaudio ra; video/3gpp 3gpp 3gp; video/mpeg mpeg mpg; video/quicktime mov; video/x-flv flv; video/x-mng mng; video/x-ms-asf asx asf; video/x-ms-wmv wmv; video/x-msvideo avi; application/x-nokia-widget wgz; text/cache-manifest mf manifest }
如果有过IIS配置经验的,这里的mime.types 和IIS的文件名映射原理是一样的,即对于文件后缀名是mf 或者 manifest 采用 text/cache-manifest 方式解析。
4. 重启 Ngnix 服务器: