13

I have prepared all the settings (at my home , Windows 10) for creating and serving js as gz files , but still - I get only the regular js files (with the original size).

Configuration

Angular's webpack file :

new CompressionPlugin({
      asset: "[path].gz[query]",
     algorithm: "gzip",
     test: /\.js$|\.css$|\.html$/,
     threshold: 10240,
     minRatio: 0.8
 })

Output files for that configuration:

enter image description here

Index.html file:

...
<body>
  <app-root>Loading...</app-root> 
  <script src="/dist/polyfills.bundle.js"></script>
  <script src="/dist/main.bundle.js"></script>
</body>
...

Diagnostics

When I navigate to http://kkk.com/index.html , I get the full size files :

enter image description here

Also - looking at the request headers , I do send the Accept-Encoding:gzip, deflate header :

enter image description here

Question

Why doesn't the GZ files served ?


Additional info :

  • There is no 304 responses , I set enter image description here.
  • At my work , windows 7 , same files - I DO(!) see the gziped files :

enter image description here

  • Disabled Antivirus
  • Permissions : Everyone : full control on the file's folder.
  • Comparison between request headers :

enter image description here

  • Since I don't want real time zipping , but PRE-ZIPPING ( files which already gzipped by angular) - I tried enabling AND disabling compression(just to see if it affects something - but it doesn't - I still get the big files) :

enter image description here

Royi Namir
  • 131,490
  • 121
  • 408
  • 714
  • Hi @yurzui , Yeah but I don't want dynamic Gzipped files. The webpack already creates them for me , but for some reason , in my win10 PC - it doesn't serve the GZ files , while in my job - (win7) - it does – Royi Namir Jul 23 '17 at 12:31
  • @yurzui this is my [config file](http://pasted.co/cda4e737) – Royi Namir Jul 23 '17 at 12:38
  • @yurzui thanks for the effort. Still it's not working - but please read my latest comment (to the answer) i've found out that it never actually served my files but the generated files. https://i.stack.imgur.com/pOiwK.png – Royi Namir Jul 25 '17 at 07:59

2 Answers2

7

OK. After searching a lot - I've managed to do it with URLREWRITE and with some IIS configuration.

First - I've disabled this :

enter image description here

Because I don't need CPU here. I already have the pre zipped files.

OK - the key here is those two sections:

1) Set contentType to application/javascript
2) Set contentEncoding to gzip.

So I've written these 2 urlrewrite rules :

The first section is to rewrite all js files to js.gz and the second rule which is an outboundrule is for adding the content encoding header with gzip value.

This is the config file :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="https" enabled="true" stopProcessing="false">
                    <match url="(.*).js" />
                    <conditions></conditions>
                    <action type="Rewrite" url="{R:1}.js.gz" appendQueryString="true" logRewrittenUrl="true" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Rewrite content-encoding header" preCondition="IsGZ" stopProcessing="false">
                    <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                    <action type="Rewrite" value="gzip" />
                </rule>
                <preConditions>
                    <preCondition name="IsGZ">
                        <add input="{URL}" pattern="\.gz$" />
                    </preCondition>


        </preConditions>
        </outboundRules>
    </rewrite>
    <urlCompression doStaticCompression="false" />
</system.webServer>

Just paste it in the web.config file (even if you don't use Asp.net).

Also - you have to add this to the mime types :

enter image description here

Now , as you can see the response I do get the right size :

enter image description here

Going deeper :

enter image description here

Which is the one I have as preziiped :

enter image description here

That's all.

Royi Namir
  • 131,490
  • 121
  • 408
  • 714
  • 1
    hmmmm, what if I want to pre-comp all the file of my site, include css, html? we cant set the same ext. name to two mime type, is there any work around? – Losses Don Feb 27 '18 at 00:33
  • A most excellent answer. Worked great for me. Thank you. I too am interested if there is a workaround for serving gziped css and html. I created a separate rewrite rule but @LossesDon is right, there isn't any way to fix the MIME type being set to application/javascript that I can see. I'm guessing there isn't anything that can be done about that. – mikeo Mar 23 '18 at 22:35
  • @mikeo Hey, just check my answer under another question: https://stackoverflow.com/questions/48889701/setup-iis10-to-serve-pre-compressed-files/49334896#49334896 – Losses Don Mar 24 '18 at 01:21
2

IIS does not support pre-compressed files. you could workaoround this fairly simply by using a solution from here: https://github.com/aspnet/StaticFiles/issues/7

the problem is that IIS is serving content as is or it compresses then and the adding the encoding header.

If you add the static compression to IIS it will gzip your files the second time. If you will not add static compression it will send them as is but will not add the encoding header.

so what you want is to hijack the request and manipulate the response grammatically.

here is an OWIN base implementation example. you could as easily use an HTTP handler for older version..

class Startup
{
    private StaticFileOptions StaticFileOptions
    {
        get
        {
            return new StaticFileOptions
            {
                OnPrepareResponse = OnPrepareResponse
            };                
        }
    }

    private void OnPrepareResponse(StaticFileResponseContext context)
    {
        var file = context.File;
        var request = context.Context.Request;
        var response = context.Context.Response;

        if (file.Name.EndsWith(".gz"))
        {
            response.Headers[HeaderNames.ContentEncoding] = "gzip";
            return;
        }

        if (file.Name.IndexOf(".min.", StringComparison.OrdinalIgnoreCase) != -1)
        {
            var requestPath = request.Path.Value;
            var filePath = file.PhysicalPath;

            if (IsDevelopment)
            {
                if (File.Exists(filePath.Replace(".min.", ".")))
                {
                    response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;
                    response.Headers[HeaderNames.Location] = requestPath.Replace(".min.", ".");
                }
            }
            else
            {
                var acceptEncoding = (string)request.Headers[HeaderNames.AcceptEncoding];
                if (acceptEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    if (File.Exists(filePath + ".gz"))
                    {
                        response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                        response.Headers[HeaderNames.Location] = requestPath + ".gz";
                    }
                }
            }
        }
    }

   public void Configure(IApplicationBuilder application)
   {
        application
            .UseDefaultFiles()
            .UseStaticFiles(StaticFileOptions)
    }
}
Mortalus
  • 10,046
  • 9
  • 60
  • 108
  • As I've already mentioned - in my windows 7 with IIS - it does serve the GZ files. And i've also shown it in my question ( there's a picture). https://i.stack.imgur.com/5NXzD.png . also i'm not using any C# here. it's plain html file with 2 scripts. – Royi Namir Jul 23 '17 at 12:48
  • 1
    sorry about that.. in IIS 7.5 there is that: https://stackoverflow.com/questions/6938713/gzip-compression-on-iis-7-5-is-not-working but it will not help you as you are looking at pre-compressed files and not letting IIS compress them realtime for you... – Mortalus Jul 23 '17 at 12:52
  • 1
    anyway you should not dwell on this as you are looking to serve pre-compressed files.. i once did the same in my .net core application and the workaround was perfect.. – Mortalus Jul 23 '17 at 12:54
  • 1
    @RoyiNamir `As I've already mentioned - in my windows 7 with IIS - it does serve the GZ files.` It does not mean that the IIS serving **your** gzipped files but the built-in compression module (which you don't want) works. – Kul-Tigin Jul 23 '17 at 14:55
  • This answer is havening non-relevant code ! My site is pure html + js without any server side code. And no middleware. – Royi Namir Jul 30 '17 at 08:47