Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Created February 1, 2012 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmm5t/1714753 to your computer and use it in GitHub Desktop.
Save rmm5t/1714753 to your computer and use it in GitHub Desktop.
Debugging HTML 5 Application Cache
# Application Cache Debugging and Handling
#
# Inspired by Jonathan Stark's Debugging HTML 5 Offline Application Cache
# http://jonathanstark.com/blog/2009/09/27/debugging-html-5-offline-application-cache/
cacheStatusValues = [
"uncached"
"idle"
"checking"
"downloading"
"updateready"
"obsolete"
]
if cache = window.applicationCache
logEvent = (e) ->
online = if (navigator.onLine) then "online" else "offline"
status = cacheStatusValues[cache.status]
type = e.type
message = "Application Cache #{type} event (#{online}, #{status})"
if (type == "error" && navigator.onLine)
message+= " (probably a syntax error in manifest)"
console.log(message)
swapCache = ->
try
cache.swapCache()
console.log("Swapped cache")
catch error
console.log("Couldn't swap cache", error)
checkCache = ->
try
cache.update()
catch error
console.log("Couldn't update cache", error)
checkCacheLater = ->
setTimeout checkCache, 300000
document.addEventListener "DOMContentLoaded", (->
cache.addEventListener "checking", logEvent, false
cache.addEventListener "downloading", logEvent, false
cache.addEventListener "progress", logEvent, false
cache.addEventListener "cached", logEvent, false
cache.addEventListener "updateready", logEvent, false
cache.addEventListener "noupdate", logEvent, false
cache.addEventListener "error", logEvent, false
cache.addEventListener "obsolete", logEvent, false
cache.addEventListener "updateready", swapCache, false
cache.addEventListener "cached", checkCacheLater, false
cache.addEventListener "noupdate", checkCacheLater, false
cache.addEventListener "updateready", checkCacheLater, false
cache.addEventListener "error", checkCacheLater, false
cache.addEventListener "obsolete", checkCacheLater, false
), false
# protection against accidental left-over console.log statements
window.console = { log: -> } unless window.console?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment