简体   繁体   English

利用浏览器缓存|修改.htaccess文件| - 不适用于javascript文件

[英]Leverage browser caching | modifying .htaccess file | - not working for javascript files

I am trying to modify my .htaccess file by specifying an expiration for resources. 我试图通过指定资源的到期来修改我的.htaccess文件。 It has worked for images but not for javascript files. 它适用于图像,但不适用于javascript文件。 When running GTMetrix it still recommends that the javascript files need expiration. 运行GTMetrix时,它仍然建议javascript文件需要过期。 I have tried "application/javascript" and "application/x-javascript" but to no avail. 我试过“application / javascript”和“application / x-javascript”,但无济于事。

Not sure what I am doing wrong. 不确定我做错了什么。

Here is my code: 这是我的代码:

     ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType application/javascript "access 1 week"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"

    </IfModule>
    ## EXPIRES CACHING ##

Adding this will make it work. 添加它将使其工作。

ExpiresByType text/x-javascript "access plus 1 month"  
ExpiresByType application/javascript "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"

Using the Network tab in the browsers inspector Chrome/FireFox/Opera, you can check the asset and see what kind of "Content Type" is being served. 使用浏览器检查器Chrome / FireFox / Opera中的“网络”选项卡,您可以检查资产并查看正在提供的“内容类型”类型。

In my case it was Content-Type:"text/javascript" 就我而言,它是Content-Type:"text/javascript"

So I added 4 permutations of ExpiresByType to my .htaccess file 所以我在我的.htaccess文件中添加了4个ExpiresByType的排列

ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"

This solved my problem and I hope it will help others. 这解决了我的问题,我希望它能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM