V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
seanwhy
V2EX  ›  程序员

基于 Webassembly 的发布问题

  •  
  •   seanwhy · 19 天前 · 1223 次点击
    大家好,我又来了,基于我上个帖子,和同事们攻关这几个月,总算把 c++端图形程序移植到浏览器上了,运行效率还挺满意,但发布时就遇到问题了。本地开发时,在 chrome 启动参数上加--enable-features=SharedArrayBuffer ,那这样浏览器运行 webassembly 模块才不会报错。别的机器访问发布的 webassembly 也必须加上这个参数才能运行。
    那问题就很明显了,有什么法子能在不修改访问机器浏览器启动参数的前提下,能正常访问我发布的 webassembly 模块网页?
    有熟悉的大佬指点一二么,不甚感谢!
    第 1 条附言  ·  19 天前
    根据几位的建议,在 nginx 添加了:
    add_header Cross-Origin-Opener-Policy same-origin;
    add_header Cross-Origin-Embedder-Policy require-corp;
    但别的机器访问报错:The Cross-Origin-Opener-Policy header has been ignored, because the URL's origin was untrustworthy. It was defined either in the final response or a redirect. Please deliver the response using the HTTPS protocol. You can also use the 'localhost' origin instead. See https://www.w3.org/TR/powerful-features/#potentially-trustworthy-origin and https://html.spec.whatwg.org/#the-cross-origin-opener-policy-header.
    这种根据资料是一定要我通过 https 协议发布,那能通过 http 协议访问么,便于部署考虑。
    7 条回复    2024-06-01 02:16:42 +08:00
    lisongeee
        1
    lisongeee  
       19 天前
    根据 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer

    需要实现安全上下文才可以使用,你给网站的响应头设置下面这些 header 了吗?

    Cross-Origin-Opener-Policy: same-origin
    Cross-Origin-Embedder-Policy: require-corp
    DOLLOR
        2
    DOLLOR  
       19 天前
    需要在页面的 HTTP 添加这两个报头
    Cross-Origin-Opener-Policy: same-origin
    Cross-Origin-Embedder-Policy: require-corp


    参见
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements
    seanwhy
        3
    seanwhy  
    OP
       19 天前
    @lisongeee 周一我试试,之前好像在 nginx 配置中加过,难道我加的不对吗,是加载服务上么
    mahaoqu
        4
    mahaoqu  
       19 天前
    https://web.dev/articles/cross-origin-isolation-guide?hl=zh-cn

    本地服务器上开启只需要在 webpack 或者 vite 的配置上加一行就行,服务器就得改 Nginx 配置
    seanwhy
        5
    seanwhy  
    OP
       19 天前
    @mahaoqu https 是必备条件么
    mahaoqu
        6
    mahaoqu  
       19 天前   ❤️ 1
    @seanwhy 安全上下文要求一定是 HTTPS 或者 localhost 。

    https://developer.mozilla.org/zh-CN/docs/Web/Security/Secure_Contexts
    flynaj
        7
    flynaj  
       19 天前 via Android
    https 是目前互联网的标配,不用纠结,测试可以用 acme.sh 申请个免费证书。
    @seanwhy
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2982 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 12:11 · PVG 20:11 · LAX 05:11 · JFK 08:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.