Skip to content

Commit e7eb44a

Browse files
authored
[browser] Expect fingerprint on dotnet.js for preloading (#120694)
1 parent 3333781 commit e7eb44a

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,13 @@ Copyright (c) .NET Foundation. All rights reserved.
566566
</Target>
567567

568568
<Target Name="_AddWasmPreloadBuildProperties" DependsOnTargets="_WasmConfigurePreload;_AddWasmStaticWebAssets" BeforeTargets="GenerateStaticWebAssetsManifest" Condition="'$(_WasmPreloadAssets)' == 'true'">
569+
<PropertyGroup>
570+
<_WasmBootConfigFileNameWithoutExtension>$([System.IO.Path]::GetFileNameWithoutExtension('$(_WasmBootConfigFileName)'))</_WasmBootConfigFileNameWithoutExtension>
571+
<_WasmBootConfigFileExtension>$([System.IO.Path]::GetExtension('$(_WasmBootConfigFileName)'))</_WasmBootConfigFileExtension>
572+
</PropertyGroup>
569573
<ItemGroup>
570-
<_WasmPreloadBuildScriptAsset Include="@(StaticWebAsset)" Condition="'%(AssetKind)' != 'Publish' and '%(FileName)%(Extension)' == '$(_WasmBootConfigFileName)'" />
574+
<_WasmPreloadBuildScriptAsset Include="@(StaticWebAsset)" Condition="'$(_WasmFingerprintAssets)' == 'true' and '$(_WasmFingerprintBootConfig)' == 'true' and '%(AssetKind)' != 'Publish' and '%(FileName)%(Extension)' == '$(_WasmBootConfigFileNameWithoutExtension).%(Fingerprint)$(_WasmBootConfigFileExtension)'" />
575+
<_WasmPreloadBuildScriptAsset Include="@(StaticWebAsset)" Condition="('$(_WasmFingerprintAssets)' != 'true' or '$(_WasmFingerprintBootConfig)' != 'true') and '%(AssetKind)' != 'Publish' and '%(FileName)%(Extension)' == '$(_WasmBootConfigFileName)'" />
571576
</ItemGroup>
572577

573578
<FilterStaticWebAssetEndpoints

src/mono/wasm/Wasm.Build.Tests/PreloadingTests.cs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.IO;
56
using System.Linq;
67
using Xunit;
@@ -18,22 +19,26 @@ public PreloadingTests(ITestOutputHelper output, SharedBuildPerTestClassFixture
1819
}
1920

2021
[Theory]
21-
[InlineData(false, false)]
22-
[InlineData(false, true)]
23-
[InlineData(true, false)]
24-
[InlineData(true, true)]
25-
public void PreloadAssets(bool isPublish, bool fingerprintDotnetJs)
22+
[InlineData(false, false, false)]
23+
[InlineData(false, false, true)]
24+
[InlineData(false, true, false)]
25+
[InlineData(false, true, true)]
26+
[InlineData(true, false, false)]
27+
[InlineData(true, false, true)]
28+
[InlineData(true, true, false)]
29+
[InlineData(true, true, true)]
30+
public void PreloadAssets(bool isPublish, bool fingerprintDotnetJs, bool preloadAssets)
2631
{
2732
Configuration config = Configuration.Debug;
2833
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.WasmBasicTestApp, "PreloadAssets");
2934

30-
string extraMSBuildArgs = $"-p:WasmFingerprintDotnetJs={fingerprintDotnetJs}";
35+
string extraMSBuildArgs = $"-p:WasmFingerprintDotnetJs={fingerprintDotnetJs.ToString().ToLower()} -p:WasmPreloadAssets={preloadAssets.ToString().ToLower()}";
3136
if (isPublish)
3237
PublishProject(info, config, new PublishOptions(ExtraMSBuildArgs: extraMSBuildArgs), wasmFingerprintDotnetJs: fingerprintDotnetJs);
3338
else
3439
BuildProject(info, config, new BuildOptions(ExtraMSBuildArgs: extraMSBuildArgs), wasmFingerprintDotnetJs: fingerprintDotnetJs);
3540

36-
string? indexHtmlPath = null;
41+
string? indexHtmlPath;
3742
if (isPublish)
3843
{
3944
indexHtmlPath = Path.Combine(
@@ -51,16 +56,37 @@ public void PreloadAssets(bool isPublish, bool fingerprintDotnetJs)
5156
Assert.True(File.Exists(indexHtmlPath));
5257
string indexHtmlContent = File.ReadAllText(indexHtmlPath);
5358

54-
if (fingerprintDotnetJs)
59+
Assert.Equal(preloadAssets ? 1 : 0, CountOccurrences(indexHtmlContent, "rel=\"preload\""));
60+
if (preloadAssets)
5561
{
56-
// Expect to find fingerprinted preload
57-
Assert.Contains("<link href=\"_framework/dotnet", indexHtmlContent);
58-
Assert.DoesNotContain("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
62+
if (fingerprintDotnetJs)
63+
{
64+
// Expect to find fingerprinted preload
65+
Assert.Contains("<link href=\"_framework/dotnet", indexHtmlContent);
66+
Assert.DoesNotContain("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
67+
}
68+
else
69+
{
70+
// Expect to find non-fingerprinted preload
71+
Assert.Contains("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
72+
}
5973
}
60-
else
74+
}
75+
76+
public static int CountOccurrences(string source, string substring)
77+
{
78+
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(substring))
79+
return 0;
80+
81+
int count = 0;
82+
int index = 0;
83+
84+
while ((index = source.IndexOf(substring, index, StringComparison.Ordinal)) != -1)
6185
{
62-
// Expect to find non-fingerprinted preload
63-
Assert.Contains("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
86+
count++;
87+
index += substring.Length;
6488
}
89+
90+
return count;
6591
}
6692
}

0 commit comments

Comments
 (0)