Git Product home page Git Product logo

appcache-nanny's Introduction

The appCache Nanny

Teaches the applicationCache douchebag some manners!

Build Status Dependency Status devDependency Status

As we all know, the Application Cache is a Douchebag. It's time to teach it some manners – The appCache Nanny for Rescue!

No more manifest attributes on your HTML files. Whether you want to cache your assets offline or not, when to start ... You Are In Control™

Have a glance:

// start to check for updates every 30s
appCacheNanny.start()

// optionally, pass intervals in ms
appCacheNanny.start({checkInterval: 10000})

// you can also check for updates at any time
appCacheNanny.update()

// The appCache nanny tells you if there is a new update available
appCacheNanny.hasUpdate()

// She tells you about all relevant applicationCache events
appCacheNanny.on('update', handleUpdate)
appCacheNanny.on('error', handleError)
appCacheNanny.on('obsolete', handleObsolete)
appCacheNanny.on('noupdate', handleNoupdate)
appCacheNanny.on('downloading', handleDownloading)
appCacheNanny.on('progress', handleProgress)
appCacheNanny.on('cached', handleCached)
appCacheNanny.on('updateready', handleUpdateready)

// plus some extra ones
appCacheNanny.on('init:downloading', handleInitDownloading)
appCacheNanny.on('init:progress', handleInitProgress)
appCacheNanny.on('init:cached', handleInitCached)
appCacheNanny.on('start', handleStart)
appCacheNanny.on('stop', handleStop)

// options
appCacheNanny.set('loaderPath', '/path/to/my-custom-loader.html')
appCacheNanny.set({ 'loaderPath': '/path/to/my-custom-loader.html' })
appCacheNanny.get('loaderPath')
appCacheNanny.get() // returns all options

Setup

  1. Copy appcache-loader.html into the root directory of your app, so that it's accessible at /appcache-loader.html.
  2. Create the manifest.appcache file and put it in the root directory of your app, next to /appcache-loader.html. If you use a different name, make sure to set it accordingly in appcache-loader.html.

Then load the appcache-nanny.js on your HTML pages.

<script src="appcache-nanny.js" async></script>

If you use bower, you can install it using:

bower install --save appcache-nanny

Or install via npm

npm install --save appcache-nanny

Background

I extracted appcache-nanny.js from minutes.io, which is an Offline First web application, anno 2011. It's battle tested by a ton of users, devices, internet environments.

minutes.io checks every 30 seconds if an update is available. And whenever the user navigates from one view to another, it reloads the page in case there is. As the assets are all cached, the user cannot tell that their page got just reloaded. It's silent, without any notification, or a prompt asking the user to reload the page. And it works very well so far.

Demo

appCacheNanny demo screencast

The appCache Nanny comes with a simple server for testing. Start it using Node:

npm start

It will start a local server at http://localhost:8888.

This is a static server with a few hidden features:

  • GET /bump-version increases the app version, so an update gets triggered on next check
  • GET /remove-manifest makes GET /manifest.appcache return 404, so it becomes obsolete on next check
  • GET /recreate-manifest undoes the previous step.

Gotchas

Unlisted paths get loaded from the server, not from cache (via FALLBACK)

Thanks to the iframe hack, your HTML pages do not get added to the list of cached assets locally, and won't get checked for updates each time. Which is great especially for a Single Page Application with pushState enabled. But if your app has paths like /welcome, /Dashboard, Meeting/123 and your manifest.appcache looks something like

CACHE MANIFEST

/
/app.js
/styles.css

FALLBACK:
/ /

Beware that opening http://yourapp.com will always show the currently cached version, as it is explicetly listed, while http://yourapp.com/welcome and other paths will load the page from the server, unless the user is offline.

You might have learned that assets that are not listed in the cache manifest cannot be loaded at all, even when online. But that's not the case if the loaded HTML page does not have the manifest property on the html tag.

Acknowledgement

The appCache Nanny is based on tremendeous amount of research others have done on applicationCache. I'd like to highlight

TODOs / IDEAs

  • on obsolete, remove the iframe, load it again to check if a new *.appcache path has been set. If yes, update and trigger updateready event, otherwise trigger obsolete event

Fine Print

The appCache Nanny has been authored by Gregor Martynus, proud member of the Hoodie Community.

License: MIT

appcache-nanny's People

Contributors

gr2m avatar greenkeeperio-bot avatar kahlil avatar mikeymckay avatar paulirish avatar timwis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

appcache-nanny's Issues

Running demo

I had to npm install hapi and moment to get it to run.

Support Grunt-built apps… somehow

Appcache-Nanny works like a charm on single-page apps that aren't processed through a task-runner. However, when using Grunt to compile everything into dist/ then it doesn't work. I configured Gruntfile.js to port all the scripts, loader, and manifest into dist/ but now the compiled app http requests always report back 200 instead of 304. For reference, I added Appcache-Nanny via Bower to a project created by Yeoman with the Angular generator.

doubled applicationCache events (seen in latest Chrome stable)

to reproduce

  1. start the demo server https://github.com/gr2m/appcache-nanny#demo
  2. Open http://localhost:8888/
  3. Click on [Check for update]
  4. Reload page
  5. Click on [Check for update]

You now see the Application Cache Checking event & Application Cache NoUpdate event twice.

I found out that applicationCache.status becomes 1 in the HTML file after step 4., although it lacks the <html manifest=".."> attribute. This should not be possible, or it's another thing I've missed or they don't say in the appCache Handbook™.

Anyway, the work around is pretty simple. When applicationCache.status is not 0 or 5, don't load the iFrame, but use the applicationCache object of the host HTML file.

Breaks when build minified

Hi,

My automated builds are failing because the ";" after use script is misplaced. Can you please fix it? Otherwise this is an awesome project!!!
2016-09-15 23_02_52-appcache-nanny js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.