【发布时间】:2020-10-11 19:59:29
【问题描述】:
我正在尝试托管一个网站,我使用 .wasm 文件和由 wasm-pack 工具创建的 .js 脚本。
我用 npm 和 node.js 在本地测试了项目,一切正常。
但后来我将它托管在树莓 (apache2) 上,当我尝试访问它时,出现以下错误:
Failed to load module script: The server responded with a non-JavaScript MIME type of "application/wasm". Strict MIME type checking is enforced for module scripts per HTML spec.
详情
有多个文件,但思路如下:
我的 index.html 加载模块 bootstrap.js
// bootstrap.js content
import("./index.js").catch(e => console.error("Error importing `index.js`:", e));
我的主要代码在index.js,它调用test_wasm_bg.js
最后,test_wasm_bg.js 使用这一行加载 wasm 文件:
// test_wasm_bg.js first line
import * as wasm from './test_wasm_bg.wasm';
问题出在哪里?
加载 Web 程序集文件的正确方法是什么?
【问题讨论】:
-
你不能导入wasm,。请改用 fetch。 developer.mozilla.org/en-US/docs/WebAssembly/…
-
好的,谢谢你的链接!但要使用“InstanciateStreaming”,您需要一个 ImportObjetc。那是什么?
-
ImportObjects 只是一个您使用函数等创建的对象,您希望向 wasm 模块公开。如果您不公开任何内容,您可以发送
{}。此链接可能对您有更多帮助 -> developer.mozilla.org/en-US/docs/WebAssembly/… 显示的示例也在 github -> github.com/mdn/webassembly-examples/blob/master/js-api-examples/… -
当我尝试这样做时,我收到以下错误:
Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="./test_wasm_bg.js" error: module is not an object or function -
test_wasm_bg.js,你的意思是test_wasm_bg.wasm
标签: javascript apache fetch mime-types webassembly