Skip to content

Commit e5c675b

Browse files
javiercnmaraf
andauthored
[StaticWebAssets] Use property for original resource (#50420)
Co-authored-by: Marek Fišera <[email protected]>
1 parent a23ef31 commit e5c675b

File tree

76 files changed

+12916
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+12916
-6
lines changed

src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.Compression.targets

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ Copyright (c) .NET Foundation. All rights reserved.
170170
<BrotliCompressionLevel Condition="'$(_BlazorBrotliCompressionLevel)' != ''">$(_BlazorBrotliCompressionLevel)</BrotliCompressionLevel>
171171

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

176177
<PropertyGroup>
@@ -253,7 +254,7 @@ Copyright (c) .NET Foundation. All rights reserved.
253254
<ApplyCompressionNegotiation
254255
CandidateEndpoints="@(StaticWebAssetEndpoint)"
255256
CandidateAssets="@(_CompressionCurrentProjectBuildAssets)"
256-
AttachWeakETagToCompressedAssets="$(AttachWeakETagToCompressedAssetsDuringDevelopment)"
257+
AttachWeakETagToCompressedAssets="$(LinkAlternativeRepresentationsToOriginalResource)"
257258
>
258259
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionBuildEndpoints" />
259260
</ApplyCompressionNegotiation>
@@ -377,7 +378,8 @@ Copyright (c) .NET Foundation. All rights reserved.
377378

378379
<ApplyCompressionNegotiation
379380
CandidateEndpoints="@(StaticWebAssetEndpoint)"
380-
CandidateAssets="@(_CompressionCurrentProjectPublishAssets)">
381+
CandidateAssets="@(_CompressionCurrentProjectPublishAssets)"
382+
AttachWeakETagToCompressedAssets="$(LinkAlternativeRepresentationsToOriginalResource)">
381383
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionPublishEndpoints" />
382384
</ApplyCompressionNegotiation>
383385

src/StaticWebAssetsSdk/Targets/Sdk.StaticWebAssets.CurrentVersion.targets

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ Copyright (c) .NET Foundation. All rights reserved.
4343

4444
<!-- Resolve the TFM-specific attributes conditionally. -->
4545
<Choose>
46+
<When Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '10.0')) ">
47+
<PropertyGroup>
48+
<_TargetingNETCoreApp30OrLater>true</_TargetingNETCoreApp30OrLater>
49+
<_TargetingNET50OrLater>true</_TargetingNET50OrLater>
50+
<_TargetingNET60OrLater>true</_TargetingNET60OrLater>
51+
<_TargetingNET70OrLater>true</_TargetingNET70OrLater>
52+
<_TargetingNET80OrLater>true</_TargetingNET80OrLater>
53+
<_TargetingNET90OrLater>true</_TargetingNET90OrLater>
54+
<_TargetingNET100OrLater>true</_TargetingNET100OrLater>
55+
<_RazorLangVersion Condition="'$(RazorLangVersion)' == '' ">10.0</_RazorLangVersion>
56+
</PropertyGroup>
57+
</When>
4658
<When Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '9.0')) ">
4759
<PropertyGroup>
4860
<_TargetingNETCoreApp30OrLater>true</_TargetingNETCoreApp30OrLater>

src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ApplyCompressionNegotiation : Task
1616
[Required]
1717
public ITaskItem[] CandidateAssets { get; set; }
1818

19-
public bool AttachWeakETagToCompressedAssets { get; set; }
19+
public string AttachWeakETagToCompressedAssets { get; set; }
2020

2121
[Output]
2222
public ITaskItem[] UpdatedEndpoints { get; set; }
@@ -248,6 +248,27 @@ private StaticWebAssetEndpoint CreateUpdatedEndpoint(
248248
Quality = quality
249249
};
250250
Log.LogMessage(MessageImportance.Low, " Created Content-Encoding selector for compressed asset '{0}' with size '{1}' is '{2}'", encodingSelector.Value, encodingSelector.Quality, relatedEndpointCandidate.Route);
251+
252+
// Handle EndpointProperty case for ETag
253+
var endpointProperties = relatedEndpointCandidate.EndpointProperties.ToList();
254+
if (string.Equals(AttachWeakETagToCompressedAssets, "EndpointProperty", StringComparison.Ordinal))
255+
{
256+
// Find ETag header in the related endpoint candidate
257+
foreach (var header in relatedEndpointCandidate.ResponseHeaders)
258+
{
259+
if (string.Equals(header.Name, "ETag", StringComparison.Ordinal))
260+
{
261+
Log.LogMessage(MessageImportance.Low, " Adding original-resource endpoint property for related endpoint '{0}'", relatedEndpointCandidate.Route);
262+
endpointProperties.Add(new StaticWebAssetEndpointProperty
263+
{
264+
Name = "original-resource",
265+
Value = header.Value
266+
});
267+
break;
268+
}
269+
}
270+
}
271+
251272
var endpointCopy = new StaticWebAssetEndpoint
252273
{
253274
AssetFile = compressedAsset.Identity,
@@ -256,7 +277,7 @@ private StaticWebAssetEndpoint CreateUpdatedEndpoint(
256277
..relatedEndpointCandidate.Selectors,
257278
encodingSelector
258279
],
259-
EndpointProperties = relatedEndpointCandidate.EndpointProperties
280+
EndpointProperties = [.. endpointProperties]
260281
};
261282
var headers = new List<StaticWebAssetEndpointResponseHeader>(7);
262283
ApplyCompressedEndpointHeaders(headers, compressedEndpoint, relatedEndpointCandidate.Route);
@@ -373,7 +394,7 @@ private void ApplyRelatedEndpointCandidateHeaders(List<StaticWebAssetEndpointRes
373394
Log.LogMessage(MessageImportance.Low, " Adding header '{0}' to related endpoint '{1}'", header.Name, relatedEndpointCandidate.Route);
374395
headers.Add(header);
375396
}
376-
else if (AttachWeakETagToCompressedAssets && string.Equals(header.Name, "ETag", StringComparison.Ordinal))
397+
else if (string.Equals(AttachWeakETagToCompressedAssets, "ResponseHeader", StringComparison.Ordinal) && string.Equals(header.Name, "ETag", StringComparison.Ordinal))
377398
{
378399
// A resource can have multiple ETags. Since the uncompressed resource has an ETag,
379400
// and we are serving the compressed resource from the same URL, we need to update

0 commit comments

Comments
 (0)