30
HTML5 Offline Web Application [email protected]

HTML5 Offline Web Application

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: HTML5 Offline Web Application

HTML5 Offline Web Application

[email protected]

Page 2: HTML5 Offline Web Application

Agenda

Client-side Caching HTTP Response Header

Expires Header Cache-Control Header ETag Header Last-Modified Header

Offline Web Application Cache Manifest Application Cache

Page 3: HTML5 Offline Web Application

Client-side Caching

Page 4: HTML5 Offline Web Application

HTTP Response Header

Expires Header Gives the date/time after which the response is

considered stale Example

Expires: Thu, 01 Dec 1994 16:00:00 GMT

Page 5: HTML5 Offline Web Application

HTTP Response Header

Cache-Control Header Tells all caching mechanisms from server to client

whether they may cache this object. It is measured in seconds

Example Cache-Control: max-age=3600

Page 6: HTML5 Offline Web Application

HTTP Response Header

Entity Tag / ETag Header An identifier for a specific version of a resource, of

ten a message digest Example

ETag: "737060cd8c284d8af7ad3082f209582d"

Page 7: HTML5 Offline Web Application

HTTP Response Header

Last-Modified Header The last modified date for the requested object Example

Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT

Most modern Web servers will generate both ETag and Last-Modified headers to use as validators for static content automatically

Page 8: HTML5 Offline Web Application

Offline Web Application

Page 9: HTML5 Offline Web Application

Before HTML5…

Browser's cache based on server-side rules and client-side configuration

Regular caching can lead to undesired results while you're offline

Page 10: HTML5 Offline Web Application

HTML5 Offline Feature

Create a manifest file that lists your app's assets and reference it in a manifest attribute in your web pages' html elements

You add the manifest attribute to the <html> element, and point it to the file containing your application’s manifest

Page 11: HTML5 Offline Web Application

Cache Manifest

Manifest will cache HTTP GET requests, while POST, PUT, and DELETE will still go to the network If the page has an active manifest file, all GET req

uests will be directed to local storage Web server is sending the text/cache-manifes

t file header when you request any file with the *.manifest / *.appcache extension

Page 12: HTML5 Offline Web Application

Manifest Structure

The first line of the file must always be CACHE MANIFEST

Comments start with # and must appear on a line of their own

Case-sensitive file names must be listed exactly as they appear on disk

3 directive sections CACHE:CACHE: section NETWORK:NETWORK: section FALLBACK:FALLBACK: section

Page 13: HTML5 Offline Web Application

Manifest Structure

CACHE:CACHE: section The files will be cached in Application Cache Add a list of cached files under the CACHE

MANIFEST directive without the CACHE:CACHE: section Add a list of cached files anywhere else in the file,

you need to place them under an explicit CACHE:CACHE: section

Page 14: HTML5 Offline Web Application

Manifest Structure

CACHE:CACHE: section rules Only one file name per line A full file name is required, no wildcards * allowed File names can contain path information or even

an absolute URL File names can't include fragment identifiers, e.g.

comment #

Page 15: HTML5 Offline Web Application

Manifest Structure

NETWORK:NETWORK: section Any URLs will bypass the application cache and

load directly from the server Explicitly state that anything that isn’t cached

must go via the web by using a wildcard * (note that this is the default behavior if omitted)

Page 16: HTML5 Offline Web Application

Manifest Structure

FALLBACK:FALLBACK: section Specify a fallback resource that must be served if

a specific resource is not available (either because you’re offline or it’s not in the cache)

List entire directories or URL paths in the NETWORK:NETWORK: and FALLBACK:FALLBACK: sections, not full files name

Page 17: HTML5 Offline Web Application

Manifest Structure

Page 18: HTML5 Offline Web Application

Manifest Structure

Page 19: HTML5 Offline Web Application

Update Cache

Once an application is offline it remains cached until … User clean their browser's data storage for your

site Manifest file is updated

Updating a file listed in the manifest doesn't mean the browser will re-cache that resource

Application cache is programmatically updated

Page 20: HTML5 Offline Web Application

Update Manifest

Application Cache Busting Add a version number (from a version control

system) or timestamp string in a comment # in the manifest file

Add an final MD5 checksum into the manifest file Add each file’s MD5 checksum as a comment

after the file name

Page 21: HTML5 Offline Web Application

Update Manifest

Only when the web page is reloaded will the new cached assets come into play and become available

If you can’t wait for the next page reload, you can programmatically add an event listener for the updateReady event and prompt the user to reload the page

Page 22: HTML5 Offline Web Application

Update Manifest via JavaScript

Page 23: HTML5 Offline Web Application

Checking Status

Checking for Browser Support Compatibility Tables if (if (window.applicationCachewindow.applicationCache) {) {

// this browser supports offline web apps // this browser supports offline web apps}}

Checking Online / Offline Check the property navigator.onlinenavigator.online whether the b

rowser is online or not Pull in a piece of JavaScript and check online / off

line status via FALLBACK rule

Page 24: HTML5 Offline Web Application

Checking Online / Offline

Page 25: HTML5 Offline Web Application

Checking Online / Offline

Page 26: HTML5 Offline Web Application

Application Cache API

Page 27: HTML5 Offline Web Application

Application Cache API

Page 28: HTML5 Offline Web Application

Clean Cache

Firefox Tools > Clear Recent History Tools > Options (Preferences on Mac OS X) >

Advanced > Network > Select specific application cache > Remove

Chrome Settings Menu > Tools > Clear Browsing Data

Safari Settings Menu > Reset Safari

Page 29: HTML5 Offline Web Application

Debug Manifest Challenge

Include missing files in the manifest When the manifest is updated and the brows

er does not reflect the update until the web page is loaded

If the manifest has a cache control header set on it, the browser may not check for a new version of the manifest Manifest is not cached by the browser, or if it is ca

ched it is done only via an ETag.

Page 30: HTML5 Offline Web Application

Conclusion

Tips Turn off the manifest file during development and enable it

only when the project is ready to go live Access Application Cache Content

Visit about:cache in Firefox Security

Private Browsing mode Disk Quota

Limit it to about 5 MB per domain Demo

Q&A