1

I would like to cache images, but not certain files like CSS and JS. I only found out how to not cache everything, is there a way to selectively cache whichever I want?

// meta tags to not store cache
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Expires" content="-1">

// force reload cache in JS
location.reload(true);
2
  • Send appropriate cache control headers from the server. Commented Feb 11, 2014 at 9:13
  • You likely want to version the js and css files. Then new files will be cached when you change them Commented Feb 11, 2014 at 10:09

1 Answer 1

2

You do this with server configuration, not JavaScript or meta tags. Configure your server to return far-futures cache headers for images, but Cache-Control: must-revalidate and such for JS and CSS.

But seriously consider whether you really want to force your users to re-request the JS and CSS on every page visit. Assuming the HTML content changes regularly, it may be better to just make them revalidate the HTML, and use versioned paths to the JS (script.v2.js vs. script.v3.js) and CSS (style.v2.css vs. style.v3.css) served with far-futures expires headers. The HTML entry page can then refer to the up-to-date versions of the files when you change them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip. True if there we're many pages, but the site I am working on is only one long page. The CSS, HTML content and JS are updated daily, rarely any the images, so I don't want user to accidentally load a cached JS with new HTML or cached CSS and so on. I want to make sure the user always the updated code.
@SamiA.: As I said above, if you're updating the HTML, you can allow caching of JS and CSS by using versioned paths. No need to prevent caching the JS and CSS.

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.