1

I have a 4mb js file (gzipped version is 900kb). The file is like this: main.328479fdsdf.js (so it has a hash based on the content).

IIS seems to compress my big js file only if is accessed 2 times in 10 seconds. However if I have a user on 3G every 1 minute he will have to wait around 30 seconds for the website to open. If is GZIPPED, the user waits only 12 seconds.

I know that GZIP should not be for all the content, but for my specific file I need to always send it over the networked compressed. How can I achieve that?

Alin C
  • 111
  • 2

1 Answers1

0

I have found a workaround. I feel is a hackish solution, but it fixed my problem.

My idea was to create a Windows Service that will do a request every couple of seconds to make sure IIS will always consider my JavaScript files as frequent hits and to always have a compress version of them:

class JsScriptCompressKeeper
{
    private bool keepRunning = true;
    public void Start()
    {
        var thread = new Thread(Run);
        thread.Start();
    }

    public void Stop()
    {
        keepRunning = false;
    }


    public void Run()
    {
        var mainUrl = "www.example.com";
        var pingTimeout = 7;  //seconds
        // my js filenames are like babel-polyfill.asd123eqwed.js and main.113edweq.js 
        var patterns = new[] { @"babel-polyfill.([A-Za-z0-9\-]+).js", @"main.([A-Za-z0-9\-]+).js"};

        using (var client = new WebClient())
        {
            // parse the HTML and find my JS files
            var mainHtml = client.DownloadString(new Uri(mainUrl));
            var urls = patterns.Select(pattern =>
            {
                var match = Regex.Match(mainHtml, pattern);
                var fileName = match.Groups[0].Value;
                return new Uri($"{mainUrl}/{fileName}");
            }).ToList();

            while (true)
            {
                if (!keepRunning)
                {
                    break;
                }

                Thread.Sleep(TimeSpan.FromSeconds(pingTimeout));
                urls.ForEach(url =>
                {
                    client.DownloadString(url);
                });
            }
        }

    }
}

P.S. I feel my solutions is too hackish, so I will not accept my answer. Maybe someone else has a cleaner solution! :)

Alin C
  • 111
  • 2