Skip to content

Instantly share code, notes, and snippets.

@huang-xiao-jian
Created June 1, 2018 13:31
Show Gist options
  • Save huang-xiao-jian/b5605a02d5c72d49121c64ddb40b9090 to your computer and use it in GitHub Desktop.
Save huang-xiao-jian/b5605a02d5c72d49121c64ddb40b9090 to your computer and use it in GitHub Desktop.
配置 workbox PWA
/**
* @description - service worker strategy
* @author - huang.jian <hjj491229492@hotmail.com>
*/
/* eslint-env worker */
/* global workbox */
// import workbox library
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js'
);
workbox.setConfig({
debug: true,
});
workbox.core.setCacheNameDetails({
prefix: 'babycherry',
suffix: 'v1',
});
workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug);
// route strategy
workbox.routing.registerRoute(
/^https:\/\/cdn\.bootcss\.com./,
workbox.strategies.staleWhileRevalidate({
cacheName: 'CDN'
}),
);
workbox.routing.registerNavigationRoute('/index.html', {
whitelist: [/^\/pwa/],
blacklist: [
/\.(js|css|jpe?g|png|gif|pdf)$/,
]
});
workbox.precaching.precacheAndRoute([]);
/**
* @description - workbox configuration for PWA
* @author - huang.jian <hjj491229492@hotmail.com>
*/
module.exports = {
swSrc: 'public/sw.js',
swDest: 'dist/client/sw.js',
globDirectory: 'dist/client',
globPatterns: ['**/*.{png,ico,html,json,jpg,js,css}'],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment