how to cache css, images and js?

前端 未结 6 2172
攒了一身酷
攒了一身酷 2020-12-13 03:01

I would like to have images, css, and javascript cached client-side on their browser when they load up a web page. There are so many different types of caching I am confused

6条回答
  •  青春惊慌失措
    2020-12-13 03:30

    @Paul Creasey and @Salsa are both correct that browsers will handle caching by default so long as the link is the same.

    As you mentioned, this raises a problem when you need to update those files as you have no guarantee that the client's browser will check for an updated version. In many cases they only do this after a fixed amount of time has passed which can create a crummy user experience.

    There are a number of questions already asked on this site as to how to handle alerting the client browsers to refresh the cache. In short, they all rely on changing the link when you change the file's contents.

    You can append a parameter to the URL that will only be used for caching purposes such as:

    
    

    Then just change the version number when you change the contents and need to force a client side refresh ala this answer.

提交回复
热议问题