Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...