Application Cache API (二)

news/2024/6/3 4:23:17 标签: javascript

开这个博客的初衷主要是想沉淀一下我最近即将开展的对于HTML5及移动端性能方面的一些探索,而appcache我们更加关注的是缓存策略上对于性能的优化上的帮助,所以对于appcache的运行过程我们需要更加地清楚,昨天的Application Cache API (一)整体介绍了一下appcache,接下来会对appcache做一些黑盒测试,以便我们了解更多。

  这个demo中主要涉及到3类资源,两个页面,我们观察3类资源在不同的场景下浏览器的appcache策略。

 demo代码:

    test1.html如下:

复制代码
<html manifest="manifest.appcache">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
   <title>demo</title>
</head>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
   applicationCache.onchecking = function(){
   console.log("checking")
}
applicationCache.ondownloading = function(){
   console.log("dowload")
}
applicationCache.onnoupdate = function(){
   console.log("noupdate")
}
applicationCache.onprogress = function(){
   console.log("progress")
}
applicationCache.oncached = function(){
   console.log("cached")
}
applicationCache.onupdateready = function(){
    console.log("updateready")
}
applicationCache.onobsolete = function(){
   console.log("obsolete")
}
applicationCache.onerror = function(){
   console.log("error")
}
</script>
<link rel="stylesheet" type="text/css" href="css/index.css" media="all" />
<body>
  <div id="test">此处观察样式效果<div>
   <img src="img/index.jpg" />
   <img src="img/no-cache.jpg">
</body>
</html>
复制代码

  test2.html如下:

复制代码
<html
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
   <title>demo</title>
</head>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
   applicationCache.onchecking = function(){
   console.log("checking")
}
applicationCache.ondownloading = function(){
   console.log("dowload")
}
applicationCache.onnoupdate = function(){
   console.log("noupdate")
}
applicationCache.onprogress = function(){
   console.log("progress")
}
applicationCache.oncached = function(){
   console.log("cached")
}
applicationCache.onupdateready = function(){
    console.log("updateready")
}
applicationCache.onobsolete = function(){
   console.log("obsolete")
}
applicationCache.onerror = function(){
   console.log("error")
}
</script>
<link rel="stylesheet" type="text/css" href="css/index.css" media="all" />
<body>
  <div id="test">此处观察样式效果<div>
   <img src="img/index.jpg" />
   <img src="img/no-cache.jpg">
</body>
</html>
复制代码
  • 我们在两个页面中对于applicationCache对象的事件进行了监听,并将当前触发的事件名输出到console中,以便我们了解发生了什么
  • 两者区别在于,一个引用了manifest,一个没有,用于进行对比。
  • js和css初始化为空,用于观察效果

 

结论及场景

  1, application cache并不因服务器上资源改变而改变,只有manifest改变才会触发重新download,并且是所有cache文件均重新获取

    正常载入test1.html时,console输出如下:

checking        /html5/appcache/:13
noupdate        /html5/appcache/:37

          当js,css和图片发生变化时,载入test1.html ,console不变:

checking        /html5/appcache/:13
noupdate        /html5/appcache/:37

   当manifest文件进行修改后,console如下:

checking                 /html5/appcache/:13
dowload                  /html5/appcache/:27
5 progress                /html5/appcache/:49
updateready              /html5/appcache/:67

 

  2,对于另一个没有引用manifest文件的html,当它加载时,不会触发applicationCache的任何事件,但是会使用缓存。

  页面加载的时候。console输入为空

  修改服务器js,css等资源,页面中没有变化,修改manifest文件后,刷新页面,资源修改的效果出现。

  

  3,直接请求资源的绝对路径,只要该url被缓存过,那么所有的访问均是该资源的缓存,而与引用所在的宿主页面是否有manifest没有关系

  给js中写上alert("更新"),访问该资源的url,结果没有变化

  更新manifest之后,该js的访问得到更新

 

       4,js和css,图片文件的本身的访问,均会进行checking

  直接在地址栏输入一个缓存的js文件,console显示如下:

Document was loaded from Application Cache with manifest http://localhost/html5/appcache/manifest.appcache
Application Cache Checking event
Application Cache NoUpdate event

  

  修改manifest文件后,再次访问这个资源。显示如下:

复制代码
Document was loaded from Application Cache with manifest http://localhost/html5/appcache/manifest.appcache
Application Cache Checking event
Application Cache Downloading event
Application Cache Progress event (0 of 4) http://localhost/html5/appcache/css/index.css
Application Cache Progress event (1 of 4) http://localhost/html5/appcache/js/index.js
Application Cache Progress event (2 of 4) http://localhost/html5/appcache/
Application Cache Progress event (3 of 4) http://localhost/html5/appcache/img/index.jpg
Application Cache Progress event (4 of 4) 
Application Cache UpdateReady event
复制代码

     所有的资源,均被重新下载

 

  经过反复试验后,我们可以总结出以下浏览器在应用缓存方面处理url的逻辑策略:

  

 

转载于:https://www.cnblogs.com/zhepama/archive/2013/06/01/3112077.html


http://www.niftyadmin.cn/n/1096006.html

相关文章

三层架构与MVC区别

三层架构是界面层&#xff08;UI&#xff09;业务逻辑层&#xff08;BLL&#xff09;和数据访问层&#xff08;DAL&#xff09;构成的&#xff0c;而MVC是模型层&#xff08;M&#xff09;界面层&#xff08;View&#xff09;和控制层&#xff08;Controller&#xff09;构成的…

svn status

A新添加的文件 D将要删除的文件 M被修改过的文件 C本地文件和SVN服务器上文件有冲突 ?未纳入文件版本控制中 !项目缺失 L文件被锁住 ~原先是某种文件类型(文件&#xff0c;目录&#xff0c;link)&#xff0c;被另外一种类型所代替 http://svnbook.red-bean.com/en/1.5/svn.ref…

【leetcode】Remove Duplicates from Sorted List

题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 解题思路 # Definition for singly-linked list. # cla…

数据挖掘算法

KDD步骤&#xff1a; 数据清理 数据集成 数据选择 数据变换 数据挖掘 模式评估 知识表示数据挖掘概念&#xff1a; 从大量的、错综复杂的数据中挖掘哪些令人感兴趣的&#xff08;易被理解、新颖的、潜在有用的、非平凡的&#xff09;模式或知识构成数据挖掘算法的三要素&#x…

linux mkdir详解

linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名&#xff0c;要求创建目录的用户在当前目录中具有写权限&#xff0c;并且指定的目录名不能是当前目录中已有的目录语法: mkdir [-m] [-p] 目录名 选项介绍: -m: 对新建目录设置存取权限,也可以用chmod命令设置; -p…

android开发之AsyncTask的用法

在Android中实现异步任务机制有两种方式&#xff0c;Handler和AsyncTask。 Handler模式需要为每一个任务创建一个新的线程&#xff0c;任务完成后通过Handler实例向UI线程发送消息&#xff0c;完成界面的更新&#xff0c;这种方式对于整个过程的控制比较精细&#xff0c;但也是…

ubuntu 11.04安装ltib问题合集

一安装ltib已经将依赖软件安装担无法找到此安装文件 1. 依赖文件的问题。提示 缺少glibc和zlib。修改文件 ./bin/Ltibutils.pm在 app_checks 中修改&#xff1a;glibc-devel > sub { -f /usr/lib/libm.so || -f /usr/lib64/libz.so || -f /lib/i386-linux-gnu/libm.so.6}…

JS多个函数多个参数如何动态调用,apply的灵活应用

问题请看代码中的注解&#xff1a; Js代码 //回调函数1 function callback(a,b,c) { alert(abc); } //回调函数2 function callback2(a,b) { alert(ab); } //这个方法做了一些操作、然后调用回调函数 function doCallback(fn,args) { //do something …