Created
July 19, 2017 17:05
-
-
Save iampava/8d56844def72260e87e73787fddffbef to your computer and use it in GitHub Desktop.
Angular 2 offline support with sw-precache library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const swPrecache = require('sw-precache'); | |
function getFiles(path, regExpArray) { | |
//Returns an erray of fileNames that respect the RegExp | |
return new Promise((resolve, reject) => { | |
fs.readdir(path, (err, items) => { | |
resolve(items.filter(item => regExpArray.some(regExp => regExp.test(item))).map(item => `${path}/${item}`)); | |
}); | |
}); | |
} | |
getFiles('dist', [new RegExp('vendor.(.+).js')]).then(items => { | |
const options = { | |
"clientsClaim ": false, | |
"skipWaiting": false, | |
"staticFileGlobs": [ | |
"dist/**.css", | |
...items | |
], | |
"stripPrefix": "dist", | |
"runtimeCaching": [{ | |
"urlPattern": "(.+).chunk.js", | |
"handler": "cacheFirst" | |
}], | |
"verbose": true | |
} | |
swPrecache.write('./dist/service-worker.js', options, (err) => { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment