7

I'm trying to get my load times down using Google PageSpeed Insights as one of my tools. I noticed that there are 2 files that it keeps complaining about:

Leverage browser caching

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

https://apis.google.com/js/api.js (30 minutes)

https://ssl.google-analytics.com/ga.js (2 hours)

I'm assuming that the above files are referenced as part of the default Magento package and looking around the admin panel I can only find where to put my analytics code.

Where do I go or which file do I need to modify to extend the expiry of the above files?

4 Answers 4

6

If you load the js files from Google, Haya is correct, there is nothing you can do to modify the cache for browser caching.

If you are using the asynchronous GA code, then it does not affect page load times, as it loads separate from your Magento page.

If you want to cache the JS, you could load it locally, as described at http://diywpblog.com/leverage-browser-cache-optimize-google-analytics/

You can edit the local version of: template/googleanalytics/ga.phtml

to adjust the URL where GA is loaded from.

6

I managed to score 99/100 with both mobile and desktop versions on my site. Just apis.google.com/js/platform.js browser caching is a barrier in 100. I know it hardly makes a difference yet just for fun. Any clue?

My htaccess-

# Enable Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
</IfModule>
<IfModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

# Leverage Browser 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 text/html "access 1 month"
  ExpiresByType application/pdf "access 1 month"
  ExpiresByType text/x-javascript "access 1 month"
  ExpiresByType application/x-shockwave-flash "access 1 month"
  ExpiresByType image/x-icon "access 1 year"
  ExpiresDefault "access 1 month"
</IfModule>
<IfModule mod_headers.c>
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
  Header set Cache-Control "max-age=2678400, public"
  </filesmatch>
  <filesmatch "\.(html|htm)$">
  Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  <filesmatch "\.(pdf)$">
  Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  <filesmatch "\.(js)$">
  Header set Cache-Control "max-age=2678400, private"
  </filesmatch>
</IfModule>
1
  • Is this an answer? Or are you asking if anyone has a clue to also have proper browser caching on the platform.js file? If so, please use the Aks question button to ask this as a question yourself. You could reference this question to provide context.
    – 7ochem
    Commented Jun 15, 2016 at 18:16
4

For Nginx:

   location ~ /analytics.js {
        proxy_pass https://www.google-analytics.com;
        expires 31536000s;
        proxy_set_header Pragma "public";
        proxy_set_header Cache-Control "max-age=31536000, public";
    }

Then change path https://www.google-analytics.com/analytics.js to https://yoursite.com/analytics.js

2
  • 1
    Does this actually work?
    – Nathan
    Commented Aug 13, 2016 at 22:28
  • I know it's old but this is GOLD. Just did the same with the maps API. Just great, works as expected!
    – Eduard
    Commented Dec 5, 2016 at 0:22
3

To my knowledge you can do nothing about this since these are part of google domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.