Skip to content

[StaticWebAssets] Use property for original resource #50420

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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 @@ -170,7 +170,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<BrotliCompressionLevel Condition="'$(_BlazorBrotliCompressionLevel)' != ''">$(_BlazorBrotliCompressionLevel)</BrotliCompressionLevel>

<!-- Attach weak ETag to compressed assets -->
<AttachWeakETagToCompressedAssetsDuringDevelopment Condition="'$(AttachWeakETagToCompressedAssetsDuringDevelopment)' == '' and '$(Configuration)' == 'Debug'">true</AttachWeakETagToCompressedAssetsDuringDevelopment>
<LinkAlternativeRepresentationsToOriginalResource Condition="'$(LinkAlternativeRepresentationsToOriginalResource)' == '' and '$(_TargetingNET100OrLater)' == 'true'">EndpointProperty</LinkAlternativeRepresentationsToOriginalResource>
<LinkAlternativeRepresentationsToOriginalResource Condition="'$(LinkAlternativeRepresentationsToOriginalResource)' == ''">ResponseHeader</LinkAlternativeRepresentationsToOriginalResource>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -253,7 +254,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<ApplyCompressionNegotiation
CandidateEndpoints="@(StaticWebAssetEndpoint)"
CandidateAssets="@(_CompressionCurrentProjectBuildAssets)"
AttachWeakETagToCompressedAssets="$(AttachWeakETagToCompressedAssetsDuringDevelopment)"
AttachWeakETagToCompressedAssets="$(LinkAlternativeRepresentationsToOriginalResource)"
>
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionBuildEndpoints" />
</ApplyCompressionNegotiation>
Expand Down Expand Up @@ -377,7 +378,8 @@ Copyright (c) .NET Foundation. All rights reserved.

<ApplyCompressionNegotiation
CandidateEndpoints="@(StaticWebAssetEndpoint)"
CandidateAssets="@(_CompressionCurrentProjectPublishAssets)">
CandidateAssets="@(_CompressionCurrentProjectPublishAssets)"
AttachWeakETagToCompressedAssets="$(LinkAlternativeRepresentationsToOriginalResource)">
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionPublishEndpoints" />
</ApplyCompressionNegotiation>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ Copyright (c) .NET Foundation. All rights reserved.

<!-- Resolve the TFM-specific attributes conditionally. -->
<Choose>
<When Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '10.0')) ">
<PropertyGroup>
<_TargetingNETCoreApp30OrLater>true</_TargetingNETCoreApp30OrLater>
<_TargetingNET50OrLater>true</_TargetingNET50OrLater>
<_TargetingNET60OrLater>true</_TargetingNET60OrLater>
<_TargetingNET70OrLater>true</_TargetingNET70OrLater>
<_TargetingNET80OrLater>true</_TargetingNET80OrLater>
<_TargetingNET90OrLater>true</_TargetingNET90OrLater>
<_TargetingNET100OrLater>true</_TargetingNET100OrLater>
<_RazorLangVersion Condition="'$(RazorLangVersion)' == '' ">10.0</_RazorLangVersion>
</PropertyGroup>
</When>
<When Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '9.0')) ">
<PropertyGroup>
<_TargetingNETCoreApp30OrLater>true</_TargetingNETCoreApp30OrLater>
Expand Down
27 changes: 24 additions & 3 deletions src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ApplyCompressionNegotiation : Task
[Required]
public ITaskItem[] CandidateAssets { get; set; }

public bool AttachWeakETagToCompressedAssets { get; set; }
public string AttachWeakETagToCompressedAssets { get; set; }

[Output]
public ITaskItem[] UpdatedEndpoints { get; set; }
Expand Down Expand Up @@ -248,6 +248,27 @@ private StaticWebAssetEndpoint CreateUpdatedEndpoint(
Quality = quality
};
Log.LogMessage(MessageImportance.Low, " Created Content-Encoding selector for compressed asset '{0}' with size '{1}' is '{2}'", encodingSelector.Value, encodingSelector.Quality, relatedEndpointCandidate.Route);

// Handle EndpointProperty case for ETag
var endpointProperties = relatedEndpointCandidate.EndpointProperties.ToList();
if (string.Equals(AttachWeakETagToCompressedAssets, "EndpointProperty", StringComparison.Ordinal))
{
// Find ETag header in the related endpoint candidate
foreach (var header in relatedEndpointCandidate.ResponseHeaders)
{
if (string.Equals(header.Name, "ETag", StringComparison.Ordinal))
{
Log.LogMessage(MessageImportance.Low, " Adding original-resource endpoint property for related endpoint '{0}'", relatedEndpointCandidate.Route);
endpointProperties.Add(new StaticWebAssetEndpointProperty
{
Name = "original-resource",
Value = header.Value
});
break;
}
}
}

var endpointCopy = new StaticWebAssetEndpoint
{
AssetFile = compressedAsset.Identity,
Expand All @@ -256,7 +277,7 @@ private StaticWebAssetEndpoint CreateUpdatedEndpoint(
..relatedEndpointCandidate.Selectors,
encodingSelector
],
EndpointProperties = relatedEndpointCandidate.EndpointProperties
EndpointProperties = [.. endpointProperties]
};
var headers = new List<StaticWebAssetEndpointResponseHeader>(7);
ApplyCompressedEndpointHeaders(headers, compressedEndpoint, relatedEndpointCandidate.Route);
Expand Down Expand Up @@ -373,7 +394,7 @@ private void ApplyRelatedEndpointCandidateHeaders(List<StaticWebAssetEndpointRes
Log.LogMessage(MessageImportance.Low, " Adding header '{0}' to related endpoint '{1}'", header.Name, relatedEndpointCandidate.Route);
headers.Add(header);
}
else if (AttachWeakETagToCompressedAssets && string.Equals(header.Name, "ETag", StringComparison.Ordinal))
else if (string.Equals(AttachWeakETagToCompressedAssets, "ResponseHeader", StringComparison.Ordinal) && string.Equals(header.Name, "ETag", StringComparison.Ordinal))
{
// A resource can have multiple ETags. Since the uncompressed resource has an ETag,
// and we are serving the compressed resource from the same URL, we need to update
Expand Down
Loading
Loading