如何在自宿主环境中在ASP.NET Core中gzip静态内容 [英] How to gzip static content in ASP.NET Core in a self host environment

查看:414
本文介绍了如何在自宿主环境中在ASP.NET Core中gzip静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用自托管环境发布ASP.NET Core网站时,是否可以提供gzip静态内容?

Is there are way to serve gzip static cotent when using self host environment to publish an ASP.NET Core website?

推荐答案

还有另一种服务于压缩文件的方法,它代替了步骤2和3.这基本上是相同的想法,但是有一个nuget 程序包,可为您轻松完成所有操作.它基本上检查.gz.br文件是否与请求的文件匹配.如果存在,则将其与适当的标头一起返回.它确实验证请求是否具有相应算法的标头.如果您想自己编译Github代码,请在此处.

There is another way to serve gzipped files that replaces steps 2 and 3. It's basically quite the same idea, but there is a nuget package that does it all for you readily available. It basically checks if the is .gz or .br file that matches the requested one. If it exists it returns it with the appropriate headers. It does verify that the request has a header for the corresponding algorithm. Github code if you want to compile it yourself is here.

在官方的存储库中,还有一个问题需要支持,所以我真的希望Microsoft将拥有标准的插件来执行此操作,因为如今使用该插件已经相当普遍且合乎逻辑.

There is also an issue to support that in the official repository, so I really hope Microsoft will have the standard plugin to do that, since it's rather common and logical to use that nowadays.

我想我已经找到了提供压缩内容的最优化方法.主要思想是对文件进行预压缩,并且由于默认的ASP.NET 5方法是使用gulp构建js,因此这样做很容易:

I think I have found the most optimized way of serving the compressed content. The main idea is to pre-compress the files and since the default ASP.NET 5 way is to use gulp to build js, it is as easy to do as this:

gulp.task("buildApplication:js", function () {
    return gulp.src(...)
        ...
        .pipe(gzip()) 
        ...
});

这将在您的bundles文件夹中生成类似libs.js.gz的内容

This will produce something like libraries.js.gz in your bundles folder

我们需要添加Content-Encoding并将Content-Type从默认application/x-gzip更改为application/javascript,因为并非所有浏览器都足够聪明,可以正确地从x-gzip

We need to add Content-Encoding and change the Content-Type from default application/x-gzip to application/javascript because not all browsers are smart enough to read js properly from x-gzip

app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = context =>
        {  
            if (headers.ContentType.MediaType == "application/x-gzip")
            {
                if (context.File.Name.EndsWith("js.gz"))
                {
                    headers.ContentType = new MediaTypeHeaderValue("application/javascript");
                }
                else if (context.File.Name.EndsWith("css.gz"))
                {
                    headers.ContentType = new MediaTypeHeaderValue("text/css");
                }

                context.Context.Response.Headers.Add("Content-Encoding", "gzip");
            }
        }
    });

现在,没有任何CPU周期可以浪费所有时间来压缩同一个内容,这是提供文件时最好的性能.为了进一步改善它,必须在gzip压缩之前将所有js捆绑和最小化.另一个升级是将同一OnPrepareResponse中的CacheControl最长期限设置为要缓存一年,然后在cshtml中添加asp-append-version="true".

Now all there is no CPU cycles to waste to gzip the same content all the time and it's the best possible performance in serving the files. To improve it even further all of js has to be bunlded and minified before gzipping. Another upgrade is to set CacheControl max age in the same OnPrepareResponse to cache for one year and add asp-append-version="true" in the cshtml.

P.S.如果您将宿主在IIS后面,则可能需要关闭js和css的静态压缩,而不要进行双重压缩,因此我不确定在这种情况下它的行为.

P.S. If you will host behind IIS you might need to turn off the static compression of js and css not to double compress, I am not sure how it will behave in this situation.

这篇关于如何在自宿主环境中在ASP.NET Core中gzip静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