简体   繁体   English

create-react-app 的最佳服务工作者策略是什么?

[英]What is the best service worker strategy for create-react-app?

Every code update to the production build shows a white screen to the user until they refresh the page.生产版本的每次代码更新都会向用户显示一个白屏,直到他们刷新页面。 What is the best service worker strategy for code updates to be pushed seamlessly to the user?将代码更新无缝推送给用户的最佳服务工作者策略是什么? (no page refresh or cache clearing necessary) (无需页面刷新或缓存清除)

The default service worker behavior is not ideal.默认的 Service Worker 行为并不理想。 It seems that every production code update causes the browser to show a white screen until the user manually refreshes the page.似乎每次生产代码更新都会导致浏览器显示白屏,直到用户手动刷新页面。 I understand this may require something along the lines of sw-precache and implementing workbox for custom service worker functionality - that is all fine, and I've tried that out and have gotten those libraries running and working to the point that It still mimics the exact same behavior (so I think the project is essentially ready for the correct service worker to be implemented without ejecting).我知道这可能需要类似于 sw-precache 和为自定义 Service Worker 功能实现工作箱的东西 - 这一切都很好,我已经尝试过了,并且已经让这些库运行并工作到它仍然模仿完全相同的行为(所以我认为该项目基本上已经准备好在不弹出的情况下实施正确的服务工作者)。 However, I'm still lost as to what is the best caching strategies/service worker to use to achieve my desired result?但是,我仍然不知道用于实现我想要的结果的最佳缓存策略/服务工作者是什么?

Heyy Timothy!嘿蒂莫西!

I think you already found the answer to your question by now.我想您现在已经找到了问题的答案。 I hope this answer will help future developers in their journey.我希望这个答案能帮助未来的开发人员在他们的旅程中。

The strategies you are referring to are called caching strategies.您所指的策略称为缓存策略。 Here's a list of commonly used ones:以下是常用的列表:

  • networkOnly – only fetch from the network networkOnly – 仅从networkOnly获取
  • cacheOnly – only fetch from cache cacheOnly – 仅从缓存中获取
  • fastest – fetch from both, and respond with whichever comes first fastest - 从两者中获取,并以先到者为准
  • cacheFirst – fetch from the cache, but also fetch from network and update cache cacheFirst – 从缓存中获取,但也从网络中获取并更新缓存

By default, CRA's service worker uses the cache-first strategy.默认情况下,CRA 的 service worker 使用cache-first策略。

In your case, you have the possibility to choose between 2 strategies:在您的情况下,您可以在两种策略之间进行选择:

  • networkOnly Strategy networkOnly策略

Pros: Will always render the latest version of the app (No white screen)优点:将始终呈现最新版本的应用程序(无白屏)

Cons: This will cause the user to fetch the app from the network every time.缺点:这将导致用户每次都从网络中获取应用程序。

  • networkFirst Strategy networkFirst策略

This strategy can be used in your case.此策略可用于您的情况。 You can set your SW to look out for a version update.您可以设置您的软件以查找版本更新。 If a new version is found, you can display a small toaster saying 'Hey there, a new version is available please click here to refresh'如果找到新版本,您可以显示一个小烤面包机,上面写着“嘿,有新版本可用,请单击此处刷新”


You can also check the following links for more information:您还可以查看以下链接以获取更多信息:

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

相关问题 在create-react-app项目中注销服务工作者的后果是什么? - What are the consequences of unregistering the service worker in create-react-app project? Service Worker无法使用create-react-app - Service Worker not working with create-react-app 没有使用 create-react-app 获得服务人员 - Not getting service worker with create-react-app create-react-app 使用自定义 service worker 而不弹出 - create-react-app use custom service worker without ejecting 在 create-react-app service worker 中缓存跨域 API 调用 - Caching cross-origin API calls in create-react-app service worker 从 create-react-app 中的 service worker 缓存中排除 index.html - exclude index.html from service worker cache in create-react-app create-react-app:无法安装站点:未检测到匹配的 Service Worker - create-react-app: Site cannot be installed: no matching service worker detected 如何在 cra-template-pwa 模板 create-react-app 应用程序中正确使用 Workbox 文件 service-worker.js? - How to use the Workbox file service-worker.js correctly in a cra-template-pwa template create-react-app app? 什么是webpack.DefinePlugin的create-react-app的好选择? - What is a good alternative to webpack.DefinePlugin for create-react-app? 使用 create-react-app 时 .babelrc 文件的位置是什么? - What is the location of .babelrc file when using create-react-app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM