Friday, 19 May 2017

Add md5 hash to file URL for optimal caching in Node.js

Appending a file's hash to it's url seems like a common thing to do to ensure files can have infinitely far cache expiration dates without causing problems when a new version of the file is available.

How is this task usually accomplished in Node.js?

Being clearer

Instead of asking the browser to load main.js, I would ask for main.085cc38ce00780e9365ee07275bfb8d8.js, where 085cc38ce00780e9365ee07275bfb8d8 is the file hash. So that when I change the content of my main file, the hash will also change and it will be as if the HTML were requesting a completely different file.

The solution I'm looking for must tackle:

  • Renaming the file or mapping the name-with-hash to name-without-hash
  • Getting my HTML markup to use the correct file name with hash.

For context: My current build pipeline uses Gulp and docker, so I rebuild everything on every change, but I am open for any solution.



via Marcelo Lazaroni

No comments:

Post a Comment