Cache Busting Index.html In A .Net Core Angular 5 Website
Answer :
Here's what I ended up with after combining a bunch of answers. My goal was to never cache index.html. While I was in there, and since Angular nicely cache-busts the js
and css
files, I had it cache all other assets for a year.
Just make sure you're using a cache-busting mechanism for assets, like images, that you're managing outside of Angular.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseStaticFiles(); if (env.IsDevelopment()) { // no caching app.UseSpaStaticFiles(); } else { app.UseSpaStaticFiles(new StaticFileOptions { OnPrepareResponse = context => { context.Context.Response.Headers.Add("Cache-Control", "max-age=31536000"); context.Context.Response.Headers.Add("Expires", "31536000"); } }); } // ... app.UseSpa(spa => { spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions { OnPrepareResponse = context => { // never cache index.html if (context.File.Name == "index.html") { context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store"); context.Context.Response.Headers.Add("Expires", "-1"); } } }; }); }
Other StackOverflow Answers: Disable Caching in .Net Core | Cache for a year
I add the following lines to the index.html
head section, to prevent the file from being cached:
<meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache">
Not a neat or perfect solution but this seems to have worked and might get people on the right track:
In Configure() in Startup.cs I added this
app.Use(async (c, next) => { if (c.Request.Path == "/") { c.Response.Headers.Add("Cache-Control", "no-store,no-cache"); c.Response.Headers.Add("Pragma", "no-cache"); } await next(); });
Since adding this I haven't been able to reproduce my issue.
Comments
Post a Comment