這是預期的瀏覽器行爲?
這確實是有意的行爲。見http://diveintohtml5.info/offline.html#debugging
if even a single resource listed in your cache manifest file fails to download properly, the entire process of caching your offline web application will fail. Your browser will fire the error event, but there is no indication of what the actual problem was.
一個解決方案,我能想到的將是檢查beforeunload
如果該window.applicationCache.status
或者是checking
或downloading
。
或者您可能能夠在用戶localStorage中設置一個標誌,指示最後一次嘗試不成功,使用error
事件(請參見下文)並嘗試重新提取文件,直到所有內容都成功加載。
如果你有很多東西要緩存,你可以顯示一個進度條和一些文本,要求用戶在頁面加載時耐心等待。對於進度條,您可以在緩存處理函數的進度事件中使用event.loaded
和event.total
。
var appCache = window.applicationCache;
// Fired after the first cache of the manifest.
appCache.addEventListener('cached', handleCacheEvent, false);
// Checking for an update. Always the first event fired in the sequence.
appCache.addEventListener('checking', handleCacheEvent, false);
// An update was found. The browser is fetching resources.
appCache.addEventListener('downloading', handleCacheEvent, false);
// The manifest returns 404 or 410, the download failed,
// or the manifest changed while the download was in progress.
appCache.addEventListener('error', handleCacheError, false);
// Fired after the first download of the manifest.
appCache.addEventListener('noupdate', handleCacheEvent, false);
// Fired if the manifest file returns a 404 or 410.
// This results in the application cache being deleted.
appCache.addEventListener('obsolete', handleCacheEvent, false);
// Fired for each resource listed in the manifest as it is being fetched.
appCache.addEventListener('progress', handleCacheEvent, false);
// Fired when the manifest resources have been newly redownloaded.
appCache.addEventListener('updateready', handleCacheEvent, false);
function handleCacheEvent(e){
if(e.type && (e.type=='progress' || e.type=='ProgressEvent')){
console.log('percent:', Math.round(e.loaded/e.total*100)+'%', 'total:', e.total, 'loaded:',e.loaded);
}
}
AppCache更新是原子的,要麼整個事情被緩存或沒有被緩存。 – robertc 2012-07-16 21:18:58
從我在平板電腦上看到的一部分資產可以使用dl'd,但如果它們不是全部都是dl'd,則脫機功能無法工作。我能夠確認,通過導航離開登錄頁面,然後查看網站設置來查看我的應用程序正在使用的存儲量。 似乎沒有暴露的API會報告何時中斷/不完整的資產發生。我可以確保離線功能的唯一方法是阻止用戶從登錄頁面導航,直到緩存事件被觸發。 我很樂意接受。 – 2012-07-17 00:08:44