📜 ⬆️ ⬇️

Page Visibility API and a side effect of redrawing pages

First, a small experiment


To make this small article much more interesting, I suggest simply opening Google search results and immediately return to reading under the cut.


Most likely, the first result of your issue was Amazon.com , the page of which the browser kindly preloaded:


')

Now logging on to Amazon.com , we’ll find a bunch of sweets in the "You've watched" and "Related products":



Yes, Amazon decided that this user went to the product page and began to show the appropriate recommendations. "Worried" can write about the leakage of data on the search, but I'm more concerned about web analytics and development.

Conclusions for developers


In my opinion, prerender is a good thing, but it has side effects that you should now think about when developing projects.

So, Wireshark showed that the backend will not be able to distinguish a prerender request from the usual one, therefore, at a minimum, server logs may consider “dead souls” - visitors who were shown the site as the first result, but there was no actual transition. Since the analytics is implemented on the back end, we received unexpected recommendations, but is it possible to solve the problem with the help of the frontend?

Yes, in Javascript, you can use the Page Visibility API , which tells the state of the page: document.visibilityState = 'prerender' . Thus, trackers and counters can wait for the moment of the first explicit page display and, as a result, analytics and recommendations are quite correct.

Perhaps there are other solutions that I will be glad to read in the comments.

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


All Articles