Skip to content

Commit 3314141

Browse files
committed
Use build-time environment detection to eliminate WASM 404 errors
1 parent 90110c6 commit 3314141

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

src/js/16-bloblang-interactive.js

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,15 @@
624624
wasmLoading = true;
625625

626626
// WASM path varies by build type:
627-
// - Production/Netlify: /blobl.wasm (site root) - try first to avoid 404 in console
627+
// - Production/Netlify: /blobl.wasm (site root)
628628
// - UI Preview (local dev): /_/blobl.wasm
629+
// Use isUiPreview variable set in head-scripts.hbs to determine correct path
629630
const rootPath = typeof uiRootPath !== 'undefined' ? uiRootPath : '/_';
630631
const siteRoot = typeof siteRootPath !== 'undefined' ? siteRootPath : '';
631-
const wasmPaths = [
632-
siteRoot + '/blobl.wasm',
633-
rootPath + '/blobl.wasm'
634-
];
632+
const isPreview = typeof isUiPreview !== 'undefined' ? isUiPreview : false;
633+
634+
// Select the correct path based on environment - no fallback to avoid 404s
635+
const wasmPath = isPreview ? rootPath + '/blobl.wasm' : siteRoot + '/blobl.wasm';
635636

636637
wasmLoadPromise = new Promise((resolve, reject) => {
637638
loadRequiredScripts()
@@ -641,41 +642,26 @@
641642
return;
642643
}
643644

644-
// Try each path until one works
645-
function tryLoadWasm(paths) {
646-
if (paths.length === 0) {
647-
reject(new Error('WASM file not found'));
648-
return;
649-
}
650-
651-
const wasmPath = paths[0];
652-
const go = new Go();
653-
654-
fetch(wasmPath)
655-
.then((response) => {
656-
if (!response.ok) {
657-
throw new Error('HTTP ' + response.status);
658-
}
659-
const responseClone = response.clone();
660-
return WebAssembly.instantiateStreaming(response, go.importObject)
661-
.catch(async () => {
662-
const bytes = await responseClone.arrayBuffer();
663-
return WebAssembly.instantiate(bytes, go.importObject);
664-
});
665-
})
666-
.then((result) => {
667-
go.run(result.instance);
668-
wasmLoaded = true;
669-
wasmLoading = false;
670-
resolve();
671-
})
672-
.catch(() => {
673-
// Try next path
674-
tryLoadWasm(paths.slice(1));
675-
});
676-
}
677-
678-
tryLoadWasm(wasmPaths);
645+
const go = new Go();
646+
fetch(wasmPath)
647+
.then((response) => {
648+
if (!response.ok) {
649+
throw new Error('WASM file not found at ' + wasmPath);
650+
}
651+
const responseClone = response.clone();
652+
return WebAssembly.instantiateStreaming(response, go.importObject)
653+
.catch(async () => {
654+
const bytes = await responseClone.arrayBuffer();
655+
return WebAssembly.instantiate(bytes, go.importObject);
656+
});
657+
})
658+
.then((result) => {
659+
go.run(result.instance);
660+
wasmLoaded = true;
661+
wasmLoading = false;
662+
resolve();
663+
})
664+
.catch(reject);
679665
})
680666
.catch(reject);
681667
});

src/partials/head-scripts.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
145145
{{/if}}
146146
<script>
147147
var uiRootPath="{{{uiRootPath}}}"
148+
var isUiPreview={{#if (eq site.title 'UI Preview')}}true{{else}}false{{/if}}
148149
</script>
149150
{{#if (or (eq page.attributes.role 'bloblang-playground')(eq page.attributes.role 'bloblang-snippets') (eq page.attributes.role 'enable-ace-editor'))}}
150151
<script defer src="{{{uiRootPath}}}/js/vendor/ace/ace.js"></script>

0 commit comments

Comments
 (0)