Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 bundling GZIP and headers

I'm testing my site with Google PageSpeed and YSlow and the bundles that i've created with MVC4 bundles aren't getting

Gzipped (Compressing resources with gzip or deflate can reduce the number of bytes sent over the network) and there is no

Vary: Accept-Encoding header (Instructs proxy servers to cache two versions of the resource: one compressed, and one uncompressed. This helps avoid issues with public proxies that do not detect the presence of a Content-Encoding header properly.)

And also how can i add encoding header for the whole scripts folder on the ISS. I know there is HTTP Response Headers, then Add Custom HTTP Response Header,

enter image description here

but will this work on the whole scripts folders and subfolders and what to put in the Name and Value fields.

How can this be solved.

Regards.

like image 912
Matija Grcic Avatar asked Aug 27 '12 08:08

Matija Grcic


4 Answers

make sure you set the following in system.webserver section of your web.config

<urlCompression doDynamicCompression="true"
                    doStaticCompression="true" dynamicCompressionBeforeCache="true" />
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge"
                   cacheControlMaxAge="365.00:00:00" cacheControlCustom="public" />
    </staticContent>
like image 85
pranav rastogi Avatar answered Nov 16 '22 20:11

pranav rastogi


OK good Q,I simple your problem testing.Please try this logic:

  public HttpResponseMessage Get(){

         var request=Request.CreateResponse(HttpStatusCode.OK);
         request.Content.Headers.Add("Content-Type", "application/x-gzip");
         request.Content.Headers.Add("Content-Encoding", "gzip");

    //TODO:Add your logic here...

         return request;
    }
like image 41
Elyor Avatar answered Nov 16 '22 21:11

Elyor


To properly get the JavaScript files on IIS compressed and served with GZip encoding put the following in your web.config.

 <staticContent>

      <remove fileExtension=".js" />
      <mimeMap fileExtension=".js" mimeType="text/javascript" />

      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>

    <urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
    <httpCompression noCompressionForHttp10="false" noCompressionForProxies="false" dynamicCompressionDisableCpuUsage="93" dynamicCompressionEnableCpuUsage="93" staticCompressionDisableCpuUsage="99" staticCompressionEnableCpuUsage="99">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" dynamicCompressionLevel="4" />
    </httpCompression>
</system.webServer>

And then on your ISS in MIME Types

enter image description here

change the application/x-javascript to text/javascript

enter image description here

You will now see in DevTools that JS files are being served with gzip in Content Encoding column.

enter image description here

like image 25
Matija Grcic Avatar answered Nov 16 '22 20:11

Matija Grcic


I think that IIS Dynamic Content Compression should be taking care of the gzipping at least, and maybe even all of this for you, have you tried this feature?

This msdn article might be helpful

like image 1
Hao Kung Avatar answered Nov 16 '22 19:11

Hao Kung