不久前我偶然发现了一篇关于此的博客文章(忘记了来源,没有为这项工作获得荣誉),这是解决这个确切问题的方法 - 检测是否支持 Webassembly,如果不支持,则重定向到“不支持浏览器” html页面。它完全按照您上面概述的方式工作。
这是要在WASM 服务工作者注册 之前添加的 JavaScript 片段,例如navigator.serviceWorker.register('service-worker.js');
:
const webassemblySupported = (function () {
try {
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
} catch (e) {
console.error('Failed to verify if webassembly is supported, assuming no.');
}
return false;
})();
// Modern browsers e.g. Edge/Chrome/Safari/Firefox/etc.
if (webassemblySupported) {
Blazor.start({});
}
// Older browsers e.g. IE11
else {
window.location = window.location + "NotSupported.html";
}