diff --git a/src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs b/src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs index e69a194c6407..ca03febfc27c 100644 --- a/src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs +++ b/src/StaticWebAssetsSdk/Tasks/OverrideHtmlAssetPlaceholders.cs @@ -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); } } } diff --git a/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsFingerprintingTest.cs b/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsFingerprintingTest.cs index 15438446676a..0cc7dbb91da4 100644 --- a/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsFingerprintingTest.cs +++ b/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssetsFingerprintingTest.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.StaticWebAssets.Tasks; using System.Text.Json; +using System.IO.Compression; namespace Microsoft.NET.Sdk.Razor.Tests; @@ -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) @@ -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(File.ReadAllText(endpointsManifestPath)); + var endpoints = JsonSerializer.Deserialize(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(" 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("", indexHtmlContent); - Assert.Contains($" 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; + } } }