0

I have a single page web app and I use versioning to cache bust the script and other resources that app needs, i.e. in default.htm I link like this:

<script src="js/index.js?plv=1.1"></script>

This works fine, however the issue I run into is that the default.htm page itself is being cached by the browser so it doesn't read the new default.htm and in turn doesn't trigger the update.

The "home" page is being served by the app.UseDefaultFiles() middleware.

I'm thinking a quick and dirty way to fix this issue, is on every code update I publish, I alternate between index.htm and default.htm as the default document and rename the other one so it doesn't get picked up by UseDefaultFiles middleware.

This is a manual step however and surely I'll forget to do it at some point.

How can I handle this situation?

2 Answers 2

3

The solution was obvious in retrospect, the home page is light, no reason to cache it at all:

app.UseStaticFiles(
  new StaticFileOptions
  {
      OnPrepareResponse = context =>
      {                   
          if (context.File.Name == "default.htm" ) {
              context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
              context.Context.Response.Headers.Add("Expires", "-1");
          }
      }
  });
Sign up to request clarification or add additional context in comments.

Comments

3

use the

asp-append-version="true"

attribute on the script tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.