@@ -78,6 +78,19 @@ public override bool Execute()
78
78
// the ItemSpec, we want to add the original as well so that it gets re-added.
79
79
// The endpoint pointing to the uncompressed asset doesn't have a Content-Encoding selector and
80
80
// will use the default "identity" encoding during content negotiation.
81
+ if ( ! HasVaryResponseHeaderWithAcceptEncoding ( relatedEndpointCandidate ) )
82
+ {
83
+ Log . LogMessage ( MessageImportance . Low , " Adding Vary response header to related endpoint '{0}'" , relatedEndpointCandidate . Route ) ;
84
+
85
+ relatedEndpointCandidate . ResponseHeaders = [
86
+ ..relatedEndpointCandidate . ResponseHeaders ,
87
+ new StaticWebAssetEndpointResponseHeader
88
+ {
89
+ Name = "Vary" ,
90
+ Value = "Accept-Encoding"
91
+ }
92
+ ] ;
93
+ }
81
94
updatedEndpoints . Add ( relatedEndpointCandidate ) ;
82
95
}
83
96
}
@@ -122,21 +135,44 @@ public override bool Execute()
122
135
foreach ( var endpoint in endpoints )
123
136
{
124
137
Log . LogMessage ( MessageImportance . Low , " Adding endpoint '{0}'" , endpoint . AssetFile ) ;
125
- }
126
- foreach ( var endpoint in endpoints )
127
- {
138
+ if ( ! HasVaryResponseHeaderWithAcceptEncoding ( endpoint ) )
139
+ {
140
+ endpoint . ResponseHeaders = [
141
+ .. endpoint . ResponseHeaders ,
142
+ new StaticWebAssetEndpointResponseHeader
143
+ {
144
+ Name = "Vary" ,
145
+ Value = "Accept-Encoding"
146
+ }
147
+ ] ;
148
+ }
128
149
additionalUpdatedEndpoints . Add ( endpoint ) ;
129
150
}
130
151
}
131
152
}
132
153
133
- updatedEndpoints . UnionWith ( additionalUpdatedEndpoints ) ;
154
+ additionalUpdatedEndpoints . UnionWith ( updatedEndpoints ) ;
134
155
135
- UpdatedEndpoints = StaticWebAssetEndpoint . ToTaskItems ( updatedEndpoints ) ;
156
+ UpdatedEndpoints = StaticWebAssetEndpoint . ToTaskItems ( additionalUpdatedEndpoints ) ;
136
157
137
158
return true ;
138
159
}
139
160
161
+ private static bool HasVaryResponseHeaderWithAcceptEncoding ( StaticWebAssetEndpoint endpoint )
162
+ {
163
+ for ( var i = 0 ; i < endpoint . ResponseHeaders . Length ; i ++ )
164
+ {
165
+ var header = endpoint . ResponseHeaders [ i ] ;
166
+ if ( string . Equals ( header . Name , "Vary" , StringComparison . OrdinalIgnoreCase ) &&
167
+ header . Value . Contains ( "Accept-Encoding" , StringComparison . OrdinalIgnoreCase ) )
168
+ {
169
+ return true ;
170
+ }
171
+ }
172
+
173
+ return false ;
174
+ }
175
+
140
176
private static HashSet < string > GetCompressedHeaders ( StaticWebAssetEndpoint compressedEndpoint )
141
177
{
142
178
var result = new HashSet < string > ( compressedEndpoint . ResponseHeaders . Length , StringComparer . Ordinal ) ;
0 commit comments