Skip to content

Instantly share code, notes, and snippets.

@stereobooster
Forked from whmountains/appcache-precache.js
Last active October 21, 2017 09:31
Show Gist options
  • Save stereobooster/c939006c031bccbdad3609f99614f6f0 to your computer and use it in GitHub Desktop.
Save stereobooster/c939006c031bccbdad3609f99614f6f0 to your computer and use it in GitHub Desktop.
Create AppCache based on parameters from sw-precache
// pair with https://github.com/gr2m/appcache-nanny
// read more http://mmariani.github.io/appcachefacts/
// https://iandevlin.com/blog/2015/06/html5/getting-appcaches-fallback-to-work-crossbrowser/
const SW_PRECACHE_CONFIG = './sw-precache-config'
const OUT_FILE = '../build/manifest.appcache'
const glob = require('globby')
const { staticFileGlobs, stripPrefix, navigateFallback } = 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, ''))
const index = files.indexOf(navigateFallback);
if (index > -1) {
files.splice(index, 1);
}
// add the header and join to string
const out = [
'CACHE MANIFEST',
`# version ${ new Date().getTime() }`,
'',
'CACHE:',
...files,
'',
'NETWORK:',
'*',
'http://*',
'https://*',
'',
'FALLBACK:',
`/ ${navigateFallback}`
].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