See the service-worker/basic of GoogleChrome/samples: A repo containing samples tied to new functionality in each release of Google Chrome.
Use workbox to do it easily.
Notes linking here
- difference between web workers vs service workers vs worklets
- Making PWAs work offline with Service workers
- simulate http server using service workers
- using a service worker (web worker) instead