admin管理员组

文章数量:1406937

I am trying to update my cache policy by adding some directives in the .htaccess file in the root of my website. Although I updated the values for javascript files to be 1 month, when using Google PageSpeed, it still displayed the previous cache values (6 hours). Why is this?

<IfModule mod_expires.c>
    ExpiresActive On
        ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 14 days"
</IfModule>

I am trying to update my cache policy by adding some directives in the .htaccess file in the root of my website. Although I updated the values for javascript files to be 1 month, when using Google PageSpeed, it still displayed the previous cache values (6 hours). Why is this?

<IfModule mod_expires.c>
    ExpiresActive On
        ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 14 days"
</IfModule>

Share Improve this question asked Oct 26, 2019 at 17:09 OctaviaLoOctaviaLo 1398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 14 days"

In order to determine the correct mime-type to use you need to look at the Content-Type HTTP response header from your server (using your browser). The default for JavaScript files is application/javascript - which you've not included here at all. You only need 1 directive that matches the response from your server, not 3 or 4. (You also stated 14 days for the last directive above?)

For example:

ExpiresActive On
ExpiresByType application/javascript "access plus 1 month"

However, you also appear to be using an external host (CDN?) to serve some of your JS resources - these will obviously not be influenced by the directives on your application server. I also notice that the final response is coming from an Nginx server (possibly a proxy) - this might also be controlling the cache headers.

The <IfModule mod_expires.c> wrapper is not required unless you intend to port your code to multiple servers where mod_expires is not installed and it is OK for these directives to fail.

when using Google PageSpeed, it still displayed the previous cache values (6 hours)

You should be manually checking the HTTP response headers Cache-Control (and Expires) in the browser to determine whether your directives are working or not.

本文标签: htaccessCache policy not updated according to PageSpeed