search

Home  >  Q&A  >  body text

前端 - 关于浏览器缓存html文件

最近在学习浏览器的缓存机制,看到有一种方案,使用强缓存,资源名用文件名+md5来命名,实现资源的更新,对于这个方案有几点疑问:

  1. 是否每次修改了资源,都要在html中修改link标签,把url指向最新的资源。这样不会很麻烦吗?尤其是同一个资源被无数html引用的时候(所以会有类似的工具/脚本?)

  2. 浏览器也会缓存这个html文件,那么html文件自身如何更新呢?比如我用node写个服务器,可能一句app.use('/', index);就好了,但是每次浏览器也会对html进行缓存,如果用到上述这种方案的话,是否应该在服务器这里做出改动,让所有返回的html文件,都不使用强缓存,这样每次就可以加载到最新修改的html了。(我也查看了几家大网站的F12,有的html就是200,比如百度和网易,新浪这种还是返回的304)

  3. 可以讲一讲详细的方案吗。。查看很多资料都是一句“资源名+md5命名,每次加载最新的资源”,初学者在实现上有些看不懂。。。

ringa_leeringa_lee2785 days ago473

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 15:30:00

    -> 1. Every time the resource is updated, the HTML needs to be updated. This is done through code. For example, the HTML page is a template engine, and the md5 file suffix is ​​generated by Webpack. Just render it.

    -> 2. See the specific strategy. If it is a static file, the update time or etag of the file is generally checked through the HTTP protocol. If it is not updated, it will return 304, if it is updated, it will return 200. The HTML file is placed on the server, and the static resources are placed together. CDN generates 200 (from cache) to distinguish. If the page needs to be rendered and generated for each request, it is usually 200.

    -> 3. The whole process is very troublesome. It will change depending on the business and tool chain. It is too complicated and most people are not in the mood to talk about it. It is recommended to read foreign blogs, such as https://medium.com/@ okonetchn...

    reply
    0
  • Cancelreply