Skip to content

Commit 5082b92

Browse files
authored
[StaticWebAssets] Compress generated html file (#48980)
1 parent 34920af commit 5082b92

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public override bool Execute()
9898
fileWrites.Add(outputPath);
9999
}
100100

101-
htmlCandidates.Add(new TaskItem(outputPath, item.CloneCustomMetadata()));
101+
var newItem = new TaskItem(outputPath, item.CloneCustomMetadata());
102+
newItem.RemoveMetadata("OriginalItemSpec");
103+
htmlCandidates.Add(newItem);
102104
}
103105
}
104106
}

test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsFingerprintingTest.cs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.StaticWebAssets.Tasks;
77
using System.Text.Json;
8+
using System.IO.Compression;
89

910
namespace Microsoft.NET.Sdk.Razor.Tests;
1011

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

91-
AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm");
92+
AssertImportMapInHtml(indexHtmlOutputPath, endpointsManifestPath, scriptPath, expectFingerprintOnScript: expectFingerprintOnScript, expectPreloadElement: testAsset == "VanillaWasm", assertHtmlCompressed: true);
9293
}
9394

9495
private void FingerprintUserJavascriptAssets(bool fingerprintUserJavascriptAssets)
@@ -125,38 +126,81 @@ private void ReplaceStringInIndexHtml(TestAsset testAsset, string sourceValue, s
125126
}
126127
}
127128

128-
private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false)
129+
private void AssertImportMapInHtml(string indexHtmlPath, string endpointsManifestPath, string scriptPath, bool expectFingerprintOnScript = true, bool expectPreloadElement = false, bool assertHtmlCompressed = false)
129130
{
130-
var indexHtmlContent = File.ReadAllText(indexHtmlPath);
131-
var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath));
132131

132+
var endpoints = JsonSerializer.Deserialize<StaticWebAssetEndpointsManifest>(File.ReadAllText(endpointsManifestPath));
133133
var fingerprintedScriptPath = GetFingerprintedPath(scriptPath);
134-
if (expectFingerprintOnScript)
134+
135+
var indexHtmlContent = File.ReadAllText(indexHtmlPath);
136+
AssertHtmlContent(indexHtmlContent);
137+
138+
if (assertHtmlCompressed)
135139
{
136-
Assert.DoesNotContain($"src=\"{scriptPath}\"", indexHtmlContent);
137-
Assert.Contains($"src=\"{fingerprintedScriptPath}\"", indexHtmlContent);
140+
var indexHtmlGzipContent = DecompressGzipFile(indexHtmlPath + ".gz");
141+
AssertHtmlContent(indexHtmlGzipContent);
142+
143+
var indexHtmlBrotliContent = DecompressBrotliFile(indexHtmlPath + ".br");
144+
AssertHtmlContent(indexHtmlBrotliContent);
138145
}
139-
else
146+
147+
void AssertHtmlContent(string content)
140148
{
141-
Assert.Contains(scriptPath, indexHtmlContent);
149+
if (expectFingerprintOnScript)
150+
{
151+
Assert.DoesNotContain($"src=\"{scriptPath}\"", content);
152+
Assert.Contains($"src=\"{fingerprintedScriptPath}\"", content);
153+
}
154+
else
155+
{
156+
Assert.Contains(scriptPath, content);
157+
158+
if (scriptPath != fingerprintedScriptPath)
159+
{
160+
Assert.DoesNotContain(fingerprintedScriptPath, content);
161+
}
162+
}
163+
164+
Assert.Contains(GetFingerprintedPath("_framework/dotnet.js"), content);
165+
Assert.Contains(GetFingerprintedPath("_framework/dotnet.native.js"), content);
166+
Assert.Contains(GetFingerprintedPath("_framework/dotnet.runtime.js"), content);
142167

143-
if (scriptPath != fingerprintedScriptPath)
168+
if (expectPreloadElement)
144169
{
145-
Assert.DoesNotContain(fingerprintedScriptPath, indexHtmlContent);
170+
Assert.DoesNotContain("<link rel=\"preload\"", content);
171+
Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", content);
146172
}
147173
}
148174

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

153-
if (expectPreloadElement)
178+
string DecompressGzipFile(string path)
154179
{
155-
Assert.DoesNotContain("<link rel=\"preload\">", indexHtmlContent);
156-
Assert.Contains($"<link href=\"{fingerprintedScriptPath}\" rel=\"preload\" as=\"script\" fetchpriority=\"high\" crossorigin=\"anonymous\"", indexHtmlContent);
180+
if (File.Exists(path))
181+
{
182+
using var fileStream = File.OpenRead(path);
183+
using var compressedStream = new GZipStream(fileStream, CompressionMode.Decompress);
184+
using var reader = new StreamReader(compressedStream);
185+
return reader.ReadToEnd();
186+
}
187+
188+
Assert.Fail($"File '{path}' does not exist.");
189+
return null;
157190
}
158191

159-
string GetFingerprintedPath(string route)
160-
=> endpoints.Endpoints.FirstOrDefault(e => e.Route == route && e.Selectors.Length == 0)?.AssetFile ?? throw new Exception($"Missing endpoint for file '{route}' in '{endpointsManifestPath}'");
192+
string DecompressBrotliFile(string path)
193+
{
194+
if (File.Exists(path))
195+
{
196+
using var fileStream = File.OpenRead(path);
197+
using var compressedStream = new BrotliStream(fileStream, CompressionMode.Decompress);
198+
using var reader = new StreamReader(compressedStream);
199+
return reader.ReadToEnd();
200+
}
201+
202+
Assert.Fail($"File '{path}' does not exist.");
203+
return null;
204+
}
161205
}
162206
}

0 commit comments

Comments
 (0)