簡體   English   中英

如何在htaccess文件中為第三方API和小部件利用緩存控制?

[英]How can I leverage cache control for 3rd party APIs and widgets in my htaccess file?

我已經將緩存文件用於常見的文件類型。 但是在如何為第三方API(例如Google API)和小部件(例如Disqus,AddThis,Pinterest,Twitter)添加緩存過期方面沒有太多幫助,這使我的網站速度降低了80%(之前是97%)

這些是一些常見的小部件,它們正在減慢我的網站的運行速度:

  1. cdn.viglink.com/images/pixel.gif?ch=1&rn=5.711541143245995(15秒)
  2. cdn.viglink.com/images/pixel.gif?ch=2&rn=5.711541143245995(15秒)
  3. disqus.com/next/config.js(60秒)
  4. asset.pinterest.com/js/pinit_main.js?0.7067068491596729(4分鍾)
  5. s7.addthis.com/js/300/addthis_widget.js(10分鍾)
  6. m.addthisedge.com/live/boost/ra-9a7a0e60b8044af/_ate.track.config_resp(60秒)
  7. platform.twitter.com/widgets.js(30分鍾)
  8. apis.google.com/js/api.js(30分鍾)
  9. apis.google.com/js/rpc:shindig_random.js?onload=init(30分鍾)

這是我的.htaccess文件

# 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 text/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>

對於Google Analytics(js)設置緩存過期,這是一個很好的資源: 利用第三方JS的瀏覽器緩存

對於其他人,請下載所有js並將其本地放置在服務器上。

但是,如果您有cloudflare(安裝了https),我發現了一種更簡單的方法

登錄到您的cloudflare帳戶,然后轉到“緩存”選項卡, 並將“瀏覽器緩存過期”級別設置為至少8天,您差不多就完成了清除所有操作並等待5分鍾,然后嘗試在其中測試網址

https://developers.google.com/speed/pagespeed/insights/

如果您的速度仍然沒有提高,請嘗試使用一些較長的URL進行測試以查看速度,移動速度肯定會提高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM