@@ -78,6 +78,19 @@ public override bool Execute()
7878 // the ItemSpec, we want to add the original as well so that it gets re-added.
7979 // The endpoint pointing to the uncompressed asset doesn't have a Content-Encoding selector and
8080 // 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+ }
8194 updatedEndpoints . Add ( relatedEndpointCandidate ) ;
8295 }
8396 }
@@ -122,21 +135,44 @@ public override bool Execute()
122135 foreach ( var endpoint in endpoints )
123136 {
124137 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+ }
128149 additionalUpdatedEndpoints . Add ( endpoint ) ;
129150 }
130151 }
131152 }
132153
133- updatedEndpoints . UnionWith ( additionalUpdatedEndpoints ) ;
154+ additionalUpdatedEndpoints . UnionWith ( updatedEndpoints ) ;
134155
135- UpdatedEndpoints = StaticWebAssetEndpoint . ToTaskItems ( updatedEndpoints ) ;
156+ UpdatedEndpoints = StaticWebAssetEndpoint . ToTaskItems ( additionalUpdatedEndpoints ) ;
136157
137158 return true ;
138159 }
139160
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+
140176 private static HashSet < string > GetCompressedHeaders ( StaticWebAssetEndpoint compressedEndpoint )
141177 {
142178 var result = new HashSet < string > ( compressedEndpoint . ResponseHeaders . Length , StringComparer . Ordinal ) ;
0 commit comments