简体   繁体   English

Web - 周期性同步和同步有什么区别?

[英]Web - what is the differences between periodic sync and sync?

I came across two different types of sync in the background for PWAs sync and periodic sync .我在 PWA同步周期性同步的后台遇到了两种不同类型的同步 there are not many resources for them and existing resources do not explain enough with sample working codes.他们的资源并不多,现有资源无法用示例工作代码进行足够的解释。

so my main question is: are there any other logical differences between them other than frequency?所以我的主要问题是:除了频率之外,它们之间还有其他逻辑差异吗?

and my side question is: are they handling requests by themselves?我的问题是:他们自己处理请求吗? I'm asking this because I want something more flexible, I mean I'm managing offline and online situations and saving data in IDB them I'm offline and I just need a background process to get my offline data from my custom IDB and send them to the server.我问这个是因为我想要更灵活的东西,我的意思是我正在管理离线和在线情况并将数据保存在IDB中我离线,我只需要一个后台进程来从我的自定义 IDB 获取我的离线数据并发送他们到服务器。

Here's a few use cases that can help illustrate the difference.以下是一些可以帮助说明差异的用例。 Also keep in mind that as of Feb. 2021, the Background Sync is only available in Chrome and Chromium-based browsers, and Periodic Background Sync is only available in Chrome after a progressive web app has been installed .另请注意,自 2021 年 2 月起, 后台同步仅在 Chrome 和基于 Chromium 的浏览器中可用,定期后台同步仅在安装渐进式 web 应用程序后在 Chrome 中可用。

Background Sync后台同步

The use case is retrying a failed update/upload operation (usually a POST or a PUT ) "in the background" at a regular interval, until it succeeds.用例是定期“在后台”重试失败的更新/上传操作(通常是POSTPUT ),直到成功。 You could imagine, for instance, trying to upload a new photo to a social media site, but your network connection is down.例如,您可以想象尝试将新照片上传到社交媒体网站,但您的网络连接已断开。 As a user, you'd want that upload retried at some point in the future.作为用户,您希望在将来的某个时间重试该上传。

The API only provides the mechanism for triggering an opportunity to re-attempt the network operation, via a sync event in the web app's service worker. API 仅提供通过 web 应用程序的服务工作线程中的sync事件触发重新尝试网络操作的机会的机制。 It's up to a developer to store information about the failed request (usually in IndexedDB) and actually resend it, and indicate whether the sync was successful or if it failed again.由开发人员存储有关失败请求的信息(通常在 IndexedDB 中)并实际重新发送它,并指示sync是成功还是再次失败。

(The workbox-background-sync library can help with the implementation details, if you'd rather not deal with everything yourself.) (如果您不想自己处理所有事情, workbox-background-sync可以帮助您处理实现细节。)

Periodic Background Sync定期后台同步

The use case is refreshing caches "in the background" so that the next time a user opens your web app, the data is fresher than it otherwise would be.用例是“在后台”刷新缓存,以便用户下次打开您的 web 应用程序时,数据会比其他情况下更新鲜。 You could imagine an installed news progressive web app using periodic background sync to update its cache of top headlines each morning.您可以想象一个已安装的新闻渐进式 web 应用程序使用定期后台同步来每天早上更新其头条新闻缓存。

Under the hood, this works by invoking a periodicsync event in your service worker, and inside that event handler, you'd normally make a GET request to update something stored in the Cache Storage API or IndexedDB.在后台,这是通过在服务工作者中调用periodicsync同步事件来实现的,在该事件处理程序中,您通常会发出GET请求来更新存储在缓存存储 API 或 IndexedDB 中的内容。

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

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