我需要从 serviceworker 内部访问 localstorage 或 cookie [英] I need to access localstorage or cookie from within the serviceworker

查看:39
本文介绍了我需要从 serviceworker 内部访问 localstorage 或 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在推送事件之后进行一次提取调用,以通过内部 api 获取通知数据,其中包含存储在本地存储或 cookie 中的用户特定参数,例如用户类型或国家/地区 ID .. 我该怎么做?

I wish to make a fetch call after the push event for fetching the notif data through an internal api with user specific params which are stored in the localstorage or cookie such as usertype or country id .. how do i do this ?

推荐答案

您不能在 Service Worker 中使用本地存储.决定服务工作者不应访问任何同步 API.您可以改用 IndexedDB,或使用 postMessage() 与受控页面进行通信.

You cannot use Local Storage in service workers. It was decided that service workers should not have access to any synchronous APIs. You can use IndexedDB instead, or communicate with the controlled page using postMessage().

默认情况下,fetch 请求中不包含 cookie,但您可以按如下方式包含它们:fetch(url, {credentials: 'include'}).

By default, cookies are not included with fetch requests, but you can include them as follows: fetch(url, {credentials: 'include'}).

这篇关于我需要从 serviceworker 内部访问 localstorage 或 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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