Skip to content

Commit ab2777f

Browse files
authored
[Static Web Assets] Fix compressed endpoint headers (#41511)
* When fingerprinting assets the headers were not being correctly applied to the compressed endpoints with fingerprint. * Added a fix and a test to cover the scenario.
1 parent f101ab0 commit ab2777f

File tree

54 files changed

+2349
-2499
lines changed

Some content is hidden

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

54 files changed

+2349
-2499
lines changed

src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override bool Execute()
2929
.ToDictionary(g => g.Key, g => g.ToList());
3030

3131
var compressedAssets = assetsById.Values.Where(a => a.AssetTraitName == "Content-Encoding").ToList();
32-
var updatedEndpoints = new List<StaticWebAssetEndpoint>();
32+
var updatedEndpoints = new HashSet<StaticWebAssetEndpoint>(StaticWebAssetEndpoint.RouteAndAssetComparer);
3333

3434
var preservedEndpoints = new Dictionary<(string, string), StaticWebAssetEndpoint>();
3535

@@ -89,13 +89,14 @@ public override bool Execute()
8989
}
9090

9191
Log.LogMessage(MessageImportance.Low, " Updated endpoint '{0}' with Content-Encoding and Vary headers", compressedEndpoint.Route);
92-
if (!updatedEndpoints.Contains(compressedEndpoint))
93-
{
94-
updatedEndpoints.Add(compressedEndpoint);
95-
}
92+
updatedEndpoints.Add(compressedEndpoint);
9693

9794
foreach (var relatedEndpointCandidate in relatedAssetEndpoints)
9895
{
96+
if (!IsCompatible(compressedEndpoint, relatedEndpointCandidate))
97+
{
98+
continue;
99+
}
99100
Log.LogMessage(MessageImportance.Low, "Processing related endpoint '{0}'", relatedEndpointCandidate.Route);
100101
var encodingSelector = new StaticWebAssetEndpointSelector
101102
{
@@ -188,7 +189,10 @@ public override bool Execute()
188189
{
189190
Log.LogMessage(MessageImportance.Low, " Adding endpoint '{0}'", endpoint.AssetFile);
190191
}
191-
updatedEndpoints.AddRange(endpoints);
192+
foreach (var endpoint in endpoints)
193+
{
194+
updatedEndpoints.Add(endpoint);
195+
}
192196
}
193197
}
194198

@@ -197,6 +201,13 @@ public override bool Execute()
197201
return true;
198202
}
199203

204+
private static bool IsCompatible(StaticWebAssetEndpoint compressedEndpoint, StaticWebAssetEndpoint relatedEndpointCandidate)
205+
{
206+
var compressedFingerprint = compressedEndpoint.EndpointProperties.FirstOrDefault(ep => ep.Name == "fingerprint");
207+
var relatedFingerprint = relatedEndpointCandidate.EndpointProperties.FirstOrDefault(ep => ep.Name == "fingerprint");
208+
return string.Equals(compressedFingerprint?.Value, relatedFingerprint?.Value, StringComparison.Ordinal);
209+
}
210+
200211
private void ApplyCompressedEndpointHeaders(List<StaticWebAssetEndpointResponseHeader> headers, StaticWebAssetEndpoint compressedEndpoint, string relatedEndpointCandidateRoute)
201212
{
202213
foreach (var header in compressedEndpoint.ResponseHeaders)

src/StaticWebAssetsSdk/Tasks/Data/StaticWebAssetPathPattern.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ public static StaticWebAssetPathPattern Parse(string rawPath, string assetIdenti
190190
}
191191
else if (!string.IsNullOrEmpty(part.Value))
192192
{
193+
// Token was embedded, so add it to the dictionary.
194+
dictionary[part.Name] = part.Value;
193195
result.Append(part.Value);
194196
}
195197
else

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_BuildMinimal_Works.Build.staticwebassets.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36900,7 +36900,7 @@
3690036900
},
3690136901
{
3690236902
"Name": "Cache-Control",
36903-
"Value": "no-cache"
36903+
"Value": "max-age=31536000, immutable"
3690436904
},
3690536905
{
3690636906
"Name": "Content-Encoding",
@@ -36928,9 +36928,17 @@
3692836928
}
3692936929
],
3693036930
"EndpointProperties": [
36931+
{
36932+
"Name": "fingerprint",
36933+
"Value": "__fingerprint__"
36934+
},
3693136935
{
3693236936
"Name": "integrity",
3693336937
"Value": "__integrity__"
36938+
},
36939+
{
36940+
"Name": "label",
36941+
"Value": "blazorwasm-minimal.bundle.scp.css.gz"
3693436942
}
3693536943
]
3693636944
},
@@ -37006,7 +37014,7 @@
3700637014
},
3700737015
{
3700837016
"Name": "Cache-Control",
37009-
"Value": "no-cache"
37017+
"Value": "max-age=31536000, immutable"
3701037018
},
3701137019
{
3701237020
"Name": "Content-Encoding",
@@ -37108,7 +37116,7 @@
3710837116
},
3710937117
{
3711037118
"Name": "Cache-Control",
37111-
"Value": "no-cache"
37119+
"Value": "max-age=31536000, immutable"
3711237120
},
3711337121
{
3711437122
"Name": "Content-Encoding",
@@ -37136,9 +37144,17 @@
3713637144
}
3713737145
],
3713837146
"EndpointProperties": [
37147+
{
37148+
"Name": "fingerprint",
37149+
"Value": "__fingerprint__"
37150+
},
3713937151
{
3714037152
"Name": "integrity",
3714137153
"Value": "__integrity__"
37154+
},
37155+
{
37156+
"Name": "label",
37157+
"Value": "blazorwasm-minimal.styles.css.gz"
3714237158
}
3714337159
]
3714437160
},
@@ -37214,7 +37230,7 @@
3721437230
},
3721537231
{
3721637232
"Name": "Cache-Control",
37217-
"Value": "no-cache"
37233+
"Value": "max-age=31536000, immutable"
3721837234
},
3721937235
{
3722037236
"Name": "Content-Encoding",

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_PublishMinimal_Works.Publish.staticwebassets.json

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,7 +5858,7 @@
58585858
},
58595859
{
58605860
"Name": "Cache-Control",
5861-
"Value": "no-cache"
5861+
"Value": "max-age=31536000, immutable"
58625862
},
58635863
{
58645864
"Name": "Content-Encoding",
@@ -5886,9 +5886,17 @@
58865886
}
58875887
],
58885888
"EndpointProperties": [
5889+
{
5890+
"Name": "fingerprint",
5891+
"Value": "__fingerprint__"
5892+
},
58895893
{
58905894
"Name": "integrity",
58915895
"Value": "__integrity__"
5896+
},
5897+
{
5898+
"Name": "label",
5899+
"Value": "blazorwasm-minimal.bundle.scp.css.gz"
58925900
}
58935901
]
58945902
},
@@ -5964,7 +5972,7 @@
59645972
},
59655973
{
59665974
"Name": "Cache-Control",
5967-
"Value": "no-cache"
5975+
"Value": "max-age=31536000, immutable"
59685976
},
59695977
{
59705978
"Name": "Content-Encoding",
@@ -6011,7 +6019,7 @@
60116019
]
60126020
},
60136021
{
6014-
"Route": "blazorwasm-minimal.__fingerprint__.styles.css.gz",
6022+
"Route": "blazorwasm-minimal.styles.css.gz",
60156023
"AssetFile": "${ProjectPath}\\obj\\Debug\\${Tfm}\\compressed\\blazorwasm-minimal#[.{fingerprint=__fingerprint__}]?.styles.css.gz",
60166024
"Selectors": [],
60176025
"ResponseHeaders": [
@@ -6056,7 +6064,7 @@
60566064
]
60576065
},
60586066
{
6059-
"Route": "blazorwasm-minimal.styles.css.gz",
6067+
"Route": "blazorwasm-minimal.__fingerprint__.styles.css.gz",
60606068
"AssetFile": "${ProjectPath}\\obj\\Debug\\${Tfm}\\compressed\\blazorwasm-minimal#[.{fingerprint=__fingerprint__}]?.styles.css.gz",
60616069
"Selectors": [],
60626070
"ResponseHeaders": [
@@ -6066,7 +6074,7 @@
60666074
},
60676075
{
60686076
"Name": "Cache-Control",
6069-
"Value": "no-cache"
6077+
"Value": "max-age=31536000, immutable"
60706078
},
60716079
{
60726080
"Name": "Content-Encoding",
@@ -6094,9 +6102,17 @@
60946102
}
60956103
],
60966104
"EndpointProperties": [
6105+
{
6106+
"Name": "fingerprint",
6107+
"Value": "__fingerprint__"
6108+
},
60976109
{
60986110
"Name": "integrity",
60996111
"Value": "__integrity__"
6112+
},
6113+
{
6114+
"Name": "label",
6115+
"Value": "blazorwasm-minimal.styles.css.gz"
61006116
}
61016117
]
61026118
},
@@ -6172,7 +6188,7 @@
61726188
},
61736189
{
61746190
"Name": "Cache-Control",
6175-
"Value": "no-cache"
6191+
"Value": "max-age=31536000, immutable"
61766192
},
61776193
{
61786194
"Name": "Content-Encoding",
@@ -20474,7 +20490,7 @@
2047420490
},
2047520491
{
2047620492
"Name": "Cache-Control",
20477-
"Value": "no-cache"
20493+
"Value": "max-age=31536000, immutable"
2047820494
},
2047920495
{
2048020496
"Name": "Content-Encoding",
@@ -20502,9 +20518,17 @@
2050220518
}
2050320519
],
2050420520
"EndpointProperties": [
20521+
{
20522+
"Name": "fingerprint",
20523+
"Value": "__fingerprint__"
20524+
},
2050520525
{
2050620526
"Name": "integrity",
2050720527
"Value": "__integrity__"
20528+
},
20529+
{
20530+
"Name": "label",
20531+
"Value": "blazorwasm-minimal.bundle.scp.css.br"
2050820532
}
2050920533
]
2051020534
},
@@ -20580,7 +20604,7 @@
2058020604
},
2058120605
{
2058220606
"Name": "Cache-Control",
20583-
"Value": "no-cache"
20607+
"Value": "max-age=31536000, immutable"
2058420608
},
2058520609
{
2058620610
"Name": "Content-Encoding",
@@ -20682,7 +20706,7 @@
2068220706
},
2068320707
{
2068420708
"Name": "Cache-Control",
20685-
"Value": "no-cache"
20709+
"Value": "max-age=31536000, immutable"
2068620710
},
2068720711
{
2068820712
"Name": "Content-Encoding",
@@ -20710,9 +20734,17 @@
2071020734
}
2071120735
],
2071220736
"EndpointProperties": [
20737+
{
20738+
"Name": "fingerprint",
20739+
"Value": "__fingerprint__"
20740+
},
2071320741
{
2071420742
"Name": "integrity",
2071520743
"Value": "__integrity__"
20744+
},
20745+
{
20746+
"Name": "label",
20747+
"Value": "blazorwasm-minimal.styles.css.br"
2071620748
}
2071720749
]
2071820750
},
@@ -20788,7 +20820,7 @@
2078820820
},
2078920821
{
2079020822
"Name": "Cache-Control",
20791-
"Value": "no-cache"
20823+
"Value": "max-age=31536000, immutable"
2079220824
},
2079320825
{
2079420826
"Name": "Content-Encoding",

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_Publish_DoesNotIncludeXmlDocumentationFiles_AsAssets.Publish.staticwebassets.json

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6122,7 +6122,7 @@
61226122
]
61236123
},
61246124
{
6125-
"Route": "blazorwasm.__fingerprint__.styles.css.gz",
6125+
"Route": "blazorwasm.styles.css.gz",
61266126
"AssetFile": "${ProjectPath}\\blazorwasm\\obj\\Debug\\${Tfm}\\compressed\\blazorwasm#[.{fingerprint=__fingerprint__}]?.styles.css.gz",
61276127
"Selectors": [],
61286128
"ResponseHeaders": [
@@ -6167,7 +6167,7 @@
61676167
]
61686168
},
61696169
{
6170-
"Route": "blazorwasm.styles.css.gz",
6170+
"Route": "blazorwasm.__fingerprint__.styles.css.gz",
61716171
"AssetFile": "${ProjectPath}\\blazorwasm\\obj\\Debug\\${Tfm}\\compressed\\blazorwasm#[.{fingerprint=__fingerprint__}]?.styles.css.gz",
61726172
"Selectors": [],
61736173
"ResponseHeaders": [
@@ -6177,7 +6177,7 @@
61776177
},
61786178
{
61796179
"Name": "Cache-Control",
6180-
"Value": "no-cache"
6180+
"Value": "max-age=31536000, immutable"
61816181
},
61826182
{
61836183
"Name": "Content-Encoding",
@@ -6205,9 +6205,17 @@
62056205
}
62066206
],
62076207
"EndpointProperties": [
6208+
{
6209+
"Name": "fingerprint",
6210+
"Value": "__fingerprint__"
6211+
},
62086212
{
62096213
"Name": "integrity",
62106214
"Value": "__integrity__"
6215+
},
6216+
{
6217+
"Name": "label",
6218+
"Value": "blazorwasm.styles.css.gz"
62116219
}
62126220
]
62136221
},
@@ -6283,7 +6291,7 @@
62836291
},
62846292
{
62856293
"Name": "Cache-Control",
6286-
"Value": "no-cache"
6294+
"Value": "max-age=31536000, immutable"
62876295
},
62886296
{
62896297
"Name": "Content-Encoding",
@@ -20785,7 +20793,7 @@
2078520793
},
2078620794
{
2078720795
"Name": "Cache-Control",
20788-
"Value": "no-cache"
20796+
"Value": "max-age=31536000, immutable"
2078920797
},
2079020798
{
2079120799
"Name": "Content-Encoding",
@@ -20813,9 +20821,17 @@
2081320821
}
2081420822
],
2081520823
"EndpointProperties": [
20824+
{
20825+
"Name": "fingerprint",
20826+
"Value": "__fingerprint__"
20827+
},
2081620828
{
2081720829
"Name": "integrity",
2081820830
"Value": "__integrity__"
20831+
},
20832+
{
20833+
"Name": "label",
20834+
"Value": "blazorwasm.styles.css.br"
2081920835
}
2082020836
]
2082120837
},
@@ -20891,7 +20907,7 @@
2089120907
},
2089220908
{
2089320909
"Name": "Cache-Control",
20894-
"Value": "no-cache"
20910+
"Value": "max-age=31536000, immutable"
2089520911
},
2089620912
{
2089720913
"Name": "Content-Encoding",

0 commit comments

Comments
 (0)