14

I'm working on a moderately-sized web application and trying to come up with the best solution to make all browsers use the cache and only invalidate it when there is an update to the asset being loaded.

According to the research I've done here and elsewhere, everyone seems to be in agreement that appending a ?v={version#} to an asset such as a css or js file is a great way to automatically invalidate the cache when an asset is updated. (As per Force browser to clear cache and Better way to prevent browser caching of JavaScript files)

But it seems to me that this solution should be generalized to all assets that reside on a web server.

So my question is, would it be a good practice to have a build script look through each src="" attribute across the entire website -- whether img, css, or js, and programmatically append ?={timestamp} where timestamp is the time when the file is last modified. This way whenever you push from dev to staging to production, only those files that have been modified will have a changed time stamp, and the browser will know to invalidate the cache for those files.

Any flaws with that approach?

NOTE: Thinking this over a bit more, timestamp would definitely be undesirable in the case of changes that are later reverted. Therefore, appending ?={md5(filecontents)} is a more robust approach. Nevertheless, the question about whether implementing this across all assets and all builds still stands.

2
  • Interesting question. Looking forward to the answer:)
    – null
    Jul 21, 2013 at 23:44
  • @ajkochanowicz That is not a general solution, even if RoR handles this for you. The problem I pose applies to all web servers and all development environments. A good question to ask would be, how does RoR implement its own solution, and does it generalize to all development environments? Because of how ubiquitous the problem is, there should be something on the level of an open source solution to address it. Jul 22, 2013 at 16:23

1 Answer 1

8

Found what I believe to be an acceptable solution at How to force browser to reload cached CSS/JS files? No idea how I missed this in my original investigation.

For anyone who comes to this question, note I'm referring to the first answer on the linked page that references Google's mod_pagespeed plugin for apache. This works at the web server level, thus "[it works] with PHP, rails, python, static HTML -- anything."

This is precisely the kind of solution I was looking for. This tool, or something similar, should be in use by all web developers to keep caching logic orthogonal to the code itself.

1
  • 1
    Just to clarify for anyone like me that has found their way here, the link to the answer @PeterThorpe is referring to is here
    – doz87
    Dec 21, 2016 at 2:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.