检测Google Chrome页面预取 [英] Detect Google Chrome page prefetch

查看:123
本文介绍了检测Google Chrome页面预取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的工具来跟踪并增加网站的访问次数。这很简单:


当服务器收到GET请求时,它会将该网站数据库中的计数器增加1。然而,Google Chrome的预渲染引擎(预测网络操作以提高页面加载性能)存在一些问题。



该网站为www.domain.com,只要您在浏览器的地址栏中输入域名www.domain.com(无需按Enter键),Chrome发送一个GET请求来预取页面,导致服务器记录该访问并将数据库中的计数器增加1.此后,如果用户按Enter键并实际加载网页,则服务器将看到另一个GET请求进入,因此再次增加柜台。这会导致在数据库中记录2次重复访问。据我所知,谷歌浏览器只下载页面,但不执行它,但只要我的服务器获取GET请求,计数器就会增加。



问题:有没有解决此问题的方法?最好是,我想检测它是否是预取或访问该网站的实际用户。

解决方案

这是不可检测的服务器端。



Chromium项目为此打开了一个问题。它被标记为不会修复预渲染没有任何明显的HTTP头)。预取请求不会显示在开发工具网络选项卡中,因此您无法通过查看标题来轻松确认。我使用Wireshark进行了检查,很遗憾,预渲染请求与正常请求之间没有区别。



解决方法:



您可以使用页面可见性API 。这比服务器端跟踪要多一点工作,但是您可以在页面中插入一个脚本标记来检查它是否是预渲染请求。如果是这种情况,请发送一个AJAX请求,以某种方式标识它是哪个页面,并用它来增加数据库中的计数器。

  if(document.visibilityState!=='prerender')
{
// ajax call registering page hit
}

请注意,AJAX请求不会从缓存中返回结果,从而阻止它到达服务器。


I am building a simple tool that tracks and increases the number of visits of a website. It's something simple as:

When the server receives a GET request, it will increase the counter in database for that website by 1.

However, I am running to a bit problem with Google Chrome's pre-render engine ("Predict network actions to improve page load performance").

The website is www.domain.com, and as soon as you type the domain name www.domain.com into the browser's address bar (without pressing Enter), Chrome sends a GET request to prefetch the page, resulting in the server logging that visit and increasing the counter in database by 1. After that, if the user presses Enter and actually loads the webpage, the server will see another GET request coming in, thus increasing the counter, again. This result in 2 duplicate visits logged in the database. As far as I understand, Google Chrome only downloads the page but doesn't execute it, but as soon as my server gets a GET requests, the counter is increased.

Question: Is there any way to work around this? Preferably, I would like to detect whether or not it is a prefetch or an actual user that visits the website.

解决方案

It appears that this is undetectable server side.

An issue was opened for this for the Chromium project. It's marked as "won't fix" (Prerendering does not have any distinguishing HTTP headers). The prefetch request doesn't show up in the Dev Tools "Network" tab, so you can't easily confirm this by look at the headers. I checked them using Wireshark, and unfortunately there was no difference distinguishing pre-render requests from "normal" requests.

Workaround:

You can check for prefetched pages client-side using the Page Visibility API. It's a bit more work than server side tracking, but you can insert a script tag in your page that checks if it's a prerender request. If that's the case, send an AJAX request which somehow identifies which page it was, and use that to increment the counter in your database.

if (document.visibilityState !== 'prerender')
{
    //ajax call registering page hit
}

Just watch out that the AJAX request doesn't return a result from cache, preventing it from arriving at the server.

这篇关于检测Google Chrome页面预取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