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

JavaScript file loading and blocking issues: performance optimization case study

Let me start with a question: When writing an HTM...

How to configure nginx+php+mysql in docker

First, understand a method: Entering a Docker con...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

User experience of portal website redesign

<br />From the launch of NetEase's new h...

Detailed explanation of MySQL trigger trigger example

Table of contents What is a trigger Create a trig...

HTML+CSS to achieve drop-down menu

1. Drop-down list example The code is as follows:...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

A brief understanding of the three uses of standard SQL update statements

1. Environment: MySQL-5.0.41-win32 Windows XP Pro...

Notes on the MySQL database backup process

Today I looked at some things related to data bac...

Copy the contents of one file to the end of another file in linux

Problem description: For example, the content of ...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...

A brief discussion on mobile terminal adaptation

Preface The writing of front-end code can never e...

MySQL date and time addition and subtraction sample code

Table of contents 1.MySQL adds or subtracts a tim...