Detect Google Chrome Page Prefetch

I am creating a simple tool that tracks and increases the number of visits to a website. This is something simple:

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

However, I ran into a small problem with the Google Chrome pre-rendering mechanism ("Predict network actions to improve page loading performance").

Website www.domain.com, and as soon as you enter the domain name www.domain.com in the address bar of the browser (without pressing Enter), Chrome sends a GET request to pre-select the page, resulting in a server that visits and increases the counter in the database by 1. After that, if the user presses Enter and actually loads the web page, the server will see another GET request, increasing the counter. This results in 2 repeat visits registered in the database. As far as I understand, Google Chrome only loads the page, but does not execute it, but as soon as my server receives GET requests, the counter increases.

Question : Is there a way around this? Preferably, I would like to determine if this is a prefetch or the actual user who visits the website.

+4
source share
1 answer

It looks like this is an undetectable server side.

A problem was open for the Chromium project. It is marked as β€œ will not fix ” ( there are no distinguishing HTTP headers in Prerendering ). The prefetch request does not appear on the Dev Tools tab, so you cannot easily confirm this by looking at the headers. I tested them using Wireshark and, unfortunately, there were no differences distinguishing pre-rendering requests from "normal" requests.

Workaround:

, API . , , script , , prerender. AJAX, - , , .

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

, AJAX , .

+2

Source: https://habr.com/ru/post/1622495/


All Articles