Table of Contents for
The Modern Web

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition The Modern Web by Peter Gasston Published by No Starch Press, 2013
  1. The Modern Web
  2. Cover
  3. The Modern Web
  4. Advance Praise for
  5. Praise for Peter Gasston’s
  6. Dedication
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. The Device Landscape
  12. The Multi-screen World
  13. Context: What We Don’t Know
  14. What You’ll Learn
  15. A. Further Reading
  16. 1. The Web Platform
  17. A Quick Note About Terminology
  18. Who You Are and What You Need to Know
  19. Getting Our Terms Straight
  20. The Real HTML5
  21. CSS3 and Beyond
  22. Browser Support
  23. Test and Test and Test Some More
  24. Summary
  25. B. Further Reading
  26. 2. Structure and Semantics
  27. New Elements in HTML5
  28. WAI-ARIA
  29. The Importance of Semantic Markup
  30. Microformats
  31. RDFa
  32. Microdata
  33. Data Attributes
  34. Web Components: The Future of Markup?
  35. Summary
  36. C. Further Reading
  37. 3. Device-Responsive CSS
  38. Media Queries
  39. Media Queries in JavaScript
  40. Adaptive vs. Responsive Web Design
  41. Viewport-Relative Length Units
  42. Responsive Design and Replaced Objects
  43. Summary
  44. D. Further Reading
  45. 4. New Approaches to CSS Layouts
  46. Multi-columns
  47. Flexbox
  48. Grid Layout
  49. The Further Future
  50. Summary
  51. E. Further Reading
  52. 5. Modern JavaScript
  53. New in JavaScript
  54. JavaScript Libraries
  55. Polyfills and Shims
  56. Testing and Debugging
  57. Summary
  58. F. Further Reading
  59. 6. Device Apis
  60. Geolocation
  61. Orientation
  62. Fullscreen
  63. Vibration
  64. Battery Status
  65. Network Information
  66. Camera and Microphone
  67. Web Storage
  68. Drag and Drop
  69. Interacting with Files
  70. Mozilla’s Firefox OS and WebAPIs
  71. PhoneGap and Native Wrappers
  72. Summary
  73. G. Further Reading
  74. 7. Images and Graphics
  75. Comparing Vectors and Bitmaps
  76. Scalable Vector Graphics
  77. The canvas Element
  78. When to Choose SVG or Canvas
  79. Summary
  80. H. Further Reading
  81. 8. New Forms
  82. New Input Types
  83. New Attributes
  84. Datalists
  85. On-Screen Controls and Widgets
  86. Displaying Information to the User
  87. Client-side Form Validation
  88. The Constraint Validation API
  89. Forms and CSS
  90. Summary
  91. I. Further Reading
  92. 9. Multimedia
  93. The Media Elements
  94. Media Fragments
  95. The Media API
  96. Media Events
  97. Advanced Media Interaction
  98. Summary
  99. J. Further Reading
  100. 10. Web Apps
  101. Web Apps
  102. Hybrid Apps
  103. TV Apps
  104. Webinos
  105. Application Cache
  106. Summary
  107. K. Further Reading
  108. 11. The Future
  109. Web Components
  110. The Future of CSS
  111. Summary
  112. L. Further Reading
  113. M. Browser Support as of March 2013
  114. The Browsers in Question
  115. Enabling Experimental Features
  116. Chapter 1: The Web Platform
  117. Chapter 2: Structure and Semantics
  118. Chapter 3: Device-Responsive CSS
  119. Chapter 4: New Approaches to CSS Layouts
  120. Chapter 5: Modern JavaScript
  121. Chapter 6: Device APIs
  122. Chapter 7: Images and Graphics
  123. Chapter 8: New Forms
  124. Chapter 9: Multimedia
  125. Chapter 10: Web Apps
  126. Chapter 11: The Future
  127. N. Further Reading
  128. Introduction
  129. Chapter 1: The Web Platform
  130. Chapter 2: Structure and Semantics
  131. Chapter 3: Device-Responsive CSS
  132. Chapter 4: New Approaches to CSS Layouts
  133. Chapter 5: Modern JavaScript
  134. Chapter 6: Device APIs
  135. Chapter 7: Images and Graphics
  136. Chapter 8: New Forms
  137. Chapter 9: Multimedia
  138. Chapter 10: Web Apps
  139. Chapter 11: The Future
  140. Index
  141. About the Author
  142. Copyright

Application Cache

Users have certain expectations of website-delivered apps, notable among them is that the apps should work offline or at least save data if the connection is lost. One way to provide offline assets is to save the data in local storage using the API I talked about in Web Storage, but a better approach might be to use the Application Cache, or AppCache. This is an interface that lists files that should be downloaded by the user agent and stored in the memory cache for use even when a network connection is unavailable.

The first step in making an AppCache is to create a manifest file, a text file with the suffix .appcache, which must be served with the text/cache-manifest MIME type. Link to this manifest using the manifest attribute of the html element on every page that you want to make available offline:

