Skip to content

Instantly share code, notes, and snippets.

@iampava
Created July 19, 2017 17:05
Show Gist options
  • Save iampava/8d56844def72260e87e73787fddffbef to your computer and use it in GitHub Desktop.
Save iampava/8d56844def72260e87e73787fddffbef to your computer and use it in GitHub Desktop.
Angular 2 offline support with sw-precache library
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