从服务工作者访问LocalStorage
我想定期调用我的服务工作者的API,以发送存储在 localstorage
的数据。当用户浏览我的网站时,将在 localstorage
中生成并保存此数据。考虑一下它之类的东西,例如在 localstorage
中保存统计信息,并通过服务工作者定期发送。我应该怎么做?我了解我无法从服务工作者访问 localstorage
,并且必须使用 postmessage
api。任何帮助将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法从Webworker进程访问LocalStorage(以及SessionStorage),结果将是
undefined
,这是出于安全原因。您需要使用
postmessage()
回到工人的原始代码,并将该代码存储在LocalStorage中。您应该使用
localstorage.setItem()
和localstorage.getItem()
从本地存储中保存并获取数据。更多信息:
worker.postmessage()
https://developer.mozilla.org/en/docs/web/api/window/localstorage“ rel =“ noreferrer”> window.localstorage
pseudo code 下面的伪代码,希望您开始:
You cannot access localStorage (and also sessionStorage) from a webworker process, they result will be
undefined
, this is for security reasons.You need to use
postMessage()
back to the Worker's originating code, and have that code store the data in localStorage.You should use
localStorage.setItem()
andlocalStorage.getItem()
to save and get data from local storage.More info:
Worker.postMessage()
Window.localStorage
Pseudo code below, hoping it gets you started:
广播频道API更容易
几种方式和控制服务工作者,但
localstorage
是不是其中之一。indexeddb
是,,但这可能是PWA的过度杀伤,一定要保持纤细。最重要的是,“ noreferrer”>广播频道API 最容易的结果。与上述
postmessage()
一起使用 MessageChannel API 。这是广播的工作方式
定义了服务工作者和客户的新广播频道。
发送在工人或客户端中发送的广播消息:
接收在工人或客户端中的广播消息:
Broadcast Channel API is easier
There are several ways to communicate between the client and the controlling service worker, but
localStorage
is not one of them.IndexedDB
is, but this might be an overkill for a PWA that by all means should remain slim.Of all means, the Broadcast Channel API results the easiest. It is by far much easier to implement than above-mentioned
postMessage()
with the MessageChannel API.Here is how broadcasting works
Define a new broadcasting channel in both the service worker and the client.
To send a broadcast message in either the worker or the client:
To receive a broadcast message in either the worker or the client:
我一直在使用此软件包,称为
localforage
,它提供了一个localstorage的接口,该接口包裹在索引中。 https://github.com/localforage/localforage/localforage然后,您可以通过将其放置在您的公共目录中来导入它,因此由您的Web服务器提供服务,然后致电:
self.importscripts('localforage.js');
在您的服务工作者中。I've been using this package called
localforage
that provides a localStorage-like interface that wraps around IndexedDB. https://github.com/localForage/localForageYou can then import it by placing it in your public directory, so it is served by your webserver, and then calling:
self.importScripts('localforage.js');
within your service worker.说
另外,这里还有一些讨论:如何使用Angular的服务工作人员访问本地存储?
https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers says
Also there is a bit of discussion here: How do I access the local storage using service workers in angular?
我本人绊倒这个问题是一个很小的WebApp项目,我考虑了以下解决方案:
当用户在线时,可以立即发送数据。当他离线时,我使用
syncevent.tag
属性将信息从客户端发送到ServiceWorker。像这样:update.php
一旦用户上网,就将新值保存到后端。这不会使服务人员访问LocalStorage,但会给他带来每一个更改。
刚开始习惯这个同步主题。因此,我真的很感兴趣,Wheather这很有帮助,并且其他人对此解决方案的看法。
Stumbling over this question myself for a tiny webapp-project, I considered the following solution:
When the user is online, the data can be sent immediately. When he is offline, I use the
SyncEvent.tag
property to send information from the client to the serviceworker. Like this:update.php
saves the new value to backend, as soon as the user goes online.This won't give the service worker access to the localStorage, but it will send him every change made.
Just starting to get used to this syncing topic. So I would really be interested, wheather this is helpful and what others think about this solution.