css and js with parameter v or version

background

Search for the keyword .htaccess cache      in the search engine , you can find a lot of tutorials about setting up website file caching. By setting, you can cache css, js and other less frequently updated files on the browser side, so that every time visitors visit you When the website is opened, the browser can obtain css, js, etc. from the browser's cache without having to read it from your server, which speeds up the opening of the website to a certain extent and saves your server traffic. .

 

question

     Now the problem is coming. The CSS and JS caches set by .htaccess have an expiration time. If the css and js have been cached in the visitor's browser, the browser will only remove the cache from the cache before the css and js caches expire. Read the css and js, if you modify the css and js on the server, then these changes will not change in the browser of the returning customer, unless the returning customer presses Ctrl + F5 to refresh your website page or manually clears it Browser cache. There are thousands of visitors to a website, and there will be a lot of repeat visitors. It is impossible for each visitor to refresh the cache after updating the CSS. So how do you deal with this problem?

 

method one

     Change the css file name: In fact, the solution to this problem is very simple, the cache is to mark the cached content by the file name. After you update the content of the css file of the website, you can change the css file name. For example, the css call statement in the original html is as follows:

<link rel="stylesheet" href="style.css" />

     Just change the css file name:

<link rel="stylesheet" href="index.css" />

     Another way to change the css filename is to write the version number to the filename like:

<link rel="stylesheet" href="index.v2011.css" />

     After the css file is updated, change the version number in the file name:

<link rel="stylesheet" href="index.v2012.css" />

Method Two

     Add a version number to the css file: In fact, it is a bit troublesome to modify the css file name after each modification of the css file, so we can add a version number to the loading css statement (ie, the content in the css link? The following content) is fine. . For example, the css call statement in the original html is as follows:

1
<link rel="stylesheet" href="style.css?v=2011" />

     Just change the version number of the css file to 2012:

1
<link rel="stylesheet" href="style.css?v=2012" />

     It should be noted that some proxy cache servers will not cache resources containing "?" in the URL, so the second method may cause your original caching function to fail, you can use the first method instead.

Summarize

     In fact, the question mark after the CSS file has no practical effect, and can only be used as a suffix. If you use the method of adding a question mark and parameters, you can add information such as the version number, and at the same time, you can refresh the cache on the browser side. A small detail can bring us great convenience.

background

     在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、js等,而不必从你的服务器读取,这样在一定程度上加快了网站的打开速度,又可以节约一下你的服务器流量。

 

问题

     现在问题来了,.htaccess设置的css、js缓存都有一个过期时间,如果在访客的浏览器中已经缓存了css、js,在这些css、js缓存未过期之前,浏览器只会从缓存中读取css和js,如果你在服务器上修改了css和js,那么这些更改在回头客的浏览器中是不会有变化的,除非回头客按了Ctrl + F5刷新了你的网站页面或者手动清空了浏览器的缓存。一个网站的访客成千上万,回头客也会有不少,你不可能在更新css后让每个访客都刷新一下缓存吧,那么这个问题你会怎么处理呢?

 

方法一

     更改css文件名:其实解决这个问题很简单,缓存是通过文件名标记缓存的内容的。在你更新了网站的css文件内容后,在更换一下css的文件名就可以了。如原先html中的css调用语句如下:

1
<link rel="stylesheet" href="style.css" />

     改一下css文件名就可以了:

1
<link rel="stylesheet" href="index.css" />

     另外一种更改css文件名的方法是将版本号写到文件名中,如:

1
<link rel="stylesheet" href="index.v2011.css" />

     css文件更新后,改一下文件名中的版本号即可:

1
<link rel="stylesheet" href="index.v2012.css" />

方法二

     给css文件加个版本号:其实每次修改css文件后还要修改css的文件名有点麻烦,那么我们可以在加载css语句中加入个版本号(即css链接中?后面的内容)就可以了。如原先html中的css调用语句如下:

1
<link rel="stylesheet" href="style.css?v=2011" />

     改一下css文件的版本号改成2012就可以了:

1
<link rel="stylesheet" href="style.css?v=2012" />

     需要注意的是,部分代理缓存服务器不会缓存网址中包含 "?" 的资源,所以方法二可能会导致你原先的缓存功能失效,可以改用第一种方法。

总结

     其实css文件后面的问号起不到实际作用,仅能当作后缀,如果用问号加参数的方法,可以添加版本号等信息,同时可以刷新一下浏览器端的缓存。一个小小的细节,可以给我们带来很大的方便。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769361&siteId=291194637