appcache-serviceworker-generator
Can generate an appcache manifest and ServiceWorker from same abstraction.
Last updated 5 years ago by henrikjoreteg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install appcache-serviceworker-generator 
SYNC missed versions from official npm registry.

appcache-serviceworker-generator

Can generates an appcache manifest and ServiceWorker from same abstraction.

While this largely ignores all the other cool stuff you can with ServiceWorker, I still found it useful for a small, static, offline app I'm building.

var createManifest = require('appcache-serviceworker-generator')
var fs = require('fs')

// give it a version (important for appcache)
var manifest = createManifest({version: '123'})

manifest.cache('/some/image.png')
manifest.cache('/')
manifest.fallback('/somepath.html /somefallback.html')

console.log(manifest.toAppCache())
// creates this string =>
// CACHE MANIFEST
// # version: 123
// 
// CACHE:
// /
// /some/image.png
// 
// NETWORK:
// *
// 
// FALLBACK:
// /somepath.html /somefallback.html
// 

// It can then also generate a *roughly* equivalent ServiceWorker as a string
// (it's your job to write to disk or whatever)
console.log(manifest.toServiceWorker())

How ready for primetime is this? Short answer: ¯\_(ツ)_/¯

I'm mainly sharing this because I think it's interesting, not because I think you should use it or saying that I'm interested in maintaining this long term.

I am using this on a small project where I've generated a static site with webpack and use the assets information that webpack has to generate an appcache for iOS (since no SW support) and a ServiceWorker for platforms that support it. It has a few very basic tests. Biggest question is how good the generated SW implementation actually is, feedback? Ping me on twitter: @HenrikJoreteg.

The app the initiates either selectively by first checking for support like this:

var cacheNanny = require('appcache-nanny')

if ('serviceWorker' in navigator) {
    navigator.serviceWorker
        .register('/sw.js')
        .then(function(registration) {
            // Registration was successful
            console.log('ServiceWorker registration successful with scope: ',    registration.scope);
        }).catch(function(err) {
            // registration failed :(
            console.log('ServiceWorker registration failed: ', err);
        })
} else {
	// this uses cache-nanny to actually give you a choice 
	// of when to init your appcache :)
	// see link below
    cacheNanny.start()
}

If you're planning on using appcache at all, I'd suggest checking out gr2m/appcache-nanny as used by the Hood.ie folks (who know a thing or two about offline).

example

See example directory. Run node example/run-me to re-generate the appcache and SW files in that dir.

install

npm install appcache-serviceworker-generator

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT

Current Tags

  • 1.0.1                                ...           latest (5 years ago)

2 Versions

  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (3)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |