Skip to content

[Blazor] Add vary header to uncompressed endpoints #50246

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

Merged
merged 4 commits into from
Aug 14, 2025
Merged
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
46 changes: 41 additions & 5 deletions src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public override bool Execute()
// the ItemSpec, we want to add the original as well so that it gets re-added.
// The endpoint pointing to the uncompressed asset doesn't have a Content-Encoding selector and
// will use the default "identity" encoding during content negotiation.
if(!HasVaryResponseHeaderWithAcceptEncoding(relatedEndpointCandidate))
{
Log.LogMessage(MessageImportance.Low, " Adding Vary response header to related endpoint '{0}'", relatedEndpointCandidate.Route);

relatedEndpointCandidate.ResponseHeaders = [
..relatedEndpointCandidate.ResponseHeaders,
new StaticWebAssetEndpointResponseHeader
{
Name = "Vary",
Value = "Accept-Encoding"
}
];
}
updatedEndpoints.Add(relatedEndpointCandidate);
}
}
Expand Down Expand Up @@ -122,21 +135,44 @@ public override bool Execute()
foreach (var endpoint in endpoints)
{
Log.LogMessage(MessageImportance.Low, " Adding endpoint '{0}'", endpoint.AssetFile);
}
foreach (var endpoint in endpoints)
{
if (!HasVaryResponseHeaderWithAcceptEncoding(endpoint))
{
endpoint.ResponseHeaders = [
.. endpoint.ResponseHeaders,
new StaticWebAssetEndpointResponseHeader
{
Name = "Vary",
Value = "Accept-Encoding"
}
];
}
additionalUpdatedEndpoints.Add(endpoint);
}
}
}

updatedEndpoints.UnionWith(additionalUpdatedEndpoints);
additionalUpdatedEndpoints.UnionWith(updatedEndpoints);

UpdatedEndpoints = StaticWebAssetEndpoint.ToTaskItems(updatedEndpoints);
UpdatedEndpoints = StaticWebAssetEndpoint.ToTaskItems(additionalUpdatedEndpoints);

return true;
}

private static bool HasVaryResponseHeaderWithAcceptEncoding(StaticWebAssetEndpoint endpoint)
{
for (var i = 0; i < endpoint.ResponseHeaders.Length; i++)
{
var header = endpoint.ResponseHeaders[i];
if (string.Equals(header.Name, "Vary", StringComparison.OrdinalIgnoreCase) &&
header.Value.Contains("Accept-Encoding", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

private static HashSet<string> GetCompressedHeaders(StaticWebAssetEndpoint compressedEndpoint)
{
var result = new HashSet<string>(compressedEndpoint.ResponseHeaders.Length, StringComparer.Ordinal);
Expand Down
Loading
Loading