VueJS/browser caching production builds

后端 未结 6 1609
抹茶落季
抹茶落季 2020-12-02 12:44

I have a VueJS app. Whenever I run npm run build it creates a new set of dist/* files, however, when I load them on the server (after deleting the

6条回答
  •  悲&欢浪女
    2020-12-02 12:48

    If you use asp.net core you can try the following trick among with the webpack which generates js files with the hash at the end of the name eg. my-home-page-vue.30f62910.js. So your index.html contains: which means whenever you change the my-home-page.vue it will generate a new hash in the filename.

    The only thing you need is to add a cache restriction against the index.html

    In your Startup.cs:

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        // ....
        app.UseStaticFiles(new StaticFileOptions
        {
          // Make sure your dist folder is correct
          FileProvider = new PhysicalFileProvider(Path.Combine(_env.ContentRootPath, "ClientApp/dist")),
          RequestPath = "",
          OnPrepareResponse = context =>
          {
            if (context.Context.Request.Path.StartsWithSegments("/index.html", StringComparison.OrdinalIgnoreCase))
            {
              context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
              context.Context.Response.Headers.Add("Expires", "-1");
            }
          },
        });
        // ....
    }
    

提交回复
热议问题