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?

MrWhite
  • 42,784
  • 4
  • 49
  • 90
CaptainCodeman
  • 558
  • 1
  • 6
  • 12

1 Answers1

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.

Maximillian Laumeister
  • 15,972
  • 3
  • 31
  • 62
  • 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 '22 at 00:38
  • @Bergi It really should, and my bet is that modern ones do. Mine doesn't support it natively though (Jekyll). – Maximillian Laumeister Apr 11 '22 at 02: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. – Patrick Mevzek Apr 11 '22 at 04:49