Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It has worked for images but not for javascript files. When running GTMetrix it still recommends that the javascript files need expiration. I have tried "application/javascript" and "application/x-javascript" but to no avail.

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 ##
like image 940
kad Avatar asked May 09 '12 06:05

kad


People also ask

How do I fix leverage browser caching in WordPress?

Fixing Leverage Browser Caching in WordPress with the W3 Total Cache can be done by following the steps listed below: Download and install the W3 Total Cache WordPress plugin. Navigate to General Settings and select Enable Browser Cache. Click save and navigate to Browser Cache Settings (top of the page)

Can JavaScript be cached?

In general, most modern browsers will cache JavaScript files. This is standard practice for modern browsers and ensures an optimized loading experience. Cached assets such as JavaScript will typically be served from the browser's cache instead of making another request for a resource that has already been retrieved.

How do I leverage browser caching?

To leverage your browser's caching generally means that you can specify how long web browsers should keep images, CSS and JS stored locally. That way the user's browser will download less data while navigating through your pages, which will improve the loading speed of your website.

Are htaccess files cached?

Their contents are not cached, they are read on every request. it's a file called . htaccess, in the directory /var/www.


1 Answers

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"
like image 191
Amjad Avatar answered Oct 18 '22 09:10

Amjad