Workaround for Web site CSS,JS after updated customer browser cache issues that need to be refreshed for normal presentation

Source: Internet
Author: User
Tags browser cache

Problem Description

Recently, the company's official website style has been adjusted to deploy to the server after the visit found page is not displayed properly, but after the refresh will show normal.

Problem Analysis

The study found that possible causes were

    1. CSS file is too large to load slowly
    2. Local cache problem, although the server modifies the CSS file, but the browser still uses the locally cached CSS,

It is obviously unreasonable to need the user to refresh one more time in order to display properly, so how to resolve the update and ask the browser to request a new CSS or JS file?

Solutions Method 1Change the Css/js file name after updating the files.

In fact, to solve this problem is very simple, the cache is tagged with the file name cache content. After you update the content of the Web site's CSS file, change the file name of the CSS. As in the original HTML, the CSS Call statement is as follows:

<link rel= "stylesheet" href= "Style.css" type= "text/css"  media= "screen"/>

Change the CSS file name:

<link rel= "stylesheet" href= "Stylev2.css" type= "text/css" media= "screen"/>

  

Method 2Add a version number to the Css/js file

It's a bit of a hassle to modify the CSS file name after every modification of the CSS files, so we can add a version number (i.e. the CSS link) to the loaded CSS statement. As in the original HTML, the CSS Call statement is as follows:

<link rel= "stylesheet" href= "style.css?v=2015" type= "text/css"  media= "screen"/>

Change the version number of the CSS file to a new:

<link rel= "stylesheet" href= "style.css?v=2016" type= "text/css"  media= "screen"/>

  

about the css/js file suffix parameter:

CSS file after the question mark does not work, only as a suffix, if you use the question mark method, you can add the version number and other information, update can also refresh the browser side of the cache. A small detail can bring us great convenience.
Like what:

<script type= "Text/javascript" src= "homepage.js?version=1.2.6" ></script><link rel= "stylesheet" href = "base.css?version=2.3.3" type= "Text/css"/>

  

Two effects of using parameters:

    1. The client will cache CSS or JS files, so each time you upgrade the JS or CSS file, change the version number, the client browser will re-download the new JS or CSS files, play a role in refreshing the cache.

    2. The script does not exist, but is dynamically generated by the server, so it has a version number to show the difference. That is, the above code is equivalent to a file:

      <script type= "Text/javascript" src= "Homepage.js" ></script>

        

      <link rel= "stylesheet" href= "Base.css" type= "Text/css"/>

        

      But the browser will think that he is a version of the file!

      The first use is the most, may also use two functions simultaneously.

    3. Source: http://blog.csdn.net/csdn100861/article/details/50684438

Workaround for Web site CSS,JS after updated customer browser cache issues that need to be refreshed for normal presentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.