<html manifest="foo.appcache">

Some browsers alert the user that your application is asking to store files offline and request permission to do so. Figure 10-1 shows how this looks in Firefox on Android.

Some browsers, such as Firefox, request permission from the user before allowing AppCache to store files.
Figure 10-1. Some browsers, such as Firefox, request permission from the user before allowing AppCache to store files.

You need to consider a number of gotchas with the application cache if you’re thinking of using it on your site. These were exhaustively listed in Jake Archibald’s article “Application Cache is a Douchebag,” published on A List Apart (see Appendix K). I touch on a few of these in the following sections.

Contents of the AppCache File

The .appcache file begins with the words CACHE MANIFEST and then follows with a list of all files that should be cached. Each file is on a new line, and you can add single-line comments by entering a hash (#) at the start of each line.

The following listing shows a manifest file that stores three files in the cache. The comment with the version and date isn’t required but will come in handy later:

CACHE MANIFEST
# Version 0.1, 2013-04-01
index.html
foo.css
foo.js

You don’t need to explicitly list the pages to which the .appcache file is linked, because any page that includes the manifest attribute on the html element will be cached by default. These automatically cached pages are known as master entries, whereas files listed in the manifest are known as explicit entries. If some files require online access (such as access to a database using JavaScript), you can create a kind of whitelist of files to always be loaded over the network by listing them after the NETWORK: header. These files are known as network entries.

In the following example, files listed in the /dynamic folder will be loaded over the network instead of from the cache:

NETWORK:
/dynamic

You can also add fallback files in case an attempt to load a resource fails, owing to the loss of a network connection or something else. You do this below the FALLBACK: header, where each new line lists a file or folder with the fallback file after it, separated by a space.

In the following example, if any file from the /templates folder fails to load the page, fallback.html will be displayed from the cache instead:

FALLBACK:
/templates/ fallback.html

These files are known as fallback entries and, along with the three previous entries, complete the categories of file that will be cached.

The Caching Sequence

When the browser loads your page, it first checks for the manifest file. If the manifest exists and hasn’t been loaded before, the browser loads the page elements as usual and then fetches copies of any files that are in the manifest but haven’t yet been loaded. The browser stores all the listed entries in the cache to be loaded on the next visit.

If the manifest file exists and has been loaded before, the browser loads all the files held in the cache first, followed by any other files that are required to load the page. Next, it checks to see if the manifest file has been updated and, if so, downloads another copy of all the listed files. These files are then saved into the cache and used the next time the page is loaded; they won’t be presented to the user immediately.

One important peculiarity is that in order for the browser to check for new versions of files to be loaded, the manifest file itself must be changed. This is where the version number or timestamp comment comes in handy: Changing either (or both) tells the browser that the manifest has been updated, and it will then download the updated files.

The AppCache API

If you need to access the cache through JavaScript, you can use the window.applicationCache object. This object contains some properties and methods that you’ll find useful if you want to force downloads of updated files after the initial page load or otherwise interact with the manifest.

The status property returns the current status of the cache, with a numeric value and named constant for each state as follows:

  • 0 (UNCACHED) means that no cache is present.

  • 1 (IDLE) means that the cache is not being updated.

  • 2 (CHECKING) means that the browser is checking the manifest file for updates.

  • 3 (DOWNLOADING) means that new resources are being added.

  • 4 (UPDATEREADY) means that a new cache is available.

  • 5 (OBSOLETE) means that the current cache is now obsolete.

You can force a check of the manifest file using the update() method, which checks to see if the manifest file has been updated. The update() method gives the status property a value of 2 while it checks the manifest; it gives a value of 3 if updated resources exist and are downloading, and a value of 4 when updated files are ready.

When the status value is 4, you can use the swapCache() method to load the updated files. At this point, remember the browser has loaded the currently cached version of some files for the user in order to speed up the page load, and any updated files in the cache won’t be presented to the user until the page is reloaded. To get around this, you can use an AppCache event, which fires at various points of the cache cycle, most notably when the status property updates. For example, when the status value becomes 2, the checking event fires, and when it becomes 3, the downloading event fires.

The following code shows one approach to reloading a page with updated assets. Here, the updateready event fires when a new cache has been downloaded and then runs a function that double-checks that the status property has a value equivalent to UPDATEREADY (4). If so, updateready swaps the cache and then asks the user if it’s okay to upload the latest version of the files by reloading the page.

var myCache = window.applicationCache;
myCache.addEventListener('updateready', function(e) {
  if (myCache.status === myCache.UPDATEREADY) {
    myCache.swapCache();
    if (confirm('Load new version?')) {
      window.location.reload();
    }
  }
}, false);

By requesting permission to reload the page, this script ensures that the user doesn’t suddenly lose data or have an action interrupted by a forced reload. This approach is used by many popular web apps, including Google’s suite of tools.