Adobe Dreamweaver CS5.5 Studio Techniques: Making Your Site Available Offline
- How Offline Sites Work
- Making the Tozai Hotel Site Available Offline
- Going Offline
- You can't always get what you want,
But if you try sometimes,
You might get what you need. - —The Rolling Stones
Loss of signal is probably one of the most frustrating aspects of surfing the web with a mobile device. You've just clicked a link and the page is beginning to load when your train enters a tunnel. Your connection disappears. Even when the train emerges from the tunnel, your mobile has to hunt for a signal and you often need to start all over again.
HTML5 can't improve mobile connectivity, but it does make it possible to continue interacting with websites, even when no network connection is available. The secret lies in caching the necessary files. Although browsers automatically cache recently downloaded files, what's different about HTML5 is that you can instruct the browser to download files in advance of their being needed. You can also specify alternative files to be displayed if the user is offline.
In this chapter, you'll learn how to make a site available offline by creating a file that not only tells the browser which files to cache, but also specifies substitute files for offline use. To speed up this process, the download files for this chapter contain a Dreamweaver extension that I created to generate a list of all files used in a site or folder.
How Offline Sites Work
To make a site available without a network connection—an offline web application, as the HTML5 specification calls it—you need to create a manifest. This is a list of files that the browser needs to download and store in an application cache. The first time someone visits your site, the browser checks the manifest and downloads the listed files ready for use offline. The next time the same user visits your site, the browser checks the manifest. If it detects a change, all the files are downloaded again, updating the application cache.
Figure 4.1 shows which browsers support offline applications as reported by caniuse.com. Light green shows full support; darker green shows partial support; and pink indicates no support. Internet Explorer (IE) is the only mainstream browser with no support. Crucially, though, iOS Safari, Android, and Opera Mobile all support offline access, making it ideal for websites that you expect to be accessed on mobile devices.
Figure 4.1 Most modern browsers apart from IE support offline access.
Creating a Manifest
The manifest is a plain text file that must be saved with a .manifest filename extension. It's not important where you locate the manifest, but the most logical place is in the site root. However, if you want to make only part of a site available offline, the manifest should be located in the relevant folder and cover the files in all subfolders. The first line inside the manifest file should look like this:
CACHE MANIFEST
There should be only a single space between CACHE and MANIFEST, both of which should be in uppercase.
Following this is a list of files grouped according to how you want them to be treated when the user is offline:
- Explicit section. All files in this section are downloaded automatically, even if they're not required for the current page.
- Online whitelist section. Files in this section are never cached. The browser always tries to access the online version.
- Fallback section. This is where you specify substitute files that the browser should use when the user is offline.
The following basic rules apply to all sections:
- Each file must be listed on a separate line, except in the fallback section where the original and substitute files are listed on the same line with a space between them.
- Document-relative paths should be relative to the manifest.
- Paths relative to the site root (in other words, those that begin with a leading slash) or fully qualified URLs are also acceptable.
- The list should include not only web pages, but other assets, such as images, style sheets, and JavaScript files.
- Blank lines are permitted.
- Comments can be included, but they must be on a separate line beginning with a hash or pound sign (#) optionally preceded by spaces or tab characters.
Sections can be listed in any order and don't need to be a single block. For example, you might want to make some files available offline only for a limited period. So, it makes sense to list them separately from the core files that don't normally change.
You create sections by placing a section header on a separate line.
Specifying files that should be cached
The explicit section is the default, so files listed immediately after CACHE MANIFEST are automatically downloaded and cached. To switch back to the explicit section after the online whitelist or fallback section, place the following section header on a separate line:
CACHE:
Specifying files that must always be accessed online
Server-side scripts and other files that you don't want to be cached locally should be listed in the online whitelist section. You create this by adding the following header on a separate line:
NETWORK:
Then list the path or URL of each file on a separate line in the same way as for files that you want to be downloaded.
If your site accesses resources on other domains or subdomains, you should add an asterisk (*) on a line of its own in the online whitelist section like this:
NETWORK:
*
This indicates that access to resources on other domains is not blocked.
Specifying alternative files to use offline
To specify alternatives for files that can't be accessed offline, create a fallback section by placing the following section header on a separate line:
FALLBACK:
Each entry in the fallback section lists a file in the online site followed by the location of a substitute file to be used when offline. Both files are listed on the same line and separated by one or more spaces.
To represent any file, use a single forward slash (/) as the first part of the entry. For example:
FALLBACK:
/ offline.html
This substitutes offline.html for any file not listed elsewhere.
Keeping the cache up to date
More often than not, updates to a site involve changing the contents of a file without changing its name. This presents a problem for the application cache. The browser checks only the filenames in the manifest. If they're the same, it assumes the cache doesn't need updating.
To force the browser to update the cache, you need to change the contents of the manifest. The simplest way to do this is to add a comment with a version number like this:
CACHE MANIFEST
# version 4
Increment the version number each time you make changes to the site, and upload the revised manifest after all the changes have been uploaded. You don't need to use a version number. Any unique value—such as a timestamp—in a comment will do.
Serving the Manifest
You attach a manifest to a web page with the HTML5 manifest attribute in the opening <html> tag like this:
<html manifest="mysite.manifest"
>
The value of the manifest attribute should be a document-relative or site-root-relative path to the manifest file.
You should do this in every page in a site that you want to make available offline.
It's important to serve the manifest with the correct MIME type: text/cache-manifest.
Because this is a new MIME type, it might not be supported by all servers.
Setting the correct MIME type on Apache
If your web server runs on Apache, you should be able to configure it using an .htaccess file in your site root. If you already have an .htaccess file, add the following line to it:
AddType text/cache-manifest .manifest
If you don't have an .htaccess file, you can create one in Dreamweaver:
- Choose File > New.
- In the New Document dialog box, select Other from the list on the left, and set Page Type to Text. Click Create.
- Type the following line of code into the new document, paying careful attention to spelling (Apache directives are case-sensitive):
AddType text/cache-manifest .manifest
Save the file in your site root with the name .htaccess. The name begins with a dot. Although it's a text file, make sure it's not saved with a .txt filename extension.
On Windows, the file will be saved as normal.
On a Mac, you'll see a warning that files with names that begin with a dot are reserved for the system and will be hidden (Figure 4.2). Click Use ".". The file will be listed as normal in the Dreamweaver Files panel. However, you won't be able to see it in the Finder or any other Mac program unless it supports hidden files.
Figure 4.2 On a Mac, Dreamweaver warns you that names beginning with a dot have special status.
- Upload the .htaccess file to your website.
Setting the MIME type on other web servers
If your website is on a server other than Apache, you need to ask the server administrator to enable the text/cache-manifest MIME type.
Creating a "Lazy" Manifest
The HTML5 specification includes among its examples the following extremely simple manifest:
CACHE MANIFEST FALLBACK: / /offline.html NETWORK: *
Instead of downloading all pages immediately, the browser stores only the fallback page (offline.html) and pages that are visited while the user is online. When the user goes offline, cached pages are retrieved from the user's application cache. But if the user clicks a link to a page that hasn't previously been visited, offline.html is displayed instead.
This lazy way of caching can be very useful on a large site. However, you still need to update the manifest with a version number or other unique value each time a page is edited. Otherwise, the old version of the page remains in the application cache.
Only HTML pages can be linked to a manifest. So, other resources—such as style sheets and images—are not stored in the application cache unless they're listed in the explicit section of the manifest.