Application Cache主要功能就是當使用者第一次造訪時,將特定檔案先存在Client端,除了可以用來離線觀看,也可以加快網站的瀏覽速度。
而Application Cache的需要透過一個manifest檔案來紀錄你要cache哪些東西
範例:
index.php
1 | <!DOCTYPE HTML> |
2 | <!-- html元素要指定manifest --> |
3 | < html manifest = "test.manifest" > |
4 | < head > |
5 | < meta charset = "utf-8" /> |
6 | < title >TEST</ title > |
7 | < link type = "text/css" rel = "stylesheet" media = "screen" href = "test.css" /> |
8 | </ head > |
9 | < body > |
10 | < div id = "doc" > |
11 | < img alt = "pic" src = "test.jpg" /> |
12 | </ div > |
13 | < script src = "jquery.min.js" ></ script > |
14 | </ body > |
15 | </ html > |
裡面由test.css和jquery.min.js組成,所以我可以將這兩個檔和index.php都cache起來。
test.manifest
1 | CACHE MANIFEST |
2 | # 註解要單獨一行 |
3 | # CACHE MANIFEST 宣告要在第一行 |
4 |
5 | # 要Cache的檔案 |
6 | CACHE: |
7 | test .css |
8 | jquery.min.js |
9 | offline.php |
10 |
11 | # NETWORK是不會被快取的白名單檔案(這些檔案都會繞過cache並透過網路存取) |
12 | NETWORK: |
13 | test .jpg |
14 |
15 | # 當test.php無法存取時,則嘗試存取offline.php頁面 |
16 | FALLBACK: |
17 | test .php offline.php |