Force browser to refresh css, javascript, etc

前端 未结 16 1792
花落未央
花落未央 2020-11-29 19:16

I\'m developing a website based on Wordpress source code through XAMPP. Sometimes I change the CSS code, scrips or something else and I notice my browser takes time to apply

相关标签:
16条回答
  • 2020-11-29 19:59

    Developer point of view
    If you are in development mode (like in the original question), the best approach is to disable caching in the browser via HTML meta tags. To make this approach universal you must insert at least three meta tags as shown below.

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    

    In this way, you as a developer, only need to refresh the page to see the changes. But do not forget to comment that code when in production, after all caching is a good thing for your clients.

    Production Mode
    Because in production you will allow caching and your clients do not need to know how to force a full reload or any other trick, you must warranty the browser will load the new file. And yes, in this case, the best approach I know is to change the name of the file.

    0 讨论(0)
  • 2020-11-29 20:00

    If you can write php, you can write:

    <script src="foo.js<?php echo '?'.mt_rand(); ?>" ></script>
    <link rel="stylesheet" type="text/css" href="foo.css<?php echo '?'.mt_rand(); ?>" />
    <img src="foo.png<?php echo '?'.mt_rand(); ?>" />
    

    It will always refresh!

    EDIT: Of course, it's not really practical for a whole website, since you would not add this manually for everything.

    0 讨论(0)
  • 2020-11-29 20:00

    The accepted answer above is correct. If, however, you only want to reload the cache periodically, and you are using Firefox, the Web Developer tools (under the Tools menu item as of November 2015) provides a Network option. This includes a Reload button. Select the Reload for a once off cache reset.

    0 讨论(0)
  • 2020-11-29 20:04

    Check this: How Do I Force the Browser to Use the Newest Version of my Stylesheet?

    Assumming your css file is foo.css, you can force the client to use the latest version by appending a query string as shown below.

    <link rel="stylesheet" href="foo.css?v=1.1">
    
    0 讨论(0)
提交回复
热议问题