Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
143023864X_HT5.pdf
Скачиваний:
8
Добавлен:
21.02.2016
Размер:
7.98 Mб
Скачать

CHAPTER 12 CREATING OFFLINE WEB APPLICATIONS

Using Application Cache to Boost Performance

Peter says: “One nice side-effect of the application cache mechanism is that you can use it to prefetch resources. The regular browser cache stores pages that you have visited, but what is stored is dependent on both client and server configuration (browser settings and expires headers). Therefore, returning to specific pages by relying on regular browser caching is fickle to say the least—anyone who has ever attempted to rely on regular browser caching to navigate through the pages of a website while on an airplane will probably agree here.

Using application cache, however, allows you not only to cache pages as you visit them but also to cache pages you have not even visited yet; it can be used as an effective prefetching mechanism. When it is time to use one of those prefetched resources it will be loaded from the application cache on local disk and not from the server, speeding up the load time dramatically. Used wisely (don’t prefetch Wikipedia), you can use application cache to dramatically improve performance. One important thing to remember is that regular browser caching is also still in effect, so watch for false positives, especially if you are trying to debug application cache behavior.”

Building an Application with HTML5 Offline Web Applications

In this example application, we will track a runner’s location while out on the trail (with intermittent or no connectivity). For example, Peter goes running, and he will have his new Geolocation–enabled phone and HTML5 web browser with him, but there is not always a great signal out in the woods around his house. He wants to use this application to track and record his location even when he cannot use the Internet.

When offline, the Geolocation API should continue to work on devices with hardware geolocation (such as GPS) but obviously not on devices that use IP geolocation. IP geolocation requires network connectivity to map the client's IP address to coordinates. In addition, offline applications can always access persistent storage on the local machine through APIs such as local storage or Indexed Database.

The example files for this application are located on the book’s page at www.apress.com and at the book‘s website in the offline code folder, and you can start the demo by navigating to the code/offline folder and issuing the command:

Python –m SimpleHTTPServer 9999.

Prior to starting the web server, make sure you have configured Python to serve the manifest files (files with the *.appcache extension) with the correct mime type as described earlier. This is the most common cause of failure for offline web applications. If it does not work as expected, check the console in Chrome Developer tools for possible descriptive error messages.

This starts Python’s HTTP server module on port 9999 (you can start it on any port, but you may need admin privileges to bind to ports lower than 1024. After starting the HTTP server, you can navigate to http://localhost :9999/tracker.html to see the application in action.

Figure 12-3 shows what happens in Firefox when you access the site for the first time: you are prompted to opt in to storing data on your computer (note, however, that not all browsers will prompt you before storing data).

303

CHAPTER 12 CREATING OFFLINE WEB APPLICATIONS

Figure 12-3. Firefox prompting to store data for the web application

After allowing the application to store data, the application cache process starts and the browser starts downloading the files referenced in the application cache manifest file (this happens after the page is loaded, and, therefore, it has minimal impact on the responsiveness of the page. Figure 12-4 shows how Chrome Developer Tools provide a detailed overview of what is cached for the localhost origin in the Resources pane. It also provides information in the console about the application cache events that fire while the page and the manifest were processed.

304

CHAPTER 12 CREATING OFFLINE WEB APPLICATIONS

Figure 12-4. The Offline Page in Chrome with details about the application cache in Chrome Developer Tools

To run this application, you will need a web server serving these static resources. Remember that the manifest file must be served with the content type text/cache-manifest. If your browser supports the application cache, but the file is served with the incorrect content type, you will receive a cache error. An easy way to test this is to view the events that fire in the Chrome Developer Tools console as shown in Figure 12-4; it will tell you if the appcache file is served with the wrong mime type.

To run this application with complete functionality, you will need a server that can receive geolocation data. The server-side complement to this example would presumably store, analyze, and make available this data. It may or may not be served from the same origin as the static application.

Figure 12-5 shows the example application running in offline mode in Firefox. You can use File Work Offline to turn this mode on in Firefox and Opera. Other browsers do not have this convenience function, but you can disconnect from the network. Note, however, that disconnecting from the network does not interrupt the connection to a Python server running on localhost.

305

CHAPTER 12 CREATING OFFLINE WEB APPLICATIONS

Figure 12-5. The application in offline mode

Creating a Manifest File for the Application Resources

First, in a text editor, create the tracker.appcache file as follows. This manifest file will list the files that are part of this application:

CACHE MANIFEST

#JavaScript

./offline.js

#./tracker.js

./log.js

#stylesheets

./html5.css

#images

Creating the HTML Structure and CSS for the UI

This is the basic UI structure of the example. Both tracker.html and html5.css will be cached, so the application will be served from the application cache.

<!DOCTYPE html>

<html lang="en" manifest="tracker.appcache"> <head>

<title>HTML5 Offline Application</title> <script src="log.js"></script>

<script src="offline.js"></script> <script src="tracker.js"></script>

306