Skip to content

[StaticWebAssets] Compress generated html file #48980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public override bool Execute()
fileWrites.Add(outputPath);
}

htmlCandidates.Add(new TaskItem(outputPath, item.CloneCustomMetadata()));
var newItem = new TaskItem(outputPath, item.CloneCustomMetadata());
newItem.RemoveMetadata("OriginalItemSpec");
htmlCandidates.Add(newItem);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Microsoft.AspNetCore.StaticWebAssets.Tasks;
using System.Text.Json;
using System.IO.Compression;

namespace Microsoft.NET.Sdk.Razor.Tests;

Expand Down Expand Up @@ -88,7 +89,7 @@ public void Publish_OverrideHtmlAssetPlaceholders(string testAsset, string scrip
var indexHtmlOutputPath = Path.Combine(outputPath, "wwwroot", "index.html");
var endpointsManifestPath = Path.Combine(outputPath, $"{projectName}.staticwebassets.endpoints.json");

AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm");
AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm", assertHtmlCompressed: true);
}

private void FingerprintUserJavascriptAssets(bool fingerprintUserJavascriptAssets)
Expand Down Expand Up @@ -125,38 +126,81 @@ private void ReplaceStringInIndexHtml(TestAsset testAsset, string sourceValue, s
}
}

private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false)
private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false, bool assertHtmlCompressed = false)
{
var indexHtmlContent = File.ReadAllText(indexHtmlPath);
var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath));

var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath));
var fingerprintedScriptPath = GetFingerprintedPath(scriptPath);
if (expectFingerprintOnScript)

var indexHtmlContent = File.ReadAllText(indexHtmlPath);
AssertHtmlContent(indexHtmlContent);

if (assertHtmlCompressed)
{
Assert.DoesNotContain($"src=\"{scriptPath}\"", indexHtmlContent);
Assert.Contains($"src=\"{fingerprintedScriptPath}\"", indexHtmlContent);
var indexHtmlGzipContent = DecompressGzipFile(indexHtmlPath + ".gz");
AssertHtmlContent(indexHtmlGzipContent);

var indexHtmlBrotliContent = DecompressBrotliFile(indexHtmlPath + ".br");
AssertHtmlContent(indexHtmlBrotliContent);
}
else

void AssertHtmlContent(string content)
{
Assert.Contains(scriptPath, indexHtmlContent);
if (expectFingerprintOnScript)
{
Assert.DoesNotContain($"src=\"{scriptPath}\"", content);
Assert.Contains($"src=\"{fingerprintedScriptPath}\"", content);
}
else
{
Assert.Contains(scriptPath, content);

if (scriptPath != fingerprintedScriptPath)
{
Assert.DoesNotContain(fingerprintedScriptPath, content);
}
}

Assert.Contains(GetFingerprintedPath("_framework/dotnet.js"), content);
Assert.Contains(GetFingerprintedPath("_framework/dotnet.native.js"), content);
Assert.Contains(GetFingerprintedPath("_framework/dotnet.runtime.js"), content);

if (scriptPath != fingerprintedScriptPath)
if (expectPreloadElement)
{
Assert.DoesNotContain(fingerprintedScriptPath, indexHtmlContent);
Assert.DoesNotContain("<link rel=\"preload\"", content);
Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", content);
}
}

Assert.Contains(GetFingerprintedPath("_framework/dotnet.js"), indexHtmlContent);
Assert.Contains(GetFingerprintedPath("_framework/dotnet.native.js"), indexHtmlContent);
Assert.Contains(GetFingerprintedPath("_framework/dotnet.runtime.js"), indexHtmlContent);
string GetFingerprintedPath(string route)
=> endpoints.Endpoints.FirstOrDefault(e => e.Route == route && e.Selectors.Length == 0)?.AssetFile ?? throw new Exception($"Missing endpoint for file '{route}' in '{endpointsManifestPath}'");

if (expectPreloadElement)
string DecompressGzipFile(string path)
{
Assert.DoesNotContain("<link rel=\"preload\">", indexHtmlContent);
Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", indexHtmlContent);
if (File.Exists(path))
{
using var fileStream = File.OpenRead(path);
using var compressedStream = new GZipStream(fileStream, CompressionMode.Decompress);
using var reader = new StreamReader(compressedStream);
return reader.ReadToEnd();
}

Assert.Fail($"File '{path}' does not exist.");
return null;
}

string GetFingerprintedPath(string route)
=> endpoints.Endpoints.FirstOrDefault(e => e.Route == route && e.Selectors.Length == 0)?.AssetFile ?? throw new Exception($"Missing endpoint for file '{route}' in '{endpointsManifestPath}'");
string DecompressBrotliFile(string path)
{
if (File.Exists(path))
{
using var fileStream = File.OpenRead(path);
using var compressedStream = new BrotliStream(fileStream, CompressionMode.Decompress);
using var reader = new StreamReader(compressedStream);
return reader.ReadToEnd();
}

Assert.Fail($"File '{path}' does not exist.");
return null;
}
}
}