diff --git a/src/Components/Directory.Build.targets b/src/Components/Directory.Build.targets index 9ccefdc7fc6d..616d89e64dc0 100644 --- a/src/Components/Directory.Build.targets +++ b/src/Components/Directory.Build.targets @@ -9,7 +9,8 @@ <_BlazorDevServerPath>$(ArtifactsDir)bin/Microsoft.AspNetCore.Components.WebAssembly.DevServer/$(Configuration)/$(DefaultNetCoreTargetFramework)/blazor-devserver.dll dotnet - exec "$(_BlazorDevServerPath)" --applicationpath "$(TargetPath)" $(AdditionalRunArguments) + <_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers + exec "$(_BlazorDevServerPath)" --applicationpath "$(TargetPath)" $(_RunExtraArguments) $(AdditionalRunArguments) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Program.cs b/src/Components/WebAssembly/DevServer/src/Server/Program.cs index e06070daea4e..21eedddef02f 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Program.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Program.cs @@ -35,6 +35,7 @@ public static IHost BuildWebHost(string[] args) => ["Logging:LogLevel:Microsoft"] = "Warning", ["Logging:LogLevel:Microsoft.Hosting.Lifetime"] = "Information", [WebHostDefaults.StaticWebAssetsKey] = name, + ["ApplyCopHeaders"] = args.Contains("--apply-cop-headers").ToString() }; config.AddInMemoryCollection(inMemoryConfiguration); diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 7ce4af4f5adb..5125c59a74af 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -29,6 +29,26 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseWebAssemblyDebugging(); + bool applyCopHeaders = configuration.GetValue("ApplyCopHeaders"); + + if (applyCopHeaders) + { + app.Use(async (ctx, next) => + { + if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + { + string fileExtension = Path.GetExtension(ctx.Request.Path); + if (string.Equals(fileExtension, ".js")) + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(ctx); + } + } + + await next(ctx); + }); + } + app.UseBlazorFrameworkFiles(); app.UseStaticFiles(new StaticFileOptions { @@ -41,7 +61,17 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseEndpoints(endpoints => { - endpoints.MapFallbackToFile("index.html"); + endpoints.MapFallbackToFile("index.html", new StaticFileOptions + { + OnPrepareResponse = fileContext => + { + if (applyCopHeaders) + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(fileContext.Context); + } + } + }); }); } @@ -69,4 +99,10 @@ private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfigura }); } } + + private static void ApplyCrossOriginPolicyHeaders(HttpContext httpContext) + { + httpContext.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; + httpContext.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; + } } diff --git a/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets b/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets index 1b98a9920cf7..754d5271e341 100644 --- a/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets +++ b/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets @@ -2,6 +2,7 @@ <_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll dotnet - "$(_BlazorDevServerDll)" --applicationpath "$(TargetPath)" + <_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers + "$(_BlazorDevServerDll)" --applicationpath "$(TargetPath)" $(_RunExtraArguments)