Can a WebWorker access the localStorage?
If not why not? Is it problematic from a security stand point?
With the localStorage property, we can save the data into the browser. You can retrieve that data for the same or for any other web page.
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.
It's simple. Just go to the developer tools by pressing F12 , then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.
Note: TWA shares the browser data like cookies and localStorage inside app, so if you are logged in inside a browser then you'll automatically be logged in TWA app as well.
Web workers only have access to the following:
XMLHttpRequest
navigator
objectlocation
objectsetTimeout
methodclearTimeout
methodsetInterval
methodclearInterval
methodPerformance
object (mark
,measure
,now
methods: caniuse?
)IndexedDB
API (see: caniuse?
)importScripts
methodJSON
Worker
The window or parent objects are not accessible from a Web worker therefore you can't access the localStorage
.
To communicate between window and the workerglobalscope
you may use postMessage()
function and onmessage
event.
Accessing the DOM and window would not be thread safe, since the child thread would have the same privileges as its parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With