https://github.com/googlechromelabs/sw-appcache-behavior
A service worker implementation of the behavior defined in a page's AppCache manifest.
https://github.com/googlechromelabs/sw-appcache-behavior
appcache service-worker
Last synced: 9 months ago
JSON representation
A service worker implementation of the behavior defined in a page's AppCache manifest.
- Host: GitHub
- URL: https://github.com/googlechromelabs/sw-appcache-behavior
- Owner: GoogleChromeLabs
- License: apache-2.0
- Created: 2017-04-10T17:08:29.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2025-03-28T22:17:16.000Z (about 1 year ago)
- Last Synced: 2025-06-25T08:49:44.423Z (10 months ago)
- Topics: appcache, service-worker
- Language: TypeScript
- Homepage:
- Size: 1.71 MB
- Stars: 54
- Watchers: 12
- Forks: 12
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## About
A pair of modules meant to ease the transition
[off of AppCache](https://alistapart.com/article/application-cache-is-a-douchebag/)
and on to
[service workers](https://developers.google.com/web/fundamentals/primers/service-workers/).
Note: These libraries attempt to replicate the caching and serving behavior that AppCache offers, but does **not** include direct equivalents to the [`window.applicationCache` interface](https://developer.mozilla.org/en-US/docs/Web/API/Window/applicationCache), nor the related events that AppCache would fire in the `window` context.
## Installation
There are two modules to install: one that is used from within the `window`
context in your web app, and the other that's used in the context of your
service worker.
```sh
npm install --save-dev appcache-polyfill-window
npm install --save-dev appcache-polyfill-sw
```
As an alternative to local installation & serving, you can load both libraries from a NPM CDN, like https://unpkg.com/ or https://www.pika.dev/.
## Usage
### Window client
```html
import {init} from '/path/to/appcache-polyfill-window/build/index.modern.js';
// Optional: define a callback that runs whenever caches are updated.
// This is *rough* replacement for listening for AppCache updates.
function myCachePopulatedCallback(urls) {
// urls is an array of updated URLs
// Your logic goes here.
}
init({
cachePopulatedCallback: myCachePopulatedCallback,
}).then(() => navigator.serviceWorker.register('sw.js'));
```
### Service worker client
```js
importScripts('/path/to/appcache-polyfill-sw/build/index.umd.js');
self.addEventListener('fetch', (event) => {
// Alternatively, examine event.request and only use the
// appcachePolyfill.handle() logic for a subset of requests.
event.respondWith(appcachePolyfill.handle(event));
});
```
## Feedback
Please [open an issue](https://github.com/googlechromelabs/sw-appcache-behavior/issues) with feedback or bug reports if you run in to problems.