大网数据广告活动
网站运营与故障问答

Leverage Browser Caching for third party resources

· 52CE 我爱测

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 thi…

问题原文

原帖链接 · 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:

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。