Version numbers in css and js links in HTML (refresh cache)

Version numbers in css and js links in HTML (refresh cache)

background
Search the keyword .htaccess cache in the search engine, and you can find many tutorials on setting up website file cache. By setting up, you can cache CSS, JS and other files that are not updated frequently on the browser side, so that every time a visitor visits your website, the browser can get CSS, JS, etc. from the browser cache instead of reading from your server. This speeds up the opening of the website to a certain extent and saves your server traffic.

question
Now the problem comes. The css and js caches set by .htaccess have an expiration time. If css and js have been cached in the visitor's browser, the browser will only read css and js from the cache before these css and js caches expire. If you modify css and js on the server, then these changes will not change in the browser of returning customers, unless the returning customer presses Ctrl + F5 to refresh your website page or manually clears the browser cache. A website has tens of thousands of visitors, many of whom are repeat visitors. You can’t ask every visitor to refresh the cache after updating the CSS. So how would you deal with this problem?

Method 1

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


Copy code
The code is as follows:
<link rel="stylesheet" href="style.css" />

Just change the css file name:


Copy code
The code is as follows:
<link rel="stylesheet" href="index.css" />

Another way to change the css file name is to write the version number into the file name, such as:


Copy code
The code is as follows:
<link rel="stylesheet" href="index.v2011.css" />

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


Copy code
The code is as follows:
<link rel="stylesheet" href="index.v2012.css" />

Method 2
Add a version number to the CSS file: In fact, it is a bit troublesome to modify the CSS file name every time the CSS file is modified. Then we can add a version number in the loading CSS statement (that is, the content after ? in the CSS link). For example, the css call statement in the original html is as follows:


Copy code
The code is as follows:
<link rel="stylesheet" href="style.css?v=2011" />

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


Copy code
The code is as follows:
<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 method 2 may cause your original cache function to fail. You can use method 1 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 question mark plus parameter method, you can add version number and other information, and refresh the browser cache at the same time. A small detail can bring us great convenience.

<<:  Solution to VMware virtual machine no network

>>:  In-depth analysis of the various backgrounds, usage scenarios and techniques of CSS

Recommend

JSONP cross-domain simulation Baidu search

Table of contents 1. What is JSONP 2. JSONP cross...

A brief discussion on JS packaging objects

Table of contents Overview definition Instance Me...

Proxy realizes the principle of two-way binding of Vue3 data

Table of contents 1. Advantages of proxy vs. Obje...

How to set the number of mysql connections (Too many connections)

During the use of mysql, it was found that the nu...

Implementation of React star rating component

The requirement is to pass in the rating data for...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

mysql 5.7.23 winx64 decompression version installation tutorial

Detailed installation tutorial of mysql-5.7.23-wi...

Use pure JS to achieve the secondary menu effect

This article example shares the specific code of ...

Detailed explanation of MySQL sql_mode query and setting

1. Execute SQL to view select @@session.sql_mode;...

Share 101 MySQL debugging and optimization tips

MySQL is a powerful open source database. With th...

Disabled values ​​that cannot be entered cannot be passed to the action layer

If I want to make the form non-input-capable, I se...

Ubuntu boot auto-start service settings

How to create a service and auto-start it in Ubun...

MySQL's method of dealing with duplicate data (preventing and deleting)

Some MySQL tables may contain duplicate records. ...