Skip to content

Instantly share code, notes, and snippets.

@whmountains
Last active November 1, 2017 14:19
Show Gist options
  • Save whmountains/f477620b1336c51d6ab8150207466d13 to your computer and use it in GitHub Desktop.
Save whmountains/f477620b1336c51d6ab8150207466d13 to your computer and use it in GitHub Desktop.
Create AppCache based on parameters from sw-precache
const SW_PRECACHE_CONFIG = './sw-precache-config'
const OUT_FILE = 'build/precache.appcache'
const glob = require('globby')
const { staticFileGlobs, stripPrefix } = require(SW_PRECACHE_CONFIG)
const fs = require('fs')
const path = require('path')
glob(staticFileGlobs).then(files => {
// filter out directories
files = files.filter(file => fs.statSync(file).isFile())
// strip out prefix
files = files.map(file => file.replace(stripPrefix, ''))
// add the header and join to string
const out = ['CACHE MANIFEST', ...files].join('\n')
// write the file
fs.writeFileSync(path.join(__dirname, OUT_FILE), out)
// we're done!
console.log(`Wrote ${OUT_FILE} with ${files.length} resources.`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment