服务人员:离线处理请求,在线发送 [英] Service Worker: handle requests when offline to send them when online

查看:103
本文介绍了服务人员:离线处理请求,在线发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天Web应用程序,希望它可以脱机工作.为此,我使用渐进式Web应用程序功能(服务人员)来使用缓存来获取Shell应用程序和已加载的消息.

I have a chat web application and I want it to work offline. For this I use progressive web apps features (Service Workers) to use cache to get the shell app and the messages already loaded.

我想做的是能够在离线状态下发布帖子,并让服务工作者处理连接问题(即:将消息保留在某个位置,直到离线,并在我们在线后立即发送)发布消息).

What I want to do is to be able to make a post message when I'm offline and let the service worker handle the connection issues (i.e.: keep the message somewhere till where are offline and as soon as we are online send the Post message).

我想使用Service Worker,因为如果用户在发布无连接的消息后离开了Web应用程序,我也想发送消息.

I want to use Service Worker because I also want to send the message if the user as left the web app after posting a message with no connection.

为此使用的最佳API是什么?

What is the best API to use for this?

我看到了后台同步 API,但这是不是标准的,而且似乎已经有将近两年没有更新了.

I saw the background sync API but it is not standard and it doesn't seem to be updated for almost 2 years.

如果有一种方法可以使客户端(Web应用程序)完全不了解这种机制,那将很酷.

If there is a way to do this in a manner that the client (the web app) is totally unaware of this mechanism it would be cool.

我的意思是我希望我的应用只执行一个 fetch("/message", {method : "post", body : {content : "hey there"}) 然后,Service Worker会截取获取的内容,如果我们在线,那么它只会发送获取的内容,但是如果我们处于离线状态,则它等待"连接再次建立,然后发送帖子.

What I mean by that is I would like my app just do a fetch("/message", {method : "post", body : {content : "hey there"}) And then the Service Worker just intercept the fetch, if we are online then it just send the fetch, but if we are offline it "wait" for the connection to be up again and then send the post.

我想知道服务工作者中是否有可用的事件侦听器,当连接从脱机状态更改为联机状态时,该事件侦听器将被激活.这样,我应该能够在脱机时将请求存储在indexDB中,然后在联机时发送帖子. 我看到了navigator.onLine,但这不是事件:(

I wonder if there is an event listener, available in the service worker, that will be activated when the connection change from offline to online. This way I should be able to store the request in indexDB when offline and then send the post when online. I saw the navigator.onLine but it is not an event :(

推荐答案

基于此

Based from this post, you may use a Service Worker in running the app in the background either via its push event handler (triggered via an incoming push message), or via its sync event handler (triggered by an automatic replay of a task that previously failed).

您可以检查离线渐进式Web应用程序的存储文档,用于离线存储数据:

You may check the Offline Storage for Progressive Web Apps documentation for storing data offline:

  • For URL addressable resources, use the Cache API (part of service workers).
  • For all other data, use IndexedDB (with a promises wrapper).

您可以缓存静态资源,并使用Cache API组成应用程序外壳(JS/CSS/HTML文件),并填写IndexedDB中的脱机页面数据.

You can cache static resources, composing your application shell (JS/CSS/HTML files) using the Cache API and fill in the offline page data from IndexedDB.

这篇关于服务人员:离线处理请求,在线发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