由谷歌Webfont导致的站点访问缓慢

自六月头开始本站出现了页面间歇性访问缓慢的情况,且页分字体发生变动.查了半天也没发现问题出在哪…最后还是浏览器的url状态栏指明了症状所在.看了一下,原来是WP调用了谷歌的远端字体,恰好近期相关服务又被重点河蟹,所以导致了上述问题.

20140611150014

至于解决方案,大致三个思路:

1.直接屏蔽远端字体调用[我用的方法,也是最简单可行的]

直接在functions.php文件中适当位置插入以下代码:

//禁用Open Sans
class Disable_Google_Fonts {
public function __construct() {
add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
}
public function disable_open_sans( $translations, $text, $context, $domain ) {
if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
$translations = 'off';
}
return $translations;
}
}
$disable_google_fonts = new Disable_Google_Fonts;

2.将远端字体替换为本地字体[需要改动的地方较多,泛用性似乎也不高]

先到\wp-includes\css路径下新建一个css文件,例如google-font.css,内容如下:

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 300;
  src: local('Open Sans Light'), local('OpenSans-Light'), url(../fonts/google/DXI1ORHCpsQm3Vp6mXoaTRa1RVmPjeKy21_GQJaLlJI.woff) format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(../fonts/google/u-WUoqrET9fUeobQW7jkRT8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 600;
  src: local('Open Sans Semibold'), local('OpenSans-Semibold'), url(../fonts/google/MTP_ySUJH_bn48VBG8sNSha1RVmPjeKy21_GQJaLlJI.woff) format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 300;
  src: local('Open Sans Light Italic'), local('OpenSansLight-Italic'), url(../fonts/google/PRmiXeptR36kaC0GEAetxrsuoFAk0leveMLeqYtnfAY.woff) format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 400;
  src: local('Open Sans Italic'), local('OpenSans-Italic'), url(../fonts/google/xjAJXh38I15wypJXxuGMBtIh4imgI8P11RFo6YPCPC0.woff) format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 600;
  src: local('Open Sans Semibold Italic'), local('OpenSans-SemiboldItalic'), url(../fonts/google/PRmiXeptR36kaC0GEAetxmWeb5PoA5ztb49yLyUzH1A.woff) format('woff');
}

修改好后保存,然后再来到\wp-includes\找到文件script-loader.php搜索以下代码并替换

$open_sans_font_url = "//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=$subsets";

替换成

$open_sans_font_url = "/wp-includes/css/google-font.css";

此方法尝试后发现还需替换所用主题路径下的functions.php文件中相应代码,保险起见最好将所有地方都替换…麻烦了些.

3.将调用地址改为墙内地址,譬如360搞的墙内专属公共库fonts.useso.com[对三牛您不太感冒,故不使用]

很简单,把有fonts.googleapis.com的地方都改成fonts.useso.com :D

搞定后再看看,网站的访问速度是不是正常了~XD

20151004更新
目前谷歌字库已解封,默认解析至北京服务器,速度很不错.

发表评论

您的电子邮箱地址不会被公开。

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :-| :-x :-o :-P :-D :) :( 8-O 8)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据