Skip to content
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

Allow to fingerprint Blazor.js #46988

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

maraf
Copy link
Member

@maraf maraf commented Feb 20, 2025

This PR allows to put hard fingerprint on blazor.js file if WriteImportMapToHtml=true.
It can additionally be controlled with BlazorFingerprintBlazorJs=false.

@maraf maraf added this to the 10.0.1xx milestone Feb 20, 2025
@maraf maraf requested a review from javiercn February 20, 2025 14:05
@maraf maraf self-assigned this Feb 20, 2025
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch untriaged Request triage from a team member labels Feb 20, 2025
Copy link
Contributor

Thanks for your PR, @maraf.
To learn about the PR process and branching schedule of this repo, please take a look at the SDK PR Guide.

@maraf maraf removed the untriaged Request triage from a team member label Feb 20, 2025
@maraf maraf marked this pull request as ready for review February 21, 2025 09:58
@Copilot Copilot bot review requested due to automatic review settings February 21, 2025 09:58
@maraf maraf requested review from a team as code owners February 21, 2025 09:58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

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

  • The change from a boolean flag to a string representing the script path means that test inputs now dictate the expected file name directly. Please verify that both cases (e.g., 'main.js' and '_framework/blazor.webassembly.js') are adequately exercised and that any documentation or comments are updated to reflect this change.
public static TheoryData<string, string> WriteImportMapToHtmlData => new TheoryData<string, string>
@maraf
Copy link
Member Author

maraf commented Feb 21, 2025

There is a bit of dance to make asset promotion in publish happy. The implementation is based on what we do in runtime to promote hard fingerprinted assets for publish. An alternative would probably be to define these assets as build only and create new assets for publish. I'm not sure if that's a better solution and whether the longer-term goal of not copying these assets to build outputs isn't a way to go

Comment on lines +159 to +213
<Target Name="_ReplaceFingerprintedBlazorJsForPublish" DependsOnTargets="ProcessPublishFilesForWasm" Condition="'$(WasmBuildingForNestedPublish)' != 'true' and '$(BlazorFingerprintBlazorJs)' == 'true'">
<ItemGroup>
<_BlazorJSStaticWebAsset Include="@(StaticWebAsset)" Condition="'%(FileName)' == '%(_BlazorJSFile.FileName)'" />
<_BlazorJSPublishCandidate Include="%(_BlazorJSStaticWebAsset.RelativeDir)%(_BlazorJSStaticWebAsset.FileName).%(_BlazorJSStaticWebAsset.Fingerprint)%(_BlazorJSStaticWebAsset.Extension)" />
<_BlazorJSPublishCandidate>
<RelativePath>_framework/$([System.IO.Path]::GetFileNameWithoutExtension('%(Filename)'))%(Extension)</RelativePath>
</_BlazorJSPublishCandidate>
</ItemGroup>

<DefineStaticWebAssets
CandidateAssets="@(_BlazorJSPublishCandidate)"
FingerprintCandidates="true"
FingerprintPatterns="@(_BlazorJSFingerprintPattern)"
SourceId="$(PackageId)"
SourceType="Computed"
AssetKind="All"
AssetMergeSource="$(StaticWebAssetMergeTarget)"
AssetRole="Primary"
AssetTraitName="WasmResource"
AssetTraitValue="boot"
CopyToOutputDirectory="Never"
CopyToPublishDirectory="PreserveNewest"
ContentRoot="%(_BlazorJSStaticWebAsset.ContentRoot)"
BasePath="%(_BlazorJSStaticWebAsset.BasePath)"
>
<Output TaskParameter="Assets" ItemName="_BlazorJSPublishStaticWebAssets" />
</DefineStaticWebAssets>
<DefineStaticWebAssetEndpoints
CandidateAssets="@(_BlazorJSPublishStaticWebAssets)"
ExistingEndpoints="@(StaticWebAssetEndpoint)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="_BlazorJSPublishStaticWebAssetsEndpoint" />
</DefineStaticWebAssetEndpoints>
<PropertyGroup>
<_BlazorJSStaticWebAssetFullPath>@(_BlazorJSStaticWebAsset->'%(FullPath)')</_BlazorJSStaticWebAssetFullPath>
</PropertyGroup>
<ItemGroup>
<_BlazorJSStaticWebAsset Include="@(StaticWebAsset)" Condition="'%(AssetTraitName)' == 'Content-Encoding' and '%(RelatedAsset)' == '$(_BlazorJSStaticWebAssetFullPath)'" />
</ItemGroup>
<FilterStaticWebAssetEndpoints Condition="'@(_BlazorJSStaticWebAsset)' != ''"
Endpoints="@(StaticWebAssetEndpoint)"
Assets="@(_BlazorJSStaticWebAsset)"
Filters=""
>
<Output TaskParameter="FilteredEndpoints" ItemName="_BlazorJSEndpointsToRemove" />
</FilterStaticWebAssetEndpoints>
<ItemGroup>
<StaticWebAsset Remove="@(_BlazorJSStaticWebAsset)" />
<StaticWebAsset Include="@(_BlazorJSPublishStaticWebAssets)" />
<StaticWebAssetEndpoint Remove="@(_BlazorJSEndpointsToRemove)" />
<StaticWebAssetEndpoint Include="@(_BlazorJSPublishStaticWebAssetsEndpoint)" />
</ItemGroup>
</Target>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is confusing me a bit, do we rewrite blazor.js in any way during this whole process? If not, I'm not sure why this is needed at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same issue we have with runtime assets. If we do hard fingerprint during build, the asset to promote for publish has identity without fingerprint (bin/Debug/wwwroot/_framework/blazor.js), but the file on disk has fingerprint (bin/Debug/wwwroot/_framework/blazor.abcd.js) and thus it complains that the file doesn't exist.

This is fixing by creating asset that has fingerprint in identity.

Maybe we can do soft fingerprint during build and hard only during publish. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants