Check browser's cache for a js file

前端 未结 4 1640
予麋鹿
予麋鹿 2020-12-25 09:22

How can I check for a javascript file in user\'s cache. If he refreshed the page or visits the site after sometime. I need not download that js file again. Does the js file

4条回答
  •  醉话见心
    2020-12-25 09:44

    The browser will automatically look after what it has in it's own cache. There are various mechanisms you can use for controlling it however.

    Look into the various HTTP caching headers, such as:

    • Last-Modified
    • Expires
    • ETag

    The Expires header is the most critical of these when it comes to client-side caching. If you set a far future Expires header (eg, 10 years) the browser will not (in theory) not look to the server again for that file. Of course then you need a method of changing the file name when the contents of the the file change. Most people manage this by adding a build number to the file path.

提交回复
热议问题