user2906759
user2906759

Reputation: 831

Why not use headers instead of cache busters?

When you read about preventing cache on deployment (if a file has changed) the solutions are often to add an incrementing query string (file.js?v=123) or rename the file to a MD5 hash of the file's contents using a build script (for example https://www.npmjs.org/package/grunt-cachebuster).

Why not use Last-Modified or ETag to solve it instead? What are the disadvantages?

Upvotes: 0

Views: 58

Answers (1)

deceze
deceze

Reputation: 522597

Sure, you can use Last-Modified and ETag instead. However, using these cache mechanisms requires a roundtrip HTTP query for these resources every single time. Which can be pretty wasteful. Instead, you really want the browser to keep a local copy and not bother checking in with the server at all for as long as possible.

Last-Modified and ETag mostly just avoid the bandwidth cost of repeatedly transferring the file over the network.
Expires or Cache-Control headers reduce the effective traffic to zero, saving both time and bandwidth; at the cost of risking using outdated data. Uniquely identifying each version of a file helps solve that.

Upvotes: 2

Related Questions