PageSpeed建议:利用浏览器缓存......如何?

时间:2014-10-09 13:53:31

标签: wordpress caching pagespeed

我有一个自己创建的自定义主题的wordpress网站。目前,我正在制作“Google PageSpeed Insights”的建议,以改善我网站上的用户体验。

PageSpeed建议“利用浏览器缓存”。以下是结果:

**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.
Leverage browser caching for the following cacheable resources:
  http:.//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js (60 minutes)
  http:.//pagead2.googlesyndication.com/pagead/osd.js (60 minutes)
  http:.//www.google.com/…callback%22%3A%22__gcse.sacb%22%7D%5D%7D (60 minutes)

所以,基本上,它建议我将Adsense脚本缓存的“到期时间”设置为60分钟。

我尝试通过REDbot检查我网站的缓存状态。以下是结果:

  

常规

     

服务器的时钟是正确的。

     

内容谈判

     

资源不会持续发送Vary。

     

支持gzip压缩的内容协商,节省72%。

     

缓存

     

此响应允许所有缓存存储它。

     

此响应允许缓存分配自己的新鲜度生命周期。

我对结果的理解是我的网站允许缓存,并允许缓存分配自己的刷新时间。我没有使用任何HTML“元”标签进行缓存,所以基本上,我的服务器是这样配置的。

现在我的问题是:我如何手动将脚本的“expitry-time”设置为60分钟。 我知道如何使用“.htaccess”文件来设置各种文件类型的缓存持续时间((使用FilesMatch))。但是,据推测,此方法适用于文件类型,而不适用于“.php”文件中的单个脚本。 那么,如何更改adsense脚本的缓存到期时间呢?

((这是我的第一个stackoverflow问题,所以如果有任何错误或不足,请指出我...谢谢))

  

更新

在“Andrew”的回答之后,我将以下内容添加到我的.htaccess文件中。

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 hour"
ExpiresByType image/jpeg "access plus 1 hour"
ExpiresByType image/gif "access plus 1 hour"
ExpiresByType image/png "access plus 1 hour"
ExpiresByType text/css "access plus 1 hour"
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/x-javascript "access plus 1 hour"
ExpiresByType image/x-icon "access plus 1 hour"
ExpiresDefault "access plus 1 hour"
</IfModule>
## EXPIRES CACHING ##

但它给了我奇怪的结果。 Pagespeed现在降低了我的分数,现在有以下建议:

  

利用浏览器缓存设置到期日期或最长年龄   静态资源的HTTP标头指示浏览器加载   以前从本地磁盘而不是从本地磁盘下载资源   网络。利用浏览器缓存来实现以下可缓存   资源:

http.://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js (60 minutes)
http.://pagead2.googlesyndication.com/pagead/osd.js (60 minutes)
http.://www.google.com/…callback%22%3A%22__gcse.sacb%22%7D%5D%7D (60 minutes)
http.://www.mananatomy.com/…heme/images/mananatomy_logo_modified.jpg (60 minutes)
http.://www.mananatomy.com/…mages/mananatomy_logo_modified_small.jpg (60 minutes)
http.://www.mananatomy.com/…s/mananatomy-theme/images/sidebar_bg.jpg (60 minutes)
http.://www.mananatomy.com/…ontent/themes/mananatomy-theme/style.css (60 minutes)
http.://www.mananatomy.com/…t/uploads/2010/12/basic-anatomy-logo.jpg (60 minutes)
http.://www.mananatomy.com/…nt/uploads/2010/12/body-systems-logo.jpg (60 minutes)
http.://www.mananatomy.com/…ntent/uploads/2010/12/histology-logo.jpg (60 minutes)

所以它变得更加令人困惑。任何帮助???

5 个答案:

答案 0 :(得分:2)

在这里阅读:

    Leverage browser caching for the following cacheable resources:
  http:.//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js (60 minutes)
  http:.//pagead2.googlesyndication.com/pagead/osd.js (60 minutes)
  http:.//www.google.com/…callback%22%3A%22__gcse.sacb%22%7D%5D%7D (60 minutes)

不是你的资源没有被缓存!

答案 1 :(得分:1)

前面的括号(60分钟)不是google根据该数量设置缓存的建议。它实际上谷歌告诉你,缓存设置为非常低(60分钟),即1小时

对于图像和其他静态文件,请将缓存设置为至少1个月!

关于google的JS文件,你无法做任何事情..你无法缓存EXTERNAL链接(esp javascript)而不会弄乱你的网站

答案 2 :(得分:1)

我已经使用了一些代码来利用浏览器缓存问题来提高网站速度性能并获得了很好的结果。

第1步:在Google Analytics上进行预取。只需将波纹管代码放入网站正文代码即可。

步骤2:然后在.htaccess文件中添加一些代码,以便记住1年或1个月的浏览器缓存历史记录。

到期高速缓存

ExpiresActive On

ExpiresByType image / jpg“访问1年”

ExpiresByType image / jpeg“访问1年”

ExpiresByType image / gif“access 1 year”

ExpiresByType image / png“访问1年”

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应用程序/ x-shockwave-flash“访问1个月”

ExpiresByType image / x-icon“访问1年”

ExpiresDefault“访问1个月”

到期高速缓存

步骤3:放完上面的代码后,在你的.htaccess文件中添加了一些额外的代码。

标题设置Cache-Control“max-age = 2592000,public”

此代码将帮助您的网站中的控制缓存和Up网站的高性能

答案 3 :(得分:0)

这是最终的标头代码,涵盖了您需要涵盖的所有内容 将此代码添加到您的.htacess文件中,该文件将在网站根目录中找到。

