问题原文
原帖链接 · Kristina J. · CC BY-SA 4.0
I am optimising a website based on PageSpeed Insights guidelines. And one of the points is to Leverage Browser Caching.
And in this case - for the third party resources - like Google Analytics and Tag Manager.
I already added this code to .htaccess file:
# Expires headers
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 2 days"
# cache.appcache needs re-requests in FF 3.6
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Your document html
ExpiresByType text/html "access plus 0 seconds"
# Data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# Feed
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
# Favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# Media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# HTC files
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType application/x-font-woff2 "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
What should I add to this code? And what is the recommended period for browser caching for Google Analytics and Google Tag Manager?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
As @TimFountain says, you have "no control" over resources served from a third party. The request goes to the third party server, not your server - so any code you place on your server is simply never processed when the third party resource is requested.
Google Analytics and Tag Manager are served from Google's servers. Only Google controls Google's servers!
However, if Google has decided not to cache these resources then there may be a very good reason why it has chosen not to do so. Not everything should be cached.
Attempting to implement caching on these "Google" resources (perhaps by serving these resources from your own server instead - not recommended) - whilst satisfying the PageSpeed test - could have a negative effect on your user experience and Analytics data. The PageSpeed tool is a guide only. You shouldn't blindly accept the recommendations.
This question over on StackOverflow specifically discusses Google Analytics and PageSpeed Insights:
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
