diff --git a/CHANGELOG.md b/CHANGELOG.md index 71202e0d..d2a211c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ The `asp-for` attribute is now obsolete; the `for` attribute should be used in i #### `` and `` tag helpers The `` and `` tag helpers are deprecated and will be removed in a future release. +### Fixes + +#### Source map errors +The hosted CSS and JavaScript files no longer have source maps. +Any console errors from browsers failing to download the referenced files should be eliminated. + ## 2.8.0 Targets GOV.UK Frontend v5.8.0. diff --git a/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreStartupFilter.cs b/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreStartupFilter.cs index 473ea45b..b9bfb816 100644 --- a/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreStartupFilter.cs +++ b/src/GovUk.Frontend.AspNetCore/GovUkFrontendAspNetCoreStartupFilter.cs @@ -29,7 +29,7 @@ public Action Configure(Action next) typeof(GovUkFrontendAspNetCoreStartupFilter).Assembly, root: "Content/Compiled"); - app.UseMiddleware(fileProvider); + app.UseMiddleware(fileProvider); app.UseStaticFiles(new StaticFileOptions() { diff --git a/src/GovUk.Frontend.AspNetCore/RewriteCompiledAssetsMiddleware.cs b/src/GovUk.Frontend.AspNetCore/RewriteCompiledAssetsMiddleware.cs new file mode 100644 index 00000000..e8e57b83 --- /dev/null +++ b/src/GovUk.Frontend.AspNetCore/RewriteCompiledAssetsMiddleware.cs @@ -0,0 +1,68 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Options; + +namespace GovUk.Frontend.AspNetCore; + +internal class RewriteCompiledAssetsMiddleware +{ + private readonly RequestDelegate _next; + private readonly IFileProvider _fileProvider; + private readonly IOptions _optionsAccessor; + + public RewriteCompiledAssetsMiddleware( + RequestDelegate next, + IFileProvider fileProvider, + IOptions optionsAccessor) + { + ArgumentNullException.ThrowIfNull(next); + ArgumentNullException.ThrowIfNull(fileProvider); + ArgumentNullException.ThrowIfNull(optionsAccessor); + _next = next; + _fileProvider = fileProvider; + _optionsAccessor = optionsAccessor; + } + + public async Task InvokeAsync(HttpContext context) + { + if (_optionsAccessor.Value.CompiledContentPath is PathString compiledContentPath && + _optionsAccessor.Value.StaticAssetsContentPath is PathString staticAssetsPath) + { + if (context.Request.Path == compiledContentPath + "/all.min.css") + { + var fileInfo = _fileProvider.GetFileInfo("all.min.css"); + + using var readStream = fileInfo.CreateReadStream(); + using var streamReader = new StreamReader(readStream); + var css = await streamReader.ReadToEndAsync(); + + css = css.Replace("url(/assets/", $"url({context.Request.PathBase}{staticAssetsPath}/"); + css = css.Replace("/*# sourceMappingURL=govuk-frontend.min.css.map */", ""); + + context.Response.Headers.ContentType = new Microsoft.Extensions.Primitives.StringValues("text/css"); + await context.Response.WriteAsync(css); + return; + } + + if (context.Request.Path == compiledContentPath + "/all.min.js") + { + var fileInfo = _fileProvider.GetFileInfo("all.min.js"); + + using var readStream = fileInfo.CreateReadStream(); + using var streamReader = new StreamReader(readStream); + var css = await streamReader.ReadToEndAsync(); + + css = css.Replace("//# sourceMappingURL=govuk-frontend.min.js.map", ""); + + context.Response.Headers.ContentType = new Microsoft.Extensions.Primitives.StringValues("text/javascript"); + await context.Response.WriteAsync(css); + return; + } + } + + await _next(context); + } +} diff --git a/src/GovUk.Frontend.AspNetCore/RewriteCssUrlsMiddleware.cs b/src/GovUk.Frontend.AspNetCore/RewriteCssUrlsMiddleware.cs deleted file mode 100644 index 1dc21c76..00000000 --- a/src/GovUk.Frontend.AspNetCore/RewriteCssUrlsMiddleware.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Options; - -namespace GovUk.Frontend.AspNetCore; - -internal class RewriteCssUrlsMiddleware -{ - private readonly RequestDelegate _next; - private readonly IFileProvider _fileProvider; - private readonly IOptions _optionsAccessor; - - public RewriteCssUrlsMiddleware( - RequestDelegate next, - IFileProvider fileProvider, - IOptions optionsAccessor) - { - ArgumentNullException.ThrowIfNull(next); - ArgumentNullException.ThrowIfNull(fileProvider); - ArgumentNullException.ThrowIfNull(optionsAccessor); - _next = next; - _fileProvider = fileProvider; - _optionsAccessor = optionsAccessor; - } - - public async Task InvokeAsync(HttpContext context) - { - if (_optionsAccessor.Value.CompiledContentPath is PathString compiledContentPath && - _optionsAccessor.Value.StaticAssetsContentPath is PathString staticAssetsPath && - context.Request.Path == compiledContentPath + "/all.min.css") - { - var fileInfo = _fileProvider.GetFileInfo("all.min.css"); - - using var readStream = fileInfo.CreateReadStream(); - using var streamReader = new StreamReader(readStream); - var css = await streamReader.ReadToEndAsync(); - - css = css.Replace("url(/assets/", $"url({context.Request.PathBase}{staticAssetsPath}/"); - - context.Response.Headers.ContentType = new Microsoft.Extensions.Primitives.StringValues("text/css"); - await context.Response.WriteAsync(css); - return; - } - - await _next(context); - } -}