使用React(CRA)的ASP.NET Core 2.1中的响应压缩

时间:2018-05-01 13:14:30

标签: asp.net-core asp.net-core-2.1

这是执行响应压缩的官方文档:https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.1&tabs=aspnetcore2x

我按照文档发现只有在调试时它才有效(index.html,bundle.js被压缩)。发布后,该应用程序没有压缩任何东西。我该怎么做才能让它在生产中发挥作用。

// Program.cs

public class Program
{
    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddAppSettingsFromDockerSecrets();
            })
            .ConfigureServices(services =>
            {
                services.AddResponseCompression();
                services.AddResponseCaching();
            })
            .Configure(app =>
            {
                app.UseResponseCompression();
                app.UseResponseCaching();
                app.Use(async (context, next) =>
                {
                    context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromSeconds(5),
                    };
                    context.Response.Headers[HeaderNames.Vary] = new string[] { "Accept-Encoding" };
                    await next();
                });
            })
            .UseStartup<Startup>();
    }

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }
}

// Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        this.Configuration = configuration;
        this.Logger = logger;
    }

    public IConfiguration Configuration { get; }

    public ILogger Logger { get; }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }
}

0 个答案:

没有答案