Detect Google Chrome page prefetch

大兔子大兔子 提交于 2019-12-05 16:25:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!