From 296435e40c0053e80fb75ab3e1be92c8c3b9c012 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 16 Nov 2017 09:34:18 +0000 Subject: [PATCH] When capturing prerendering template, avoid problems with HTTP compression --- .../Prerendering/SpaPrerenderingExtensions.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs b/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs index dfb6a17..b223d46 100644 --- a/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs +++ b/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs @@ -94,6 +94,11 @@ namespace Microsoft.AspNetCore.Builder // HTML content so it can be passed as a template to the prerenderer. RemoveConditionalRequestHeaders(context.Request); + // Make sure we're not capturing compressed content, because then we'd have + // to decompress it. Since this sub-request isn't leaving the machine, there's + // little to no benefit in having compression on it. + var originalAcceptEncodingValue = GetAndRemoveAcceptEncodingHeader(context.Request); + // Capture the non-prerendered responses, which in production will typically only // be returning the default SPA index.html page (because other resources will be // served statically from disk). We will use this as a template in which to inject @@ -111,6 +116,11 @@ namespace Microsoft.AspNetCore.Builder finally { context.Response.Body = originalResponseStream; + + if (!string.IsNullOrEmpty(originalAcceptEncodingValue)) + { + context.Request.Headers[HeaderNames.AcceptEncoding] = originalAcceptEncodingValue; + } } // If it isn't an HTML page that we can use as the template for prerendering, @@ -181,6 +191,20 @@ namespace Microsoft.AspNetCore.Builder request.Headers.Remove(HeaderNames.IfRange); } + private static string GetAndRemoveAcceptEncodingHeader(HttpRequest request) + { + var headers = request.Headers; + var value = (string)null; + + if (headers.ContainsKey(HeaderNames.AcceptEncoding)) + { + value = headers[HeaderNames.AcceptEncoding]; + headers.Remove(HeaderNames.AcceptEncoding); + } + + return value; + } + private static (string, string) GetUnencodedUrlAndPathQuery(HttpContext httpContext) { // This is a duplicate of code from Prerenderer.cs in the SpaServices package.