SlideShare a Scribd company logo
Offline Application Cache
        Jonathan Stark




              1
What is AppCache?



        2
The Basics



    3
Manifest File

• A simple text document
• Hosted on your web server
• Contains a list of static resources

                      4
demo.manifest
CACHE MANIFEST

index.html

logo.jpg

scripts/demo.js

styles/screen.css



                  5
index.html

<html manifest="demo.manifest">




               6
.htaccess

AddType text/cache-manifest .manifest




                 7
It Works!

• User loads index.html normally
• Listed files download in the background
• User can now go offline

                    8
What About Updates?

• Update your content (HTML, CSS, JS, IMG)
• Update your manifest
• All files re-download next time

                    9
Online Whitelist



       10
demo.manifest
CACHE MANIFEST

index.html

scripts/demo.js

styles/screen.css

NETWORK:

logo.jpg


                  11
Offline Fallbacks



       12
demo.manifest
CACHE MANIFEST

index.html

scripts/demo.js

styles/screen.css

FALLBACK:

logo.jpg offline.jpg


                  13
demo.manifest
CACHE MANIFEST

index.html

scripts/demo.js

styles/screen.css

FALLBACK:

images/ images/offline.jpg


                  14
Dynamic Manifest



       15
manifest.php
<?php
echo "CACHE MANIFESTn";
$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file) {
    if ($file->IsFile()
    and $file != "./manifest.php"
    and substr($file->getFilename(), 0, 1) != ".") {
        echo $file . "n";
    }
}




                         16
Debugging



    17
access_log
tail -f /var/log/apache2/access_log




                18
JavaScript API
// Convenience array of status values
var cacheStatusValues = [];
cacheStatusValues[0] = 'uncached';
cacheStatusValues[1] = 'idle';
cacheStatusValues[2] = 'checking';
cacheStatusValues[3] = 'downloading';
cacheStatusValues[4] = 'updateready';
cacheStatusValues[5] = 'obsolete';




                              19
JavaScript API
// Listeners for all possible events
var cache = window.applicationCache;
cache.addEventListener('cached', logEvent, false);
cache.addEventListener('checking', logEvent, false);
cache.addEventListener('downloading', logEvent, false);
cache.addEventListener('error', logEvent, false);
cache.addEventListener('noupdate', logEvent, false);
cache.addEventListener('obsolete', logEvent, false);
cache.addEventListener('progress', logEvent, false);
cache.addEventListener('updateready', logEvent, false);




                             20
JavaScript API
// Log every event to the console
function logEvent(e) {
    var online, status, type, message;
    online = (navigator.onLine) ? 'yes' : 'no';
    status = cacheStatusValues[cache.status];
    type = e.type;
    message = 'online: ' + online;
    message+= ', event: ' + type;
    message+= ', status: ' + status;
    if (type == 'error' && navigator.onLine) {
        message+= ' (prolly a syntax error in manifest)';
    }
    console.log(message);
}
                             21
JavaScript API
// Check for manifest changes every 10 seconds
setInterval(function(){cache.update()}, 10000);




                             22
Demo!



  23
More Info

• http://jonathanstark.com/books
• http://jonathanstark.com/contact
• http://jonathanstark.com/e4h

                     24

More Related Content

What's hot (20)

PPTX
Learning Svelte
Christoffer Noring
 
PDF
The road to continuous deployment: a case study (DPC16)
Michiel Rook
 
PDF
Learning django step 1
永昇 陳
 
KEY
Dancing with websocket
Damien Krotkine
 
KEY
Lazy Loading Because I'm Lazy
Johnathan Leppert
 
PDF
Javascript Frameworks for Joomla
Luke Summerfield
 
PDF
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
PDF
Phinx talk
Michael Peacock
 
PDF
"Service Worker: Let Your Web App Feel Like a Native "
FDConf
 
PDF
Better Testing With PHP Unit
sitecrafting
 
PDF
Kyiv.py #17 Flask talk
Alexey Popravka
 
PPT
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
Srijan Technologies
 
PDF
Puppet Camp 2012
Server Density
 
PPTX
Qualidade de vida: Com Zabbix e API
Luiz Sales
 
PDF
Security Challenges in Node.js
Websecurify
 
PDF
Laravel Design Patterns
Bobby Bouwmann
 
PDF
Scalable web application architecture
postrational
 
PDF
XamarinとAWSをつないでみた話
Takehito Tanabe
 
Learning Svelte
Christoffer Noring
 
The road to continuous deployment: a case study (DPC16)
Michiel Rook
 
Learning django step 1
永昇 陳
 
Dancing with websocket
Damien Krotkine
 
Lazy Loading Because I'm Lazy
Johnathan Leppert
 
Javascript Frameworks for Joomla
Luke Summerfield
 
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
Phinx talk
Michael Peacock
 
"Service Worker: Let Your Web App Feel Like a Native "
FDConf
 
Better Testing With PHP Unit
sitecrafting
 
Kyiv.py #17 Flask talk
Alexey Popravka
 
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
Srijan Technologies
 
Puppet Camp 2012
Server Density
 
Qualidade de vida: Com Zabbix e API
Luiz Sales
 
Security Challenges in Node.js
Websecurify
 
Laravel Design Patterns
Bobby Bouwmann
 
Scalable web application architecture
postrational
 
XamarinとAWSをつないでみた話
Takehito Tanabe
 

Viewers also liked (8)

PDF
Implementing Data Caching and Data Synching Using Oracle MAF
Steven Davelaar
 
PPTX
Offnet- Offline Mobile Application
Sudip Adhikari
 
PDF
Offline Strategy for an Online World
SoCal UX Camp
 
PDF
Designing For Offline
Steven Trevathan
 
ODP
Offline First Applications
GeekNightHyderabad
 
PPTX
Offline first: application data and synchronization
EatDog
 
PDF
Sync is hard: building offline-first Android apps from the ground up
droidcon Dubai
 
PDF
Data Synchronization Patterns in Mobile Application Design
Eric Maxwell
 
Implementing Data Caching and Data Synching Using Oracle MAF
Steven Davelaar
 
Offnet- Offline Mobile Application
Sudip Adhikari
 
Offline Strategy for an Online World
SoCal UX Camp
 
Designing For Offline
Steven Trevathan
 
Offline First Applications
GeekNightHyderabad
 
Offline first: application data and synchronization
EatDog
 
Sync is hard: building offline-first Android apps from the ground up
droidcon Dubai
 
Data Synchronization Patterns in Mobile Application Design
Eric Maxwell
 
Ad

Similar to Offline Application Cache (20)

PDF
Using html5 to build offline applications
Woody Pewitt
 
PDF
Rails for Mobile Devices @ Conferencia Rails 2011
Alberto Perdomo
 
PPT
HTML5 Offline Web Application
Allan Huang
 
PPTX
Html5 Offline Applications
Sunny Sharma
 
PPTX
Html5 offline
Wei-Hsiung Chen
 
PPTX
Offline Storage
SP Balamurugan
 
PDF
06 HTML5 Mobile
Ynon Perek
 
PPTX
The Power of HTML5 Offline: Mobile and More!
FITC
 
PDF
Aleksey Bogachuk - "Offline Second"
IT Event
 
PPTX
Peter lubbers-html5-offline-web-apps
Skills Matter
 
PDF
Building great mobile apps: Somethings you might want to know
shwetank
 
PDF
HTML5: huh, what is it good for?
Remy Sharp
 
PPTX
Taking Web Applications Offline
Matt Casto
 
PPT
HTML 5 Offline Web apps
Alexandre Marreiros
 
PDF
Web fundamentals
Nguyen Van Vuong
 
PDF
Service workers
Pavel Zhytko
 
PDF
Webapps without the web
Remy Sharp
 
PDF
HTML5 Offline Web Applications (Silicon Valley User Group)
robinzimmermann
 
PPTX
Take your drupal sites offline
Chris Ward
 
PDF
Progressive Web Apps - deep dive
Kenneth Rohde Christiansen
 
Using html5 to build offline applications
Woody Pewitt
 
Rails for Mobile Devices @ Conferencia Rails 2011
Alberto Perdomo
 
HTML5 Offline Web Application
Allan Huang
 
Html5 Offline Applications
Sunny Sharma
 
Html5 offline
Wei-Hsiung Chen
 
Offline Storage
SP Balamurugan
 
06 HTML5 Mobile
Ynon Perek
 
The Power of HTML5 Offline: Mobile and More!
FITC
 
Aleksey Bogachuk - "Offline Second"
IT Event
 
Peter lubbers-html5-offline-web-apps
Skills Matter
 
Building great mobile apps: Somethings you might want to know
shwetank
 
HTML5: huh, what is it good for?
Remy Sharp
 
Taking Web Applications Offline
Matt Casto
 
HTML 5 Offline Web apps
Alexandre Marreiros
 
Web fundamentals
Nguyen Van Vuong
 
Service workers
Pavel Zhytko
 
Webapps without the web
Remy Sharp
 
