简体   繁体   English

使用postMessage在不同域的iframe中进行本地存储

[英]localstorage in iframe of different domain using postMessage

My question has to do with the performance setbacks of using localStorage and a possible workaround. 我的问题与使用localStorage性能挫折和可能的解决方法有关。 As I understand it (and I'm not too sure about this), as soon as a reference to localStorage is seen on a page (compile time?) it blocks the thread and reads the data from disk to populate the localStorage memory for reference. 据我了解(我对此不太确定),只要在页面上看到对localStorage的引用(编译时?),它将阻塞线程并从磁盘读取数据以填充localStorage内存以供参考。 Or maybe it looks for data on disk if the domain has stored data in the past and so it knows localstorage is used there already. 或者,如果域过去已存储数据,那么它可能会在磁盘上寻找数据,因此它知道本地存储已在此处使用。

In either case, since the localstorage api is synchronous it blocks the thread to read from disk leaving us to wait for the data to be read before the page can do anything else. 在这两种情况下,由于localstorage api是同步的,因此它将阻止线程从磁盘读取数据,从而使我们在页面可以执行任何其他操作之前等待数据被读取。 Say we insert an iframe into the page, which is dynamically loaded with a different domain as its source. 假设我们将一个iframe插入到页面中,该页面会动态加载另一个域作为其源。 If we use this iframe to do all the localstorage reading and writing via postMessage as described in this post , wouldn't the thread only be blocked as soon as we load the iframe? 如果我们用这个iframe来完成所有通过的postMessage的localStorage的阅读和写作中描述的这个帖子 ,就不会线程只能阻止只要我们加载的iframe? Does this give us an asynchronous method of using localstorage? 这是否为我们提供了使用本地存储的异步方法? Or am I totally off? 还是我完全离开?

Since every window and iframe runs in a separate browser execution context (ie they all execute concurrently) -- then yes, this should will you async localStorage via postMessage. 由于每个窗口和iframe都在单独的浏览器执行上下文中运行(即它们都同时执行),因此可以,您应该通过postMessage异步localStorage。 Obviously, loading the iframe from a different domain versus the same domain may have impact on the security of the system. 显然,从不同的域和相同的域加载iframe可能会影响系统的安全性。 While it is true that postMessage works cross-domain, I'd rather load this iframe from a domain I completely trusted (ie. my own domain). 尽管postMessage确实可以跨域工作,但我宁愿从我完全信任的域(即我自己的域)中加载此iframe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM