Skip to content

Commit

Permalink
Remove source map references from compiled assets
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Jan 17, 2025
1 parent e491018 commit 54d22e1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 51 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The `asp-for` attribute is now obsolete; the `for` attribute should be used in i
#### `<govuk-error-message>` and `<govuk-fieldset>` tag helpers
The `<govuk-error-message>` and `<govuk-fieldset>` 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
typeof(GovUkFrontendAspNetCoreStartupFilter).Assembly,
root: "Content/Compiled");

app.UseMiddleware<RewriteCssUrlsMiddleware>(fileProvider);
app.UseMiddleware<RewriteCompiledAssetsMiddleware>(fileProvider);

app.UseStaticFiles(new StaticFileOptions()
{
Expand Down
68 changes: 68 additions & 0 deletions src/GovUk.Frontend.AspNetCore/RewriteCompiledAssetsMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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<GovUkFrontendAspNetCoreOptions> _optionsAccessor;

public RewriteCompiledAssetsMiddleware(
RequestDelegate next,
IFileProvider fileProvider,
IOptions<GovUkFrontendAspNetCoreOptions> 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);
}
}
50 changes: 0 additions & 50 deletions src/GovUk.Frontend.AspNetCore/RewriteCssUrlsMiddleware.cs

This file was deleted.

0 comments on commit 54d22e1

Please sign in to comment.