<IfModule mod_mime.c> AddType text/css .css AddType text/x-component .htc AddType application/x-javascript .js AddType application/javascript .js2 AddType text/javascript .js3 AddType text/x-js .js4 AddType video/asf .asf .asx .wax .wmv .wmx AddType video/avi .avi AddType image/bmp .bmp AddType application/java .class AddType video/divx .divx AddType application/msword .doc .docx AddType application/vnd.ms-fontobject .eot AddType application/x-msdownload .exe AddType image/gif .gif AddType application/x-gzip .gz .gzip AddType image/x-icon .ico AddType image/jpeg .jpg .jpeg .jpe AddType image/webp .webp AddType application/json .json AddType application/vnd.ms-access .mdb AddType audio/midi .mid .midi AddType video/quicktime .mov .qt AddType audio/mpeg .mp3 .m4a AddType video/mp4 .mp4 .m4v AddType video/mpeg .mpeg .mpg .mpe AddType video/webm .webm AddType application/vnd.ms-project .mpp AddType application/x-font-otf .otf AddType application/vnd.ms-opentype ._otf AddType application/vnd.oasis.opendocument.database .odb AddType application/vnd.oasis.opendocument.chart .odc AddType application/vnd.oasis.opendocument.formula .odf AddType application/vnd.oasis.opendocument.graphics .odg AddType application/vnd.oasis.opendocument.presentation .odp AddType application/vnd.oasis.opendocument.spreadsheet .ods AddType application/vnd.oasis.opendocument.text .odt AddType audio/ogg .ogg AddType application/pdf .pdf AddType image/png .png AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx AddType audio/x-realaudio .ra .ram AddType image/svg+xml .svg .svgz AddType application/x-shockwave-flash .swf AddType application/x-tar .tar AddType image/tiff .tif .tiff AddType application/x-font-ttf .ttf .ttc AddType application/vnd.ms-opentype ._ttf AddType audio/wav .wav AddType audio/wma .wma AddType application/vnd.ms-write .wri AddType application/font-woff .woff AddType application/font-woff2 .woff2 AddType application/vnd.ms-excel .xla .xls .xlsx .xlt .xlw AddType application/zip .zip </IfModule> <IfModule mod_expires.c> ExpiresActive On ExpiresByType text/css A31536000 ExpiresByType text/x-component A31536000 ExpiresByType application/x-javascript A31536000 ExpiresByType application/javascript A31536000 ExpiresByType text/javascript A31536000 ExpiresByType text/x-js A31536000 ExpiresByType video/asf A31536000 ExpiresByType video/avi A31536000 ExpiresByType image/bmp A31536000 ExpiresByType application/java A31536000 ExpiresByType video/divx A31536000 ExpiresByType application/msword A31536000 ExpiresByType application/vnd.ms-fontobject A31536000 ExpiresByType application/x-msdownload A31536000 ExpiresByType image/gif A31536000 ExpiresByType application/x-gzip A31536000 ExpiresByType image/x-icon A31536000 ExpiresByType image/jpeg A31536000 ExpiresByType image/jpg A31536000 ExpiresByType image/webp A31536000 ExpiresByType application/json A31536000 ExpiresByType application/vnd.ms-access A31536000 ExpiresByType audio/midi A31536000 ExpiresByType video/quicktime A31536000 ExpiresByType audio/mpeg A31536000 ExpiresByType video/mp4 A31536000 ExpiresByType video/mpeg A31536000 ExpiresByType video/webm A31536000 ExpiresByType application/vnd.ms-project A31536000 ExpiresByType application/x-font-otf A31536000 ExpiresByType application/vnd.ms-opentype A31536000 ExpiresByType application/vnd.oasis.opendocument.database A31536000 ExpiresByType application/vnd.oasis.opendocument.chart A31536000 ExpiresByType application/vnd.oasis.opendocument.formula A31536000 ExpiresByType application/vnd.oasis.opendocument.graphics A31536000 ExpiresByType application/vnd.oasis.opendocument.presentation A31536000 ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000 ExpiresByType application/vnd.oasis.opendocument.text A31536000 ExpiresByType audio/ogg A31536000 ExpiresByType application/pdf A31536000 ExpiresByType image/png A31536000 ExpiresByType application/vnd.ms-powerpoint A31536000 ExpiresByType audio/x-realaudio A31536000 ExpiresByType image/svg+xml A31536000 ExpiresByType application/x-shockwave-flash A31536000 ExpiresByType application/x-tar A31536000 ExpiresByType image/tiff A31536000 ExpiresByType application/x-font-ttf A31536000 ExpiresByType application/vnd.ms-opentype A31536000 ExpiresByType audio/wav A31536000 ExpiresByType audio/wma A31536000 ExpiresByType application/vnd.ms-write A31536000 ExpiresByType application/font-woff A31536000 ExpiresByType application/font-woff2 A31536000 ExpiresByType application/vnd.ms-excel A31536000 ExpiresByType application/zip A31536000 </IfModule> <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon image/webp application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel <IfModule mod_mime.c> # DEFLATE by extension AddOutputFilter DEFLATE js css htm html xml </IfModule> </IfModule> <FilesMatch "\.(css|htc|less|js|js2|js3|js4|CSS|HTC|LESS|JS|JS2|JS3|JS4)$"> FileETag MTime Size <IfModule mod_headers.c> Header unset Set-Cookie </IfModule> </FilesMatch> <FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|TXT|XSD|XSL|XML)$"> FileETag MTime Size <IfModule mod_headers.c> Header append Vary User-Agent env=!dont-vary </IfModule> </FilesMatch> <FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|WEBP|JSON|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|WEBM|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTF|TTC|_TTF|WAV|WMA|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW|ZIP)$"> FileETag MTime Size <IfModule mod_headers.c> Header unset Set-Cookie </IfModule> </FilesMatch> <FilesMatch "\.(bmp|class|doc|docx|eot|exe|ico|webp|json|mdb|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|pot|pps|ppt|pptx|svg|svgz|swf|tif|tiff|ttf|ttc|_ttf|wav|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|BMP|CLASS|DOC|DOCX|EOT|EXE|ICO|WEBP|JSON|MDB|WEBM|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|POT|PPS|PPT|PPTX|SVG|SVGZ|SWF|TIF|TIFF|TTF|TTC|_TTF|WAV|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW)$"> <IfModule mod_headers.c> Header unset Last-Modified </IfModule> </FilesMatch> <IfModule mod_headers.c> Header set Referrer-Policy "no-referrer-when-downgrade" </IfModule>

答案 4 :(得分:-2)

按照以下步骤,您将找到该问题的解决方案:

  1. 创建.htpasswd文件,转到htaccesstools.com/htpasswd-generator/并生成用户名和密码
  2. 将.htpasswd文件上传到您的WordPress wp-admin文件夹

  3. 将以下代码添加到.htaccess文件

    $("option:selected").val();