@scotty_walker
To build a service worker with webpack, follow these steps:
1
|
npm install workbox-webpack-plugin --save-dev |
1 2 3 4 5 6 7 8 9 10 11 12 |
const { GenerateSW } = require('workbox-webpack-plugin'); module.exports = { // other webpack configuration options... plugins: [ new GenerateSW({ clientsClaim: true, skipWaiting: true, }), ], }; |
1 2 3 4 5 6 7 8 9 10 11 |
if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js') .then((registration) => { console.log('Service worker registered: ', registration); }) .catch((registrationError) => { console.log('Service worker registration failed: ', registrationError); }); }); } |
That's it! You have successfully built a service worker with webpack.