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

KEY
Asynchronous Interfaces
PDF
ActionHeroJS Talk
PPTX
ATG pipelines
PDF
What happens in laravel 4 bootstraping
PDF
Spout - Building a RESTful web app with Angular.js and BEAR.Sunday
PPTX
ATG Advanced Profile Management
PDF
API Days Paris - Automatic Testing of (RESTful) API Documentation
PDF
HTML5 tutorial: canvas, offfline & sockets
Asynchronous Interfaces
ActionHeroJS Talk
ATG pipelines
What happens in laravel 4 bootstraping
Spout - Building a RESTful web app with Angular.js and BEAR.Sunday
ATG Advanced Profile Management
API Days Paris - Automatic Testing of (RESTful) API Documentation
HTML5 tutorial: canvas, offfline & sockets

What's hot (20)

PPTX
Learning Svelte
PDF
The road to continuous deployment: a case study (DPC16)
PDF
Learning django step 1
KEY
Dancing with websocket
KEY
Lazy Loading Because I'm Lazy
PDF
Javascript Frameworks for Joomla
PDF
Asynchronous PHP and Real-time Messaging
PDF
Phinx talk
PDF
"Service Worker: Let Your Web App Feel Like a Native "
PDF
Better Testing With PHP Unit
PDF
Kyiv.py #17 Flask talk
PPT
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
PDF
Puppet Camp 2012
PPTX
Qualidade de vida: Com Zabbix e API
PDF
Security Challenges in Node.js
PDF
Laravel Design Patterns
PDF
Scalable web application architecture
PDF
XamarinとAWSをつないでみた話
Learning Svelte
The road to continuous deployment: a case study (DPC16)
Learning django step 1
Dancing with websocket
Lazy Loading Because I'm Lazy
Javascript Frameworks for Joomla
Asynchronous PHP and Real-time Messaging
Phinx talk
"Service Worker: Let Your Web App Feel Like a Native "
Better Testing With PHP Unit
Kyiv.py #17 Flask talk
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
Puppet Camp 2012
Qualidade de vida: Com Zabbix e API
Security Challenges in Node.js
Laravel Design Patterns
Scalable web application architecture
XamarinとAWSをつないでみた話
Ad

Viewers also liked (8)

PDF
Implementing Data Caching and Data Synching Using Oracle MAF
PPTX
Offnet- Offline Mobile Application
PDF
Offline Strategy for an Online World
PDF
Designing For Offline
ODP
Offline First Applications
PPTX
Offline first: application data and synchronization
PDF
Sync is hard: building offline-first Android apps from the ground up
PDF
Data Synchronization Patterns in Mobile Application Design
Implementing Data Caching and Data Synching Using Oracle MAF
Offnet- Offline Mobile Application
Offline Strategy for an Online World
Designing For Offline
Offline First Applications
Offline first: application data and synchronization
Sync is hard: building offline-first Android apps from the ground up
Data Synchronization Patterns in Mobile Application Design
Ad

Similar to Offline Application Cache (20)

PDF
Using html5 to build offline applications
PDF
Rails for Mobile Devices @ Conferencia Rails 2011
PPT
HTML5 Offline Web Application
PPTX
Html5 Offline Applications
PPTX
Html5 offline
PPTX
Offline Storage
PDF
06 HTML5 Mobile
PPTX
The Power of HTML5 Offline: Mobile and More!
PDF
Aleksey Bogachuk - "Offline Second"
PPTX
Peter lubbers-html5-offline-web-apps
PDF
Building great mobile apps: Somethings you might want to know
PDF
HTML5: huh, what is it good for?
PPTX
Taking Web Applications Offline
PPT
HTML 5 Offline Web apps
PDF
Web fundamentals
PDF
Service workers
PDF
Webapps without the web
PDF
HTML5 Offline Web Applications (Silicon Valley User Group)
PPTX
Take your drupal sites offline
PDF
Progressive Web Apps - deep dive
Using html5 to build offline applications
Rails for Mobile Devices @ Conferencia Rails 2011
HTML5 Offline Web Application
Html5 Offline Applications
Html5 offline
Offline Storage
06 HTML5 Mobile
The Power of HTML5 Offline: Mobile and More!
Aleksey Bogachuk - "Offline Second"
Peter lubbers-html5-offline-web-apps
Building great mobile apps: Somethings you might want to know
HTML5: huh, what is it good for?
Taking Web Applications Offline
HTML 5 Offline Web apps
Web fundamentals
Service workers
Webapps without the web
HTML5 Offline Web Applications (Silicon Valley User Group)
Take your drupal sites offline
Progressive Web Apps - deep dive

More from Jonathan Stark (20)

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

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Review of recent advances in non-invasive hemoglobin estimation
cuic standard and advanced reporting.pdf

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.