Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer: 错误:无法加载模块。无法加载所需模块 'dotnet.wasm'。
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer: Error: Failed to load module. The required module 'dotnet.wasm' could not be loaded.
ID: dotnet/blazor-wasm-prerendering-chunk-load-failure
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| .NET 8.0 | active | — | — | — |
| Microsoft.AspNetCore.Components.WebAssembly 8.0.0 | active | — | — | — |
| Blazor WebAssembly 8.0 | active | — | — | — |
根因分析
当浏览器由于静态文件服务配置错误、缺少 MIME 类型或初始加载期间网络问题而无法获取 'dotnet.wasm' 时,Blazor WebAssembly 预渲染失败。
English
Blazor WebAssembly prerendering fails when the browser cannot fetch 'dotnet.wasm' due to incorrect static file serving, missing MIME types, or network issues during initial load.
官方文档
https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-8.0#configure-the-server解决方案
-
确保服务器为 .wasm 文件设置正确的 MIME 类型:在 IIS 中添加 'application/wasm' MIME 类型;在 Kestrel 中自动处理,但通过浏览器 DevTools 网络选项卡验证。同时检查是否在端点路由之前调用了 'app.UseStaticFiles()'。
-
如果使用 CDN 或反向代理,确保其透传 WebAssembly 文件。在 Apache 中添加重写规则:'AddType application/wasm .wasm',或在 Nginx 中添加 'application/wasm' MIME 映射。
无效尝试
常见但无效的做法:
-
65% 失败
Adding 'dotnet.wasm' to a custom bundler without proper WebAssembly MIME type mapping causes the browser to refuse the file.
-
80% 失败
Deleting the 'bin' and 'obj' folders and rebuilding may fix stale files but not underlying configuration issues like MIME types.
-
50% 失败
Disabling prerendering entirely (e.g., removing 'RenderMode.InteractiveWebAssembly') avoids the symptom but loses SEO and performance benefits.