Is it possible to intercept and cache WebSocket messages in a Service Worker like all the examples do for normal HTTP requests?

前端 未结 2 1858
悲哀的现实
悲哀的现实 2021-01-17 09:27

I know you can create WebSocket connections from within a Service Worker itself; my question is more whether or not you can use a WebSocket from your app as normal and have

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 09:57

    I think one should use fetch for the first load of the page, so its data can be cached and returned by the service worker when you're offline.

    So you initiate the websocket connection only if you're online. No need to cache data then.

    And you fallback in saving message in cache in the app side when it fails to send via socket.

提交回复
热议问题