Nicholas Westby
Nicholas Westby

Reputation: 1230

How to GZIP Compress CSS Files When Server Pushed via HTTP/2 in IIS 10

I am server pushing some CSS files in an ASP.NET MVC site like this in a controller:

public ActionResult Index()
{
    var cssToPush = new[]
    {
        "/bundle/file1.min.css",
        "/bundle/file2.min.css",
    };
    foreach (var file in cssToPush)
    {
        Response.PushPromise(file);
    }
    return View(GetModel());
}

When I do this, I see in the Chrome network tab that the CSS files are not using GZIP compression and they're using 44KB.

When I skip server pushing, the CSS files are using GZIP compression and they are using 14KB.

My theory is that this is because the server doesn't necessarily know if the client will accept GZIP compression, so it responds with uncompressed assets. However, I figure this is super common and there must be some standard way of enabling GZIP compression with server pushed assets. I would consider manually GZIP compressing them on the file system to be non-standard and cumbersome.

Other info:

Upvotes: 2

Views: 859

Answers (2)

bteller
bteller

Reputation: 23

While I don't know if you are still having this issue, what resolved it for me was to pass the Accept-Encoding header back through on the response.

var values = new NameValueCollection();
values.Add("Accept-Encoding", filterContext.HttpContext.Request.Headers.Get("Accept-Encoding"));             
filterContext.HttpContext.Response.PushPromise("~/wwwroot/dist/push_asset.css", "GET", values);    

Upvotes: 2

Jalpa Panchal
Jalpa Panchal

Reputation: 12844

you could try to use iis static and dynamic content compression.

add below code in web.config file:

<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\
temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>

Upvotes: 0

Related Questions