Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function cache () {
await wbBuild.generateSW({
cacheId: `${applicationId}`,
globDirectory: `${releaseTarget}/`,
swDest: `${releaseTarget}/sw.js`,
globPatterns: ['*.{html,css,js}']
})
.then(() => {
console.log('Service worker generated.')
})
.catch((err) => {
console.log('[ERROR] This happened: ' + err)
})
}
task('generate-service-worker', () =>
wbBuild.generateSW({
globDirectory: config.dest.dist,
swDest: `${config.dest.dist}/sw.js`,
globPatterns: ['**\/*.{html,js,css}']
})
.then(() => {
console.log('Service worker generated.')
})
.catch((err) => {
console.log('[ERROR] This happened: ' + err)
}))
function makeServiceWorker (cb) {
console.log('Making service worker')
workboxBuild
.generateSW({
swDest: '.data/sw.js',
importWorkboxFrom: 'local',
skipWaiting: true,
clientsClaim: true,
navigateFallback: '/',
navigateFallbackWhitelist: [/^\/doc/, /^\/create/, /^\/add-link/],
globDirectory: '.',
globPatterns: ['index.html', 'static/manifest.webmanifest', 'static/**/*.svg', '.data/**/*.png'],
modifyUrlPrefix: {
'static': '',
'.data': ''
},
templatedUrls: {
'/': [ 'views/main.js' ]
},
const { generateSW } = require('workbox-build');
const swDest = 'build/sw.js';
generateSW({
swDest,
globDirectory: 'build'
}).then(({count, size}) => {
console.log(`Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.`);
});
builder.plugin('built', () => {
if (workboxOptions.swSrc) {
return swBuild.injectManifest(workboxOptions)
} else {
return swBuild.generateSW(workboxOptions)
}
})
})
tmpFile.write(fs.readFileSync(output, 'utf8').replace(/\.(precache|precacheAndRoute)\s*\(\s*\[([^\]]*)\]\)/gi, '.$1([])'));
try {
res = await workbox.injectManifest({
swSrc: tmpFile.path,
swDest: output,
globDirectory: input,
globPatterns: ['**/*'],
globIgnores: exclude,
maximumFileSizeToCacheInBytes: 1024 * 1024 * 10,
});
} catch (err) {
tmpFile.unlink();
throw err;
}
} else {
res = await workbox.generateSW({
globDirectory: input,
swDest: output,
globPatterns: ['**/*'],
globIgnores: exclude,
maximumFileSizeToCacheInBytes: 1024 * 1024 * 10,
});
}
task();
app.log(colors.bold(colors.green('service worker generated!')));
let { size, zipped } = utils.size(options.output);
app.log(`${utils.relativeToCwd(options.output)} ${colors.grey(`(${utils.prettyBytes(size)}, ${utils.prettyBytes(zipped)} zipped)`)}`);
if (options.watch) {
let watcher = new Watcher(input, {
log: false,
ignore: '**/*.map',
compiler.plugin('after-emit', (compilation, callback) => {
const config = this.getConfig(compilation);
if (config.swSrc) {
swBuild.injectManifest(config)
.then(() => callback())
.catch((e) => callback(e));
} else {
swBuild.generateSW(config)
.then(() => callback())
.catch((e) => callback(e));
}
});
}
const defaultConfig = {
globDirectory: this.options.outDir,
globPatterns: [
'**/*.{js,css,html,png,jpg,jpeg,gif,tiff}'
],
importWorkboxFrom: 'disabled',
importScripts: [],
swDest: destination
}
const cfg = Object.assign(defaultConfig, config);
cfg.importScripts = cfg.importScripts.concat(this.importScripts);
logger.progress('Creating service worker...');
await workbox.generateSW(cfg);
logger.log('Service worker generated at: ' + cfg.swDest);
} catch (error) {
throw error;
}
}
function precache() {
return workboxBuild.generateSW(path.join(config.dest, 'sw.js'), {
staticFileGlobs: shellFiles.map((uri) => `${config.dest}${uri}`),
stripPrefix: config.dest,
navigateFallback: '/404',
cacheId: packageName
});
}