Skip to content

Commit a731f81

Browse files
committed
fetch wasm file should append baseUrl base on currenScript running env
1 parent f9718b9 commit a731f81

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/wasm-helper.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ const wasmHelper = async (opts = {}, url: string) => {
3030
// a lot of static file servers, so we just work around it by getting the
3131
// raw buffer.
3232
// @ts-ignore
33-
const response = await fetch(url);
33+
const getBaseUrl = () => {
34+
const currentScriptUrl = getRunningScript()()
35+
if (!currentScriptUrl) return ''
36+
return new URL(currentScriptUrl).origin
37+
}
38+
const getRunningScript = ()=>{
39+
return ()=>{
40+
return new Error().stack.match(/([^ \n])*([a-z]*:\/\/\/?)*?[a-z0-9\/\\]*\.js/ig)?.[0]
41+
}
42+
}
43+
const baseUrl = typeof window !== 'undefined' ? getBaseUrl() : ''
44+
const response = await fetch(baseUrl + url);
3445
const contentType = response.headers.get("Content-Type") || "";
3546
if ("instantiateStreaming" in WebAssembly && contentType.startsWith("application/wasm")) {
3647
result = await WebAssembly.instantiateStreaming(response, opts);

0 commit comments

Comments
 (0)