栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

利用第三方JS的浏览器缓存

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用第三方JS的浏览器缓存

确实是个烦人的问题。恐怕没有一个那么 容易 修复的。但是您可以使用

cron

首先,请记住,Google不太可能因自己的工具(如Google
Analytics(分析))而对您进行惩罚。但是,如前所述,可以使用进行修复

cron
,这基本上意味着您可以在本地加载Javascript并提取更新的脚本。

这个怎么做:

首先,您需要下载正在运行的脚本。我将以Google Analytics(分析)为例(这似乎是人们抱怨最多的问题脚本,但是您可以将其复制到任何外部脚本中)。

在您的代码中查找并找到脚本的名称,在本例中为:

google-analytics.com/ga.js
。将此URL弹出到Web浏览器中,它将显示源代码。只需将其复制并另存为
ga.js

在我的情况下,将此新创建的Javascript文件保存到您的网络服务器上:

- JS  - ga.js

接下来,您将需要更新正在调用脚本的页面上的代码,只需更改正在调用Javascript文件的目录即可。在本例中,我们将再次更改此行:

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.yoursite.com/js/ga.js';

至此,您的站点现在将从 本地 站点运行脚本!但是,这意味着脚本将永远不会更新。除非您每周重新运行此简短过程。那取决于你..但我对此实在太懒了。

这是CRON发挥作用的地方:

几乎每一个托管服务都会有一个选项供您设置

cron
作业。在Hostinger上,它位于您的主机面板上,在GoDaddy上,您可以在“内容”选项下找到它。

将以下脚本放入您的脚本

cron
,您所需要做的就是更改变量的绝对路径
$localfile
。该脚本的作用是从Google提取文件的更新脚本
ga.js
。您可以设置运行该过程的频率范围。从每小时一次到每月一次甚至更长时间不等。

如果您还针对Google
Analytics(分析)以外的外部文件执行此操作,则还需要更改变量

$remoteFile
。所以,
$remoteFile
是的URL外部Javascript文件和变量
$localFile
,你就会把路到新的本地存储的文件,就这么简单!

<?// script to update local version of Google analytics script// Remote file to download$remoteFile = 'http://www.google-analytics.com/ga.js';$localfile = 'ENTER YOUR ABSOLUTE PATH TO THE FILE HERE';//For Cpanel it will be /home/USERNAME/public_html/ga.js// Connection time out$connTimeout = 10;$url = parse_url($remoteFile);$host = $url['host'];$path = isset($url['path']) ? $url['path'] : '/';if (isset($url['query'])) {  $path .= '?' . $url['query'];}$port = isset($url['port']) ? $url['port'] : '80';$fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout );if(!$fp){  // On connection failure return the cached file (if it exist)  if(file_exists($localfile)){    readfile($localfile);  }} else {  // Send the header information  $header = "GET $path HTTP/1.0rn";  $header .= "Host: $hostrn";  $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6rn";  $header .= "Accept: */*rn";  $header .= "Accept-Language: en-us,en;q=0.5rn";  $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7rn";  $header .= "Keep-Alive: 300rn";  $header .= "Connection: keep-alivern";  $header .= "Referer: http://$hostrnrn";  fputs($fp, $header);  $response = '';  // Get the response from the remote server  while($line = fread($fp, 4096)){    $response .= $line;  }  // Close the connection  fclose( $fp );  // Remove the headers  $pos = strpos($response, "rnrn");  $response = substr($response, $pos + 4);  // Return the processed response  echo $response;  // Save the response to the local file  if(!file_exists($localfile)){    // Try to create the file, if doesn't exist    fopen($localfile, 'w');  }  if(is_writable($localfile)) {    if($fp = fopen($localfile, 'w')){      fwrite($fp, $response);      fclose($fp);    }  }}?>

就是这样,它应该可以解决您利用Leverage Browser Caching第三方脚本遇到的所有问题。

资料来源:http : //diywpblog.com/leverage-browser-cache-optimize-google-
analytics/

注意:

实际上,这些文件不会对您的实际页面速度产生很大影响。但是我能理解您对Google惩罚您的担心。但是,如果你有一个,只会出现
运行这些外部脚本量。正如我之前所说,与Google相关的任何事情都不会对您不利。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号