private static void SetCompressionMode(HttpContext context, BundlingOptions options) { // Only called when we expect to serve the body. var responseCompressionFeature = context.Features.Get <IHttpsCompressionFeature>(); if (responseCompressionFeature != null) { responseCompressionFeature.Mode = options.HttpsCompression; } }
public async Task <BundleResponse> BuildBundleAsync(Bundle bundle, IDictionary <string, string> fragments, HttpContext httpContext, BundlingOptions options) { Guard.NotNull(bundle, nameof(bundle)); Guard.NotNull(httpContext, nameof(httpContext)); Guard.NotNull(options, nameof(options)); Logger.Debug("Building bundle '{0}'.", bundle.Route); using var chronometer = _chronometer.Step($"Bundle '{bundle.Route}'"); var bundleFiles = bundle.EnumerateFiles(httpContext, options) .Where(x => x.File.Exists) .ToArray(); if (bundleFiles.Length == 0) { throw new InvalidOperationException($"The bundle '{bundle.Route}' does not contain any files."); } var context = new BundleContext { Bundle = bundle, Fragments = fragments, HttpContext = httpContext, Options = options, Files = bundleFiles }; foreach (var bundleFile in bundleFiles) { context.Content.Add(await bundle.LoadContentAsync(bundleFile)); } context.IncludedFiles.AddRange(context.Content.Select(x => x.Path)); var response = await bundle.GenerateBundleResponseAsync(context); return(response); }
private static ValueTask ServeBundleResponse(BundleResponse bundleResponse, HttpContext httpContext, BundlingOptions options) { var response = httpContext.Response; var contentHash = bundleResponse.ContentHash; response.ContentType = bundleResponse.ContentType; if (options.EnableClientCache == true) { response.Headers[HeaderNames.CacheControl] = "max-age=31536000"; // 1 year if (httpContext.Request.Query.ContainsKey("v")) { response.Headers[HeaderNames.CacheControl] += ",immutable"; } } if (contentHash.HasValue()) { response.Headers[HeaderNames.ETag] = $"\"{contentHash}\""; if (IsConditionalGet(httpContext, contentHash)) { response.StatusCode = 304; return(ValueTask.CompletedTask); } } if (bundleResponse.Content?.Length > 0) { SetCompressionMode(httpContext, options); var buffer = Encoding.UTF8.GetBytes(bundleResponse.Content); return(response.Body.WriteAsync(buffer.AsMemory(0, buffer.Length))); } return(ValueTask.CompletedTask); }