Skip to content

Instantly share code, notes, and snippets.

@kainy
Created December 25, 2017 09:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kainy/a40cfea1a514ef0e789a1943eb03d0ee to your computer and use it in GitHub Desktop.
使用workbox生成SW脚本的gulpfile配置。
gulp.task('sw', ['copy', 'css', 'js', 'img'], () => {
return wbBuild.generateSW({
globDirectory: './www/',
swDest: './optimized/sw.js',
globPatterns: ["**/*.{css,jpg,png,js,html,shtml}"],
ignoreUrlParametersMatching: [/(v|t)/],
skipWaiting: true,
clientsClaim: true,
runtimeCaching: [{
urlPattern : 'https://API-Url/(.*)',
handler: 'networkFirst',
options: {
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern : 'https://vendor-Url/(.*)',
handler: 'cacheFirst',
options: {
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern : 'https://CDN-Url/(.*)',
handler: 'cacheFirst',
options: {
cacheableResponse: {
statuses: [0, 200]
}
}
}],
directoryIndex: 'index.shtml',
maximumFileSizeToCacheInBytes: 3000000,
globIgnores: ['sw.js']
})
.then(() => {
console.log('Service worker generated.');
})
.catch((err) => {
console.log('[ERROR] This happened: ' + err);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment