├── .gitignore ├── .jshintrc ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── README.md ├── appcache-loader.html ├── appcache-nanny.js ├── bin └── dev-server.js ├── bower.json ├── demo ├── demo.gif ├── index.html ├── manifest.appcache ├── script.js └── style.css ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | log 2 | node_modules 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | /* 3 | * ENVIRONMENTS 4 | * ================= 5 | */ 6 | 7 | // Define globals exposed by modern browsers. 8 | "browser": true, 9 | 10 | // Define globals exposed by jQuery. 11 | "jquery": true, 12 | 13 | // Define globals exposed by Node.js. 14 | "node": true, 15 | 16 | /* 17 | * ENFORCING OPTIONS 18 | * ================= 19 | */ 20 | 21 | // Force all variable names to use either camelCase style or UPPER_CASE 22 | // with underscores. 23 | "camelcase": true, 24 | 25 | // Prohibit use of == and != in favor of === and !==. 26 | "eqeqeq": true, 27 | 28 | // Suppress warnings about == null comparisons. 29 | "eqnull": true, 30 | 31 | // Enforce tab width of 2 spaces. 32 | "indent": 2, 33 | 34 | // Prohibit use of a variable before it is defined. 35 | "latedef": "nofunc", 36 | 37 | // Require capitalized names for constructor functions. 38 | "newcap": true, 39 | 40 | // Enforce use of single quotation marks for strings. 41 | "quotmark": "single", 42 | 43 | // Prohibit trailing whitespace. 44 | "trailing": true, 45 | 46 | // Prohibit use of explicitly undeclared variables. 47 | "undef": true, 48 | 49 | // Warn when variables are defined but never used. 50 | "unused": false, 51 | 52 | // Enforce placing 'use strict' at the top function scope 53 | "strict": false 54 | } 55 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | notifications: 7 | email: false 8 | node_js: 9 | - '4' 10 | before_install: 11 | - npm i -g npm@3 12 | before_script: 13 | - npm prune 14 | after_success: 15 | - npm run semantic-release 16 | env: 17 | matrix: 18 | - CLIENT=saucelabs:firefox:34 # pinning version, see https://code.google.com/p/selenium/issues/detail?id=8390 19 | - CLIENT=saucelabs:chrome 20 | - CLIENT=saucelabs:safari:6 21 | - CLIENT="saucelabs:iphone:8.1:OS X 10.9" 22 | - CLIENT="saucelabs:internet explorer:11:Windows 8.1" 23 | branches: 24 | except: 25 | - /^v\d+\.\d+\.\d+$/ 26 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource+coc@martynus.net. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [https://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: https://contributor-covenant.org 74 | [version]: https://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | The appCache Nanny 2 | ================== 3 | 4 | > Teaches the applicationCache douchebag some manners! 5 | 6 | [](https://travis-ci.org/gr2m/appcache-nanny) 7 | [](https://david-dm.org/gr2m/appcache-nanny) 8 | [](https://david-dm.org/gr2m/appcache-nanny#info=devDependencies) 9 | 10 | As we all know, the [Application Cache is a Douchebag](http://alistapart.com/article/application-cache-is-a-douchebag). 11 | It's time to teach it some manners – The appCache Nanny for Rescue! 12 | 13 | No more manifest attributes on your HTML files. Whether you want to cache 14 | your assets offline or not, when to start ... You Are In Control™ 15 | 16 | Have a glance: 17 | 18 | ```js 19 | // start to check for updates every 30s 20 | appCacheNanny.start() 21 | 22 | // optionally, pass intervals in ms 23 | appCacheNanny.start({checkInterval: 10000}) 24 | 25 | // you can also check for updates at any time 26 | appCacheNanny.update() 27 | 28 | // The appCache nanny tells you if there is a new update available 29 | appCacheNanny.hasUpdate() 30 | 31 | // She tells you about all relevant applicationCache events 32 | appCacheNanny.on('update', handleUpdate) 33 | appCacheNanny.on('error', handleError) 34 | appCacheNanny.on('obsolete', handleObsolete) 35 | appCacheNanny.on('noupdate', handleNoupdate) 36 | appCacheNanny.on('downloading', handleDownloading) 37 | appCacheNanny.on('progress', handleProgress) 38 | appCacheNanny.on('cached', handleCached) 39 | appCacheNanny.on('updateready', handleUpdateready) 40 | 41 | // plus some extra ones 42 | appCacheNanny.on('init:downloading', handleInitDownloading) 43 | appCacheNanny.on('init:progress', handleInitProgress) 44 | appCacheNanny.on('init:cached', handleInitCached) 45 | appCacheNanny.on('start', handleStart) 46 | appCacheNanny.on('stop', handleStop) 47 | 48 | // options 49 | appCacheNanny.set('loaderPath', '/path/to/my-custom-loader.html') 50 | appCacheNanny.set({ 'loaderPath': '/path/to/my-custom-loader.html' }) 51 | appCacheNanny.get('loaderPath') 52 | appCacheNanny.get() // returns all options 53 | ``` 54 | 55 | Setup 56 | ----- 57 | 58 | 1. Copy `appcache-loader.html` into the root directory of your app, 59 | so that it's accessible at `/appcache-loader.html`. 60 | 2. Create the `manifest.appcache` file and put it in the root directory 61 | of your app, next to `/appcache-loader.html`. If you use a different 62 | name, make sure to set it accordingly in `appcache-loader.html`. 63 | 64 | Then load the `appcache-nanny.js` on your HTML pages. 65 | 66 | ```html 67 | 68 | ``` 69 | 70 | If you use [bower](http://bower.io/), you can install it using: 71 | 72 | ``` 73 | bower install --save appcache-nanny 74 | ``` 75 | 76 | Or install via [npm](https://www.npmjs.com/) 77 | 78 | ``` 79 | npm install --save appcache-nanny 80 | ``` 81 | 82 | Background 83 | ---------- 84 | 85 | I extracted `appcache-nanny.js` from [minutes.io](https://minutes.io), which is an [Offline First](http://offlinefirst.org/) 86 | web application, anno 2011. It's battle tested by a ton of users, devices, internet environments. 87 | 88 | minutes.io checks every 30 seconds if an update is available. And whenever the user navigates 89 | from one view to another, it reloads the page in case there is. As the assets are all cached, 90 | the user cannot tell that their page got just reloaded. It's silent, without any notification, 91 | or a prompt asking the user to reload the page. And it works very well so far. 92 | 93 | Demo 94 | ---- 95 | 96 |  97 | 98 | The appCache Nanny comes with a simple server for testing. Start it using Node: 99 | 100 | ```js 101 | npm start 102 | ``` 103 | 104 | It will start a local server at http://localhost:8888. 105 | 106 | This is a static server with a few hidden features: 107 | 108 | - `GET /bump-version` increases the app version, so an update gets triggered 109 | on next check 110 | - `GET /remove-manifest` makes `GET /manifest.appcache` return 404, so it 111 | becomes obsolete on next check 112 | - `GET /recreate-manifest` undoes the previous step. 113 | 114 | 115 | Gotchas 116 | ------- 117 | 118 | ### Unlisted paths get loaded from the server, not from cache (via FALLBACK) 119 | 120 | Thanks to the [iframe hack](http://labs.ft.com/category/tutorial/), your HTML pages do 121 | not get added to the list of cached assets locally, and won't get checked for updates 122 | each time. Which is great especially for a Single Page Application with pushState enabled. 123 | But if your app has paths like `/welcome`, `/Dashboard`, `Meeting/123` and your `manifest.appcache` 124 | looks something like 125 | 126 | ``` 127 | CACHE MANIFEST 128 | 129 | / 130 | /app.js 131 | /styles.css 132 | 133 | FALLBACK: 134 | / / 135 | ``` 136 | 137 | Beware that opening `http://yourapp.com` will always show the currently cached 138 | version, as it is explicetly listed, while `http://yourapp.com/welcome` and 139 | other paths will load the page from the server, unless the user is offline. 140 | 141 | You might have learned that assets that are not listed in the cache manifest 142 | cannot be loaded at all, even when online. But that's not the case if the 143 | loaded HTML page does not have the `manifest` property on the `html` tag. 144 | 145 | 146 | Acknowledgement 147 | --------------- 148 | 149 | The appCache Nanny is based on tremendeous amount of research others have done 150 | on applicationCache. I'd like to highlight 151 | 152 | * **[Jake Archibald](https://twitter.com/jaffathecake)**: [Application Cache is a Douchebag](http://alistapart.com/article/application-cache-is-a-douchebag) 153 | and for some great laughs: [Network connectivity: optional](http://www.youtube.com/watch?v=Z7sRMg0f5Hk) 154 | * **[Financial Times Lab Team](http://labs.ft.com)**: [Tutorial: How to make an offline HTML5 web app](http://labs.ft.com/category/tutorial/), 155 | also recommended: [Andrew Betts – Offline rules](http://www.youtube.com/watch?v=Ut4R4udJ4Gw) 156 | * **[Eric Bidelman](https://twitter.com/ebidel)**: [A Beginner's Guide to Using the Application Cache](http://www.html5rocks.com/en/tutorials/appcache/beginner/) 157 | * [Appcache Facts](http://appcache.offline.technology/) by [Mark Christian](https://twitter.com/shinypb) & [Peter Lubbers](https://twitter.com/peterlubbers) 158 | 159 | 160 | TODOs / IDEAs 161 | ------------- 162 | 163 | * on obsolete, remove the iframe, load it again to check if a new *.appcache path 164 | has been set. If yes, update and trigger `updateready` event, otherwise trigger 165 | `obsolete` event 166 | 167 | Fine Print 168 | ---------- 169 | 170 | The appCache Nanny has been authored by [Gregor Martynus](https://github.com/gr2m), 171 | proud member of the [Hoodie Community](http://hood.ie/). 172 | 173 | License: MIT 174 | -------------------------------------------------------------------------------- /appcache-loader.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |20 | is cached? 21 | | 22 |
23 |
24 | yes
25 |
26 |
27 | no
30 | 28 | 29 | |
31 |
---|---|
34 | has update? 35 | | 36 |
37 |
38 | yes
39 | (no more events will be triggered)
42 | 40 | 41 |
43 | no
46 |
47 | 44 | 45 | |
48 |
51 | Is autoupdating? 52 | | 53 |
54 |
55 | no
59 | 56 | 57 | 58 |
60 | yes
63 | 61 | 62 | |
64 |
Server | 67 |
68 |
69 |
70 |
71 |
72 | |
73 |
79 | 80 |
81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /demo/manifest.appcache: -------------------------------------------------------------------------------- 1 | CACHE MANIFEST 2 | 3 | CACHE: 4 | / 5 | /style.css 6 | /script.js 7 | /appcache-nanny.js 8 | /version.js 9 | 10 | FALLBACK: 11 | / / 12 | -------------------------------------------------------------------------------- /demo/script.js: -------------------------------------------------------------------------------- 1 | /* global appCacheNanny, XMLHttpRequest */ 2 | 3 | // log all appCacheNanny events 4 | [ 5 | 'obsolete', 6 | 'noupdate', 7 | 'downloading', 8 | 'progress', 9 | 'cached', 10 | 'updateready', 11 | 'init:downloading', 12 | 'init:progress', 13 | 'init:cached', 14 | 'start', 15 | 'stop', 16 | 'online', 17 | 'offline' 18 | ].forEach(function (eventName) { 19 | appCacheNanny.on(eventName, function (event) { 20 | log('event', eventName, event) 21 | }) 22 | }) 23 | 24 | // log errors 25 | appCacheNanny.on('error', function () { 26 | log('error', 'appCacheNanny error') 27 | }) 28 | 29 | // update "has update?" state in UI 30 | appCacheNanny.on('updateready', function () { 31 | document.body.setAttribute('data-hasupdate', '1') 32 | }) 33 | 34 | // update "is cached?" state in UI 35 | ;[ 36 | 'noupdate', 37 | 'cached', 38 | 'init:cached' 39 | ].forEach(function (eventName) { 40 | appCacheNanny.on(eventName, function () { 41 | document.body.setAttribute('data-iscached', '1') 42 | }) 43 | }) 44 | appCacheNanny.on('obsolete', function () { 45 | document.body.setAttribute('data-iscached', '0') 46 | }) 47 | // requesting non-existing path will only succeed when 48 | // app is cached, because of the `/ /` line below FALLBACK 49 | request({ 50 | path: '/non-existing', 51 | onSuccess: function () { 52 | document.body.setAttribute('data-iscached', '1') 53 | }, 54 | onError: function (xhr) { 55 | console.log('You can ignore the failing request to /non-existing. It\'s just a test if the app is cached or not.') 56 | 57 | // IE response with error code, but returns the text 58 | if (/appCache Demo page/.test(xhr.responseText)) { 59 | document.body.setAttribute('data-iscached', '1') 60 | } 61 | } 62 | }) 63 | 64 | function log (type, text, event) { 65 | var item = document.createElement('p') 66 | 67 | if (/progress/.test(text)) { 68 | item.innerHTML = '' + type + ' ' + text + ' (' + event.loaded + '/' + event.total + ')' 69 | } else { 70 | item.innerHTML = '' + type + ' ' + text 71 | } 72 | 73 | item.className = type 74 | document.querySelector('#logs').appendChild(item) 75 | document.body.setAttribute('data-haslogs', '1') 76 | } 77 | 78 | function request (options) { 79 | var req = new XMLHttpRequest() 80 | req.open('GET', options.path, true) 81 | 82 | req.onload = function () { 83 | if (req.status !== 200) { 84 | return options.onError && options.onError(req) 85 | } 86 | options.onSuccess && options.onSuccess(req.responseText) 87 | } 88 | 89 | req.onerror = function () { 90 | options.onError && options.onError(req) 91 | } 92 | req.send() 93 | } 94 | 95 | /* eslint-disable no-unused-vars */ 96 | function clearLogs () { 97 | document.querySelector('#logs').innerHTML = '' 98 | document.body.setAttribute('data-haslogs', '0') 99 | } 100 | 101 | function check () { 102 | log('command', 'appCacheNanny.update()') 103 | appCacheNanny.update() 104 | } 105 | function start (interval) { 106 | if (interval) { 107 | log('command', 'appCacheNanny.start({checkInterval: ' + interval + '})') 108 | appCacheNanny.start({checkInterval: interval}) 109 | } else { 110 | log('command', 'appCacheNanny.start()') 111 | appCacheNanny.start() 112 | } 113 | document.body.setAttribute('data-isautoupdating', '1') 114 | } 115 | function stop () { 116 | log('command', 'appCacheNanny.stop()') 117 | appCacheNanny.stop() 118 | document.body.setAttribute('data-isautoupdating', '0') 119 | } 120 | function bumpVersion () { 121 | request({ 122 | path: '/bump-version', 123 | onSuccess: function (version) { 124 | log('server', 'New version: ' + version) 125 | }, 126 | onError: function () { 127 | log('error', 'Could not bump version – server error') 128 | } 129 | }) 130 | } 131 | function removeManifest () { 132 | request({ 133 | path: '/remove-manifest', 134 | onSuccess: function () { 135 | log('server', 'Cache manifes removed.') 136 | appCacheNanny.update() 137 | appCacheNanny.on('obsolete', createManifest) 138 | }, 139 | onError: function () { 140 | log('error', 'Could not remove manifest – server error') 141 | } 142 | }) 143 | } 144 | 145 | function createManifest () { 146 | request({ 147 | path: '/recreate-manifest', 148 | onSuccess: function () { 149 | log('server', 'Cache manifes recreated.') 150 | appCacheNanny.off('obsolete', createManifest) 151 | }, 152 | onError: function () { 153 | log('error', 'Could not recreate manifest – server error') 154 | } 155 | }) 156 | } 157 | -------------------------------------------------------------------------------- /demo/style.css: -------------------------------------------------------------------------------- 1 | html { 2 | font-family: Helvetica, Arial; 3 | background: white; 4 | } 5 | body { 6 | margin: 0; 7 | } 8 | 9 | [data-iscached="0"] .if-iscached, 10 | [data-iscached="1"] .unless-iscached, 11 | [data-hasupdate="0"] .if-hasupdate, 12 | [data-hasupdate="1"] .unless-hasupdate, 13 | [data-isautoupdating="0"] .if-isautoupdating, 14 | [data-isautoupdating="1"] .unless-isautoupdating, 15 | [data-haslogs="0"] .if-haslogs, 16 | [data-haslogs="1"] .unless-haslogs { 17 | display: none; 18 | } 19 | header { 20 | padding: 1em; 21 | background: #eee; 22 | border-bottom: 1px solid #ddd; 23 | } 24 | h1 small { 25 | display: block; 26 | font-weight: normal; 27 | font-size: 0.5em; 28 | } 29 | small { 30 | opacity: .5; 31 | } 32 | table { 33 | width: 100%; 34 | } 35 | th { 36 | text-align: left; 37 | white-space: nowrap; 38 | width: 1px; 39 | padding-right: .5em; 40 | min-width: 130px; 41 | } 42 | td, th { 43 | border-top: 1px solid #000; 44 | vertical-align: top; 45 | padding-top: .5em; 46 | padding-bottom: .5em; 47 | } 48 | button { 49 | border: 0; 50 | padding: 1em; 51 | margin: 0 0 1em; 52 | background: #333; 53 | color: #fff; 54 | border-radius: 4px; 55 | } 56 | 57 | #logs p { 58 | padding: 1em; 59 | margin: 0; 60 | border-bottom: 1px solid #ddd; 61 | } 62 | #logs + p { 63 | padding: 1em; 64 | margin: 0; 65 | } 66 | #logs strong { 67 | font-size: .8em; 68 | font-weight: normal; 69 | border-radius: 4px; 70 | padding: .2em .4em; 71 | color: #fff; 72 | } 73 | #logs .command strong { 74 | background: #333; 75 | } 76 | #logs .event strong { 77 | background: #36F; 78 | } 79 | #logs .error strong { 80 | background: #C00; 81 | } 82 | #logs .server strong { 83 | background: #00B32D; 84 | } 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appcache-nanny", 3 | "description": "Teaches the applicationCache douchebag some manners!", 4 | "main": "appcache-nanny.js", 5 | "scripts": { 6 | "pretest": "standard", 7 | "test": "frontend-test-background mocha test/*.js", 8 | "start": "node ./bin/dev-server.js", 9 | "semantic-release": "semantic-release pre && npm publish && semantic-release post" 10 | }, 11 | "repository": "github:gr2m/appcache-nanny", 12 | "keywords": [ 13 | "browser", 14 | "appcache", 15 | "offlinefirst" 16 | ], 17 | "author": "Gregor Martynus