@@ -29,6 +29,26 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati
2929
3030 app . UseWebAssemblyDebugging ( ) ;
3131
32+ bool applyCopHeaders = configuration . GetValue < bool > ( "ApplyCopHeaders" ) ;
33+
34+ if ( applyCopHeaders )
35+ {
36+ app . Use ( async ( ctx , next ) =>
37+ {
38+ if ( ctx . Request . Path . StartsWithSegments ( "/_framework" ) && ! ctx . Request . Path . StartsWithSegments ( "/_framework/blazor.server.js" ) && ! ctx . Request . Path . StartsWithSegments ( "/_framework/blazor.web.js" ) )
39+ {
40+ string fileExtension = Path . GetExtension ( ctx . Request . Path ) ;
41+ if ( string . Equals ( fileExtension , ".js" ) )
42+ {
43+ // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
44+ ApplyCrossOriginPolicyHeaders ( ctx ) ;
45+ }
46+ }
47+
48+ await next ( ctx ) ;
49+ } ) ;
50+ }
51+
3252 app . UseBlazorFrameworkFiles ( ) ;
3353 app . UseStaticFiles ( new StaticFileOptions
3454 {
@@ -41,7 +61,17 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati
4161
4262 app . UseEndpoints ( endpoints =>
4363 {
44- endpoints . MapFallbackToFile ( "index.html" ) ;
64+ endpoints . MapFallbackToFile ( "index.html" , new StaticFileOptions
65+ {
66+ OnPrepareResponse = fileContext =>
67+ {
68+ if ( applyCopHeaders )
69+ {
70+ // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
71+ ApplyCrossOriginPolicyHeaders ( fileContext . Context ) ;
72+ }
73+ }
74+ } ) ;
4575 } ) ;
4676 }
4777
@@ -69,4 +99,10 @@ private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfigura
6999 } ) ;
70100 }
71101 }
102+
103+ private static void ApplyCrossOriginPolicyHeaders ( HttpContext httpContext )
104+ {
105+ httpContext . Response . Headers [ "Cross-Origin-Embedder-Policy" ] = "require-corp" ;
106+ httpContext . Response . Headers [ "Cross-Origin-Opener-Policy" ] = "same-origin" ;
107+ }
72108}
0 commit comments