简体   繁体   English

如何配置 Create React Apps 默认 Service Worker 以缓存不在 react src 上下文(公共文件夹)中的资产?

[英]How to configre Create React Apps default Service Worker to cache assets that are not in react src Context (public Folder)?

today I tried the ServiceWorker of Create React App and everything is cached except the Favicon of my Page which is defined in public/img/favicon.ico, so when the Page is Offline the Favicon disapears and Connection Error is displayed in Console, so does anyone has experience how to configure webpage Plugin of the Service worker to cache this too?今天我尝试了 Create React App 的 ServiceWorker,除了在 public/img/favicon.ico 中定义的我的页面的 Favicon 之外,所有内容都被缓存,所以当页面脱机时,Favicon 消失并且连接错误显示在控制台中,也是如此任何人都有经验如何配置服务工作者的网页插件来缓存这个?

I have a CRA project here .在这里有一个 CRA 项目。 I don't precache my favicon, I let a default handler runtime cache it, but I do precache a fallback image and you should be able to do the same to precache your favicon.我不预先缓存我的网站图标,我让默认处理程序运行时缓存它,但我会预先缓存一个后备图像,你应该能够这样做来预先缓存你的网站图标。

CRA creates a default sw which I don't think I touched. CRA 创建了一个我认为我没有触及的默认软件。 To get more control I use workbox in a sw-addendum file that I append to the default sw in the build.为了获得更多控制权,我在 sw-addendum 文件中使用了工作箱,我将 append 设置为构建中的默认 sw。 In the addendum you can precache what you need and set up routes, etc.在附录中,您可以预先缓存您需要的内容并设置路线等。

see package.json for postdeploy script有关部署后脚本,请参阅package.json

"postdeploy": "cp src/face.69232788.jpg docs/face.69232788.jpg && cat src/swAddendum.js >> docs/service-worker.js",

sw-addendum here sw-附录在这里

workbox.precaching.precache([
  './face.69232788.jpg',
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM