• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

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.

Notes

Firefox alerts users that the site is asking to store data on your computer for offline use and offers the option to decline. Most other browsers download the files without asking.


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.

Notes

Section headers must be written in uppercase and are followed by a colon. Headers can be preceded by spaces, but there should be nothing else on the same 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:

Notes

Technically speaking, you can use the CACHE: section header immediately after CACHE MANIFEST, but it’s unnecessary.


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.

Close Up: Browser Caches

Application caches are designed to make the website—or parts of it—available offline. They’re separate from the normal browser cache, which speeds up the rendering of pages by avoiding the need to download files that haven’t changed. When the normal cache reaches capacity, older files are deleted to make way for newer ones. The location of both types of cache is dependent on the browser.

The HTML5 specification doesn’t prescribe any limit for the amount of disk space used by an application cache. The specification is equally vague about allowing users to delete specific application caches. Web developers should exercise their judgment about which files to make available offline and not fill up users’ disk space unnecessarily.


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:

1.
Choose File > New.

2.
In the New Document dialog box, select Other from the list on the left, and set Page Type to Text. Click Create.

3.
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

4.
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.


5.
Upload the .htaccess file to your website.

Close Up: .htaccess

An .htaccess file is a mini-configuration file for the Apache web server. It has the advantage that all the settings are applied immediately without the need to restart the server. Normally, an .htaccess file is located in the site root and applies to the whole site. However, you can apply different settings to individual folders (directories) by placing an .htaccess file in the folder you want to control (the same settings apply to all subfolders unless overridden by another .htaccess file).

Most hosting companies configure their servers to allow site owners to fine-tune their settings with .htaccess. However, if you don’t have permission to use .htaccess, you need to ask the server administrator to enable the text/cache-manifest MIME type.


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.

Notes

As long as they’re attached to a manifest, visited pages are stored in the application cache because a page that links to the manifest is automatically included in the explicit section. However, it’s generally recommended that you list files individually rather than relying on this default behavior.


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.

Notes

Web pages that use a server-side technology, such as PHP, ColdFusion, or ASP.NET, can also be linked to a manifest. However, the offline version stored by the application cache contains only the HTML output. For example, if the dynamic code outputs the current date, the version stored in the application cache displays the date when the online version was most recently accessed. As soon as you go back online, the stored date is updated.


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint