@@ -39,6 +39,11 @@ public void InitializeStreamingRenderingFraming(HttpContext httpContext, bool is
39
39
40
40
public async Task SendStreamingUpdatesAsync ( HttpContext httpContext , Task untilTaskCompleted , TextWriter writer )
41
41
{
42
+ // Important: do not introduce any 'await' statements in this method above the point where we write
43
+ // the SSR framing markers, otherwise batches may be emitted before the framing makers, and then the
44
+ // response would be invalid. See the comment below indicating the point where we intentionally yield
45
+ // the sync context to allow SSR batches to begin being emitted.
46
+
42
47
SetHttpContext ( httpContext ) ;
43
48
44
49
if ( _streamingUpdatesWriter is not null )
@@ -56,9 +61,11 @@ public async Task SendStreamingUpdatesAsync(HttpContext httpContext, Task untilT
56
61
57
62
try
58
63
{
59
- await writer . WriteAsync ( _ssrFramingCommentMarkup ) ;
60
- await EmitInitializersIfNecessary ( httpContext , writer ) ;
61
- await writer . FlushAsync ( ) ; // Make sure the initial HTML was sent
64
+ writer . Write ( _ssrFramingCommentMarkup ) ;
65
+ EmitInitializersIfNecessary ( httpContext , writer ) ;
66
+
67
+ // At this point we yield the sync context. SSR batches may then be emitted at any time.
68
+ await writer . FlushAsync ( ) ;
62
69
await untilTaskCompleted ;
63
70
}
64
71
catch ( NavigationException navigationException )
@@ -77,15 +84,15 @@ public async Task SendStreamingUpdatesAsync(HttpContext httpContext, Task untilT
77
84
}
78
85
}
79
86
80
- internal async Task EmitInitializersIfNecessary ( HttpContext httpContext , TextWriter writer )
87
+ internal void EmitInitializersIfNecessary ( HttpContext httpContext , TextWriter writer )
81
88
{
82
89
if ( _options . JavaScriptInitializers != null &&
83
90
! IsProgressivelyEnhancedNavigation ( httpContext . Request ) )
84
91
{
85
92
var initializersBase64 = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( _options . JavaScriptInitializers ) ) ;
86
- await writer . WriteAsync ( "<!--Blazor-Web-Initializers:" ) ;
87
- await writer . WriteAsync ( initializersBase64 ) ;
88
- await writer . WriteAsync ( "-->" ) ;
93
+ writer . Write ( "<!--Blazor-Web-Initializers:" ) ;
94
+ writer . Write ( initializersBase64 ) ;
95
+ writer . Write ( "-->" ) ;
89
96
}
90
97
}
91
98
0 commit comments