在来自不同域的iframe中运行Meteor应用程序

Chr*_*itz 17 iframe meteor

经过数周的工作,我现在终于准备好部署我的应用程序,以发现流星似乎没有在iframe中运行.当顶级窗口和iframe在同一个域上运行时,它可以正常工作,但在域不同时则不行.我在Chrome上遇到的错误是:

Uncaught SecurityError: Access to 'sessionStorage' is denied for this document. 
Run Code Online (Sandbox Code Playgroud)

在那个错误之后,Meteor的初始化似乎停止了,甚至连Meteor自身都没有被定义.

经过一番挖掘,我找到了这个参考和解释:http://www.w3.org/TR/webstorage/#user-tracking :" 阻止第三方存储 用户代理可能会限制对localStorage对象的访问,因为脚本来自于浏览上下文的顶级文档的域,例如拒绝访问在iframe中运行的其他域中的页面的API."

问题不是我的应用程序特有的.您可以使用流星图库中的任何演示应用程序并尝试将它们嵌入到具有iframe的另一个页面中,您将看到我的意思.

有没有办法解决?

编辑2014-01-07: 我已经尝试包装了一些在try-catch块中抛出异常的地方,但是给人的印象是,这太过于使流星瘫痪,以至于由于其他原因而无法正确初始化.

Sui*_*oth 13

我无法重现您遇到的具体问题,但以下是为了使其正常工作所遵循的步骤.也许我们可以从那里解构它并弄清楚错误是什么.

我创建了一个空白应用程序

流星创建testapp

然后我添加了浏览器策略模块:

meteor添加浏览器策略

然后我编辑了testapp.js以使其使用Session:

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return Session.get( 'welcome_text' );
  };

  Template.hello.events({
    'click input' : function () {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
        Session.set( 'welcome_text', 'Hello: ' + Math.random() );
    }
  });
}

if (Meteor.isServer) {

  BrowserPolicy.framing.allowAll();

  Meteor.startup(function () {
    // code to run on server at startup
  });
}
Run Code Online (Sandbox Code Playgroud)

我创建了两个nginx虚拟主机:

server {

    listen 80;
    server_name test1.com;

    location / {
        proxy_pass http://127.0.0.1:3000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}


server {

    listen 80;
    server_name test2.com;

    location / {
        alias /websites/test2.com
    }

}
Run Code Online (Sandbox Code Playgroud)

test1是meteor app,test2是iframe.

对于test2,我创建了/websites/test2/index.html:

<iframe src="http://test1.com">
Run Code Online (Sandbox Code Playgroud)

然后我将test1.com和test2.com添加到我的hosts文件并启动了meteor应用程序.

我在浏览器上访问了http://test2.com,一切都按预期工作.看起来你或多或少尝试过这些步骤,但是你的应用程序或服务器堆栈可能有其他组件干扰了浏览器策略.

我首先更新您的问题,以包含流星应用程序的请求和响应标头.你的流星应用程序是否在代理服务器后面运行?

  • 那个:这看起来很傻,但你是对的!这不是问题.鉴于您尝试重现它的失败,我回去检查它是否与浏览器中的安全设置有关.当我使用REST API和cookie创建解决方法时,我注意到我的浏览器默认阻止了第三方cookie.这看起来也被解释为阻止访问本地和sessionStorage(即使Chrome设置中没有说明).非常感谢你花时间尝试重现它.显然你是第一个.应得的! (2认同)