2

When I run Google PageSpeed Insights I get the following optimization suggestion.

Leverage browser caching

When I click the Leverage browser caching link it sends me to a page that recommends I use Cache-Control or ETags.

Setting up Cache-Control with .htaccess should be simple enough so I included the following lines inside of my existing .htaccess file.

<IfModule mod_headers.c>

<filesMatch "https://www.google-analytics.com/analytics.js">
Header set Cache-Control "max-age=7200, public"
</filesMatch> 

<filesMatch "https://www.googletagmanager.com/gtag/js?id=UA-00000000-0">
Header set Cache-Control "max-age=900, public"
</filesMatch> 

</IfModule>

I uploaded the .htaccess file, tested it and got the same error. Even though Google recommends using Cache-Control that doesn't solve the problem.

When that didn't work I tried ExpiresByType but it didn't solve them problem either.

<IfModule mod_expires.c>

ExpiresActive On

<IfModule mod_expires.c>
ExpiresByType application/javascript "access plus 2 hours"
</IfModule>

</IfModule>

Is there a way to use an .htaccess file to instruct browsers to cache these files or is that not possible?

DR01D
  • 350
  • 2
  • 11

1 Answers1

4

Nothing in .htaccess can affect a domain outside of your control.

You have a couple options.

  • Self-host the gtag.js file, which is possible but not recommended.
  • Proxy the gtag.js file, adding your own headers to it. This addresses the main concern (updates) from the above doc.
  • Ignore it. It's essentially a false-positive; Google has presumably intentionally set a low cache time for these assets for a reason.
ceejayoz
  • 32,469
  • 7
  • 81
  • 105