Add more service-worker functionality with create-react-app

后端 未结 2 1216
抹茶落季
抹茶落季 2021-02-12 21:40

So create-react-app includes service worker functionality by default, which is lovely - all my static assets get cached for offline use out of the box. Cool, but now I want to t

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-12 21:58

    I had the same problem and without doing eject, I could replace the default service-worker with workbox-generated one to add runtime caching webfonts, api calls, etc.

    1. install workbox-build
    2. prepare src/sw-template.js to add your registerRoute calls
    3. prepare /build-sw.js to copy workbox file to 'build' folder and injectManifest
    4. modify package.json scripts to run above build-sw.js
    5. modify src/registerServiceWorker.js to replace the default one

    You can find detailed file changes here.

提交回复
热议问题