From 20be0a3c7259107f3ba658ef916065521bfb4a55 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 15 Aug 2019 09:37:52 -0700 Subject: [PATCH] Improve instantiateStreaming fallback This commit improves our `instantiateStreaming` fallback to only actually trigger the fallback if the headers look wrong. If the headers look right then we let through the original error which should help avoid accidentally papering over bugs with different bugs in misconfigured situations. Closes #1696 --- crates/cli-support/src/js/mod.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 9d4041135f3..cd3b4ba1415 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -536,13 +536,19 @@ impl<'a> Context<'a> { if (typeof WebAssembly.instantiateStreaming === 'function') {{ result = WebAssembly.instantiateStreaming(response, imports) .catch(e => {{ - console.warn(\"`WebAssembly.instantiateStreaming` failed. Assuming this is \ - because your server does not serve wasm with \ - `application/wasm` MIME type. Falling back to \ - `WebAssembly.instantiate` which is slower. Original \ - error:\\n\", e); return response - .then(r => r.arrayBuffer()) + .then(r => {{ + if (r.headers.get('Content-Type') != 'application/wasm') {{ + console.warn(\"`WebAssembly.instantiateStreaming` failed \ + because your server does not serve wasm with \ + `application/wasm` MIME type. Falling back to \ + `WebAssembly.instantiate` which is slower. Original \ + error:\\n\", e); + return r.arrayBuffer(); + }} else {{ + throw e; + }} + }}) .then(bytes => WebAssembly.instantiate(bytes, imports)); }}); }} else {{