3

I have seen a number of articles suggesting the use of a query string to force refresh of a cached file. For example, style.css becomes style.css?ver=134, and you update the "version number" whenever you change the file.

Are there any potential problems with this method? If so, is there an alternative?

2

1 Answer 1

5

This idea is called "cache busting", and there aren't any problems with your method that I'm aware of. It's a common, solid method that works reliably.

The other common alternative, of course, is to version the file path itself (e.g. /styles/v134/style.css or /styles/style-v134.css).

There's one final method that I've found useful in a niche circumstance: static site generators. If you have a static generated site that you don't update that often, you can have the site generator append the site's build time to URLs for all your versioned resources, e.g. /styles/style.css?t=(unix-time-stamp). This has the advantage of you not needing to manually update your file versions when each file changes, but has the disadvantage of cache busting ALL your resources every time you push an update.

3
  • 1
    A static site generator should just hash the contents of the resource if it doesn't want to concern itself with version-keeping.
    – Bergi
    Apr 11 at 0:38
  • @Bergi It really should, and my bet is that modern ones do. Mine doesn't support it natively though (Jekyll). Apr 11 at 2:51
  • 1
    "A static site generator should just hash the contents of the resource if it doesn't want to concern itself with version-keeping." Which is what basically the HTTP etag is about... It allows to keep a proper URL without having to insert true or fake version numbers in the URL itself. Apr 11 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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