Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

webpack - Why MD5 hashing is being frequently used for preventing of loading of outdated CSS, JavaScript and media files?

I understand why we need to make the names of CSS, JavaScript and media files like images unique when building the project for production. I don't understand why the using of MD5 for this effect reaching is so popular, for example:

To generate the filename like main.7e2c49a622975ebd9b7e.js, we need to read the file and create the MD5 has of the content. Seems like it will take a lot of time, especially in HD images and videos case.

There are some other alternatives how to generate the unique file name:

  1. Using the UNIX timestamp
  2. Using the Math.random()

Just two instances, but I don't see the reason to explore more.

question from:https://stackoverflow.com/questions/65866710/why-md5-hashing-is-being-frequently-used-for-preventing-of-loading-of-outdated-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A primary reason is that if the files contents don't change, the hash won't change. And if the hash doesn't change that means your users' browsers won't have to download the files after your next deployment (as the browser has cached the file from a previous pageload).

If you used timestamp, every filename would change for every deployment, resulting in your users' browsers having to redownload every file in your project...even files whose content did not change. This can have a hugely negative performance hit if you are deploying code frequently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...