Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 296435e

Browse files
When capturing prerendering template, avoid problems with HTTP compression
1 parent aeabbdc commit 296435e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs

+24
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public static void UseSpaPrerendering(
9494
// HTML content so it can be passed as a template to the prerenderer.
9595
RemoveConditionalRequestHeaders(context.Request);
9696

97+
// Make sure we're not capturing compressed content, because then we'd have
98+
// to decompress it. Since this sub-request isn't leaving the machine, there's
99+
// little to no benefit in having compression on it.
100+
var originalAcceptEncodingValue = GetAndRemoveAcceptEncodingHeader(context.Request);
101+
97102
// Capture the non-prerendered responses, which in production will typically only
98103
// be returning the default SPA index.html page (because other resources will be
99104
// served statically from disk). We will use this as a template in which to inject
@@ -111,6 +116,11 @@ public static void UseSpaPrerendering(
111116
finally
112117
{
113118
context.Response.Body = originalResponseStream;
119+
120+
if (!string.IsNullOrEmpty(originalAcceptEncodingValue))
121+
{
122+
context.Request.Headers[HeaderNames.AcceptEncoding] = originalAcceptEncodingValue;
123+
}
114124
}
115125

116126
// If it isn't an HTML page that we can use as the template for prerendering,
@@ -181,6 +191,20 @@ private static void RemoveConditionalRequestHeaders(HttpRequest request)
181191
request.Headers.Remove(HeaderNames.IfRange);
182192
}
183193

194+
private static string GetAndRemoveAcceptEncodingHeader(HttpRequest request)
195+
{
196+
var headers = request.Headers;
197+
var value = (string)null;
198+
199+
if (headers.ContainsKey(HeaderNames.AcceptEncoding))
200+
{
201+
value = headers[HeaderNames.AcceptEncoding];
202+
headers.Remove(HeaderNames.AcceptEncoding);
203+
}
204+
205+
return value;
206+
}
207+
184208
private static (string, string) GetUnencodedUrlAndPathQuery(HttpContext httpContext)
185209
{
186210
// This is a duplicate of code from Prerenderer.cs in the SpaServices package.

0 commit comments

Comments
 (0)