HTML5 Offline Web Applications (Silicon Valley User Group)
robinzimmermann
 
Take your drupal sites offline
Chris Ward
 
Progressive Web Apps - deep dive
Kenneth Rohde Christiansen
 
Ad

More from Jonathan Stark (20)

PDF
Ditching Hourly for Independent Firms
Jonathan Stark
 
PDF
How To Get More Leads And Increase Your Fees - Jonathan Stark
Jonathan Stark
 
PDF
Make More Money Without Working More Hours
Jonathan Stark
 
PDF
The Hourly Trap
Jonathan Stark
 
PDF
How To Write Proposals That Close Without Lowering Your Prices
Jonathan Stark
 
PDF
How To Write Better Proposals
Jonathan Stark
 
PDF
How To Increase Your Income Without Hiring Junior Developers
Jonathan Stark
 
PDF
Thinking Outside The Little Black Box: Interaction Design in The Post-Mobile Era
Jonathan Stark
 
PDF
The Path to Value Pricing
Jonathan Stark
 
PDF
Pigeonhole Yourself
Jonathan Stark
 
PDF
The Revolution Will Not Be Televised: Managing Content and Experience in the ...
Jonathan Stark
 
PDF
Pigeonhole Yourself
Jonathan Stark
 
PDF
Rethink Mobile: Mobile Strategy for Product Designers
Jonathan Stark
 
PDF
The Browser is Dead, Long Live the Web!
Jonathan Stark
 
KEY
The Revolution Will Not Be Televised
Jonathan Stark
 
KEY
Principles of Mobile Interface Design
Jonathan Stark
 
KEY
Free Coffee, Bad Apples, and the Future of Currency
Jonathan Stark
 
KEY
Three Things First
Jonathan Stark
 
KEY
Everyone Connected
Jonathan Stark
 
PPT
Enterprise Mobile: The Prosumer Impact On Business
Jonathan Stark
 
Ditching Hourly for Independent Firms
Jonathan Stark
 
How To Get More Leads And Increase Your Fees - Jonathan Stark
Jonathan Stark
 
Make More Money Without Working More Hours
Jonathan Stark
 
The Hourly Trap
Jonathan Stark
 
How To Write Proposals That Close Without Lowering Your Prices
Jonathan Stark
 
How To Write Better Proposals
Jonathan Stark
 
How To Increase Your Income Without Hiring Junior Developers
Jonathan Stark
 
Thinking Outside The Little Black Box: Interaction Design in The Post-Mobile Era
Jonathan Stark
 
The Path to Value Pricing
Jonathan Stark
 
Pigeonhole Yourself
Jonathan Stark
 
The Revolution Will Not Be Televised: Managing Content and Experience in the ...
Jonathan Stark
 
Pigeonhole Yourself
Jonathan Stark
 
Rethink Mobile: Mobile Strategy for Product Designers
Jonathan Stark
 
The Browser is Dead, Long Live the Web!
Jonathan Stark
 
The Revolution Will Not Be Televised
Jonathan Stark
 
Principles of Mobile Interface Design
Jonathan Stark
 
Free Coffee, Bad Apples, and the Future of Currency
Jonathan Stark
 
Three Things First
Jonathan Stark
 
Everyone Connected
Jonathan Stark
 
Enterprise Mobile: The Prosumer Impact On Business
Jonathan Stark
 

Recently uploaded (20)

PDF
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
PDF
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
PDF
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Generative AI in Healthcare: Benefits, Use Cases & Challenges
Lily Clark
 
PDF
CIFDAQ'S Token Spotlight for 16th July 2025 - ALGORAND
CIFDAQ
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
"Effect, Fiber & Schema: tactical and technical characteristics of Effect.ts"...
Fwdays
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PPTX
Machine Learning Benefits Across Industries
SynapseIndia
 
PDF
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PPTX
Earn Agentblazer Status with Slack Community Patna.pptx
SanjeetMishra29
 
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Generative AI in Healthcare: Benefits, Use Cases & Challenges
Lily Clark
 
CIFDAQ'S Token Spotlight for 16th July 2025 - ALGORAND
CIFDAQ
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
"Effect, Fiber & Schema: tactical and technical characteristics of Effect.ts"...
Fwdays
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Machine Learning Benefits Across Industries
SynapseIndia
 
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Earn Agentblazer Status with Slack Community Patna.pptx
SanjeetMishra29
 

Offline Application Cache

