Automatically clean up the cache of js and css files in HTML pages (automatically add version numbers)

Automatically clean up the cache of js and css files in HTML pages (automatically add version numbers)

In the process of web project development, we often reference css and js files. After updating the files, cache problems often occur (the code has been changed, but it has not changed when accessed on the browser). In this case, we usually use the following two solutions:

1. Clear the browser cache manually

2. Add the version number (such as layout.css?v=1)

Personally, I think method 2 is faster because clearing the browser cache requires waiting for the browser to respond. But it is troublesome to change the version number every time, so we need to find a way to automatically add the version number.

Here is how I collected it:

Method 1: You can automatically add a version number to HTML through js

 <script type="text/javascript">  
document.write("<link rel='stylesheet' type='text/css' href='/css/layout.css?v="+new Date().getTime()+"'>");   
</script>

Method 2: If it is a jsp page, you can use java code to generate a timestamp (if it is a jsp page, you can also use method 1, but this method is more convenient)

<link rel="stylesheet" type="text/css" href="/css/layout.css?v=<%=System.currentTimeMillis() %>">

Method 3: Use other methods to add version numbers, such as automatic configuration with node.js

ps: The purpose of clearing the cache is to see the update status of the page in time. When we put the page online (that is, deploy it to the formal environment and will not make any changes), it is recommended to fix the version number, because the cached page is faster to access. When it needs to be updated, replace the fixed version number.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Detailed basic operations on data tables in MySQL database

>>:  Example of implementing hollow triangle arrow and X icon with after pseudo element

Recommend

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Summary of the pitfalls of using primary keys and rowids in MySQL

Preface We may have heard of the concept of rowid...

HTML background color gradient achieved through CSS

Effect screenshots: Implementation code: Copy code...

Introduction to general_log log knowledge points in MySQL

The following operation demonstrations are all ba...

Solution for converting to inline styles in CSS (css-inline)

Talk about the scene Send Email Embedding HTML in...

Solve the problem of garbled Chinese characters in Mysql5.7

When using MySQL 5.7, you will find that garbled ...

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of...

Detailed explanation of when javascript scripts will be executed

JavaScript scripts can be embedded anywhere in HT...

Design Story: The Security Guard Who Can't Remember License Plates

<br />In order to manage the vehicles enteri...

How to install MySQL 8.0.17 and configure remote access

1. Preparation before installation Check the data...

How to use an image button as a reset form button

When we make a form, we often set a submit button ...

HTML Tutorial: Collection of commonly used HTML tags (6)

Related articles: Beginners learn some HTML tags ...

Web page printing thin line table + page printing ultimate strategy

When I was printing for a client recently, he aske...

Detailed explanation of Docker container data volumes

What is Let’s first look at the concept of Docker...