Skip to content

Commit 4fdc5d2

Browse files
committed
Fix asset URLs for subpath deployments
1 parent 85aaabe commit 4fdc5d2

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/cli/generators.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ namespace webcc
10281028

10291029
void generate_html(const std::string &out_dir, const std::string &template_path)
10301030
{
1031-
const std::string script_tag = " <script src=\"/app.js\"></script>";
1031+
const std::string script_tag = " <script src=\"./app.js\"></script>";
10321032
std::string html;
10331033

10341034
// Try to find a custom template

src/cli/js_templates.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const supportsStreaming = () => {
1717
};
1818
1919
const run = async () => {
20+
const scriptSrc = document.currentScript && document.currentScript.src;
21+
const assetBase = new URL('.', scriptSrc || window.location.href);
22+
const wasmUrl = new URL('app.wasm', assetBase);
23+
2024
const imports = {
2125
env: {
2226
// C++ calls this function to tell JS "I wrote commands, please execute them"
@@ -35,9 +39,9 @@ const run = async () => {
3539
3640
let mod;
3741
if (supportsStreaming()) {
38-
mod = await WebAssembly.instantiateStreaming(fetch('/app.wasm'), imports);
42+
mod = await WebAssembly.instantiateStreaming(fetch(wasmUrl), imports);
3943
} else {
40-
const response = await fetch('/app.wasm');
44+
const response = await fetch(wasmUrl);
4145
const bytes = await response.arrayBuffer();
4246
mod = await WebAssembly.instantiate(bytes, imports);
4347
}

0 commit comments

Comments
 (0)