Editor's Notes

  • #2: Hi I&amp;#x2019;m Jonathan Stark. I&amp;#x2019;m a software consultant from Providence RI. I do mobile app strategy, design, and development.
  • #3: There&amp;#x2019;s a feature of HTML5 called the offline application cache (aka AppCache) that allows users to run web apps even when they are not connected to the Internet. It works like this: when a user navigates to your web app, the browser downloads and stores all the files it needs to display the page (HTML, CSS, JavaScript, images, etc.). The next time the user navigates to your web app, the browser will recognize the URL and serve the files out of the local application cache instead of pulling them across the network.
  • #4: You have to do three things to enable the app cache: - Creating a manifest file - Adding the manifest attribute to HTML open tags - Enabling the text/cache-manifest content type
  • #5: The main component of the offline application cache is a cache manifest file. A manifest file is just a simple text document that lives on your web server and contains a list of files that a user&amp;#x2019;s device must download and save in order to function.
  • #6: The paths in the manifest are relative to the location of the manifest file (similar to URLs in CSS files). You can also use absolute URLs if you prefer.
  • #7: Now that the manifest file is created, you link to it by adding a manifest attribute to the HTML tag inside index.html.
  • #8: You must serve the manifest file with the text/cache-manifest content type or the browser will not recognize it. If you are using the Apache web server or a compatible web server, you can accomplish this by adding an .htaccess file to your web directory.
  • #9: Our offline application cache is now in working order. The next time a user browses to http://example.com/index.html, the page and its resources will load normally over the network. In the background, all the files listed in the manifest will be downloaded locally to the user&amp;#x2019;s device. Once the download completes and the user refreshes the page, she&amp;#x2019;ll be accessing the local files only. She can now disconnect from the Internet and continue to access the web app.
  • #10: So now that the user is accessing our files locally on her device, we have a new problem: how does she get updates when changes are made to the website? When the user does have access to the Internet and navigates to the URL of our web app, her browser checks the manifest file on our site to see if it still matches the local copy. If the remote manifest has changed, the browser downloads all the files listed in it. It downloads these in the background to a temporary cache. The comparison between the local manifest and the remote manifest is a byte-by-byte comparison of the file contents (including comments and blank lines). The file modification timestamp and changes to any of the resources themselves are irrelevant when determining whether or not changes have been made. If something goes wrong during the download (e.g., the user loses her Internet connection), then the partially downloaded cache is automatically discarded and the previous one remains in effect. If the download is successful, the new local files will be used the next time the user launches the app.
  • #11: Sometimes, you don&amp;#x2019;t want certain files to be cached. Fortunately, app cache offers the option to force the browser to always access certain resources over the network. This means that the browser will not cache those resources locally, and that they will not be available when the user is offline.
  • #12: To specify a resource as online only, you use the NETWORK: keyword (the trailing : is essential) in the manifest file. Here, I&amp;#x2019;ve whitelisted logo.jpg by moving it into the NETWORK section of the manifest file. When the user is offline, the image will show up as a broken image link. When he is online, it will appear normally.
  • #13: If you don&amp;#x2019;t want offline users to see the broken image, you can use the FALLBACK keyword to specify a fallback resource.
  • #14: Now, when the user is offline, he&amp;#x2019;ll see offline.jpg, and when he&amp;#x2019;s online he&amp;#x2019;ll see logo.jpg.
  • #15: You can also specify a single fallback image for multiple resources by using a partial path. Let&amp;#x2019;s say I add an images directory to my website and put some files in it. I can then tell the browser to fall back to offline.jpg for anything contained in the images directory. Now, when the user is offline, he&amp;#x2019;ll see offline.jpg, and when he&amp;#x2019;s online he&amp;#x2019;ll see the actual online images.
  • #16: The manifest file is pretty finicky. If you have a single typo, invalid path, etc... it won&amp;#x2019;t work at all. If you have a lot of files in your app, it might make sense to build a manifest file dynamically.
  • #17: You can use a scripting language like PHP to traverse your directory structure to generate a list of files in your app. Theoretically, you could use this file as your manifest, but in practice this would cause place an unnecessary load on your server. I recommend using the PHP file to help you create a static manifest rather than serving it directly.
  • #18: It can be tough to debug apps that use the offline application cache because there&amp;#x2019;s very little visibility into what is going on. You find yourself constantly wondering if your files have downloaded, or if you are viewing remote or local resources. Plus, switching your device between online and offline modes is not the snappiest procedure and can really slow down the development cycle.
  • #19: The best way to ensure that you know what is actually being requested from you web server is to monitor your server logs while interacting with your app. If your manifest is working correctly, you should only see requests for your manifest file and any files listed in the network or fallback sections of the manifest.