4

There are a lot of questions and answers on SO related to my problem [I want the browser to cache js/css forever. During a new release if some of the js/css files have been updated, the browser should reload and cache them.]

This solution seemed most appropriate to me : What is an elegant way to force browsers to reload cached CSS/JS files?

However, there is just one thing that I am unable to figure out. The solution makes use of last_modified_time. However, I am not allowed to use it. I need to use some other mechanism.

What are the options? Is there a possibility of pre-calculating the versions during build and updating(replacing) them in jsps via build script (before deployment, so that the version numbers are not calculated on run time)? Any existing tool for this purpose? I use Java/Jsp.

Community
  • 1
  • 1
TJ-
  • 14,085
  • 12
  • 59
  • 90
  • A bit annoying solution, but you can save revision number in file and dynamically append it to css file name via PHP. But every time you want to force user realod CSS you need edit this file. – neworld Aug 16 '12 at 12:38
  • 1
    "However, I am not allowed to use it" Why? – GolezTrol Aug 16 '12 at 12:39
  • @GolezTrol because of some of the legacy build processes. The last modified time of all the files in the deployment changes immaterial of they being changed. – TJ- Aug 16 '12 at 12:42
  • @neworld Are you suggesting that I keep a repository of ? Or should I store the revision number in the css file itself (that could be done using SVN for the time being). On each reference, I read this file and use the version number in the reference? – TJ- Aug 16 '12 at 12:48
  • @TJ-: Doesn't matter where you store it. If you want, you can store in DB. This method has advantage, if you make not very important upgrade, you can do not force to realod (modified comments or CSs formating). – neworld Aug 16 '12 at 12:51
  • okay, I think I will agree. Suppose I store the versions in the db. Is there a process/tool to ensure that the developers update the db whenever they modify a css/js file? I am looking for a more end to end solution. – TJ- Aug 16 '12 at 12:55

3 Answers3

2

We always use

file.css?[deploytimestamp]

This way the CSS file is cached for each deployment at the client. The same goes for our minified javascript. Is this an option for you?

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • 1
    That is possible but I would not like the js/css to be reloaded if it has not changed across deployments. – TJ- Aug 16 '12 at 12:44
  • It wouldn't. `file.css?v=1` would be cached and not reloaded, then you change it to v=2 and `file.css?v=2` gets cached once then used from cache (until changing version again...)) –  Aug 29 '12 at 07:49
  • @DuncanNZ - but that's not what David is suggesting - he's appending the timestamp of the deployment not the version. – UpTheCreek Apr 15 '13 at 10:28
  • How can I place deploytimestamp automatically in all js references in asp.net webforms? – Emad Armoun Apr 05 '16 at 06:32
2

It may not be the best way, but this is what I am doing now:

  1. All of my js/css have a [source control = svn] revision number
  2. References in my jsp are like /foo/path1/path2/xyz000000/foo.
  3. Build Step 1 - Generate a map of css|js files and their revision numbers
  4. Build Step 2 - Replace xyz000000 references in jsps with a hash of svn revisions
  5. A rule in url rewriter to direct all /foo/path1/path2/xyz<767678>/foo. to /foo/path1/path2/foo.[js|css]
  6. Infinitely cache the css|js files
  7. Whenever there is a commit, the revision number changes and so do the references in .jsp
TJ-
  • 14,085
  • 12
  • 59
  • 90
1

Generate an md5-hash of each css file after deployment. Use this hash instead of the timestamp in the url of the css.

file.css?[hash of file.css contents]

It may be wise to calculate the hashes once after deployment and store them to gain some performance. You could store them in a database, or even in a PHP array in a separate file that is included in your website code.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210