Rel PreRender / PreFetch, does JS execute?

<!โ€”Firefox Prefetching --> <link rel="prefetch" href="http://www.example.com/page2.html"> <!โ€”Chrome Prefetching --> <link rel="prerender" href="http://www.example.com/page2.html"> 

If I had Javascript on the page for prerender / prefetch (e.g. JS Google Analytics), would preloading prerender / prefetch execute Javascript on the page? Or does this delay JS execution until the user requests a page?

+6
source share
3 answers

I just did some research on this:

Prefetch load a top-level resource, which is often an HTML page.

Prerender will also select the children and execute the Javascript code. Using the page visibility API, we can determine the current visibility state of the loaded page.

So, the answer is yes, it will execute JS as long as the user uses Chrome and prerender. In addition, the current version of Google Analytics makes full use of the page visibility API, so the statistics will not be distorted, however, probably, in the wild there is a lot of code whose statistics are distorted due to improper handling of prerdings.

+7
source

No, preloading only the cache file, it does not execute the javascript that it contains.

As I know, analytics.js does not analyze the website when you request it. It works when javascript is executed. When the prefetch browser (request) executes the analytics.js file, which does not mean that the analytics.js file is executing. Thus, the Google Analytics data for your site will not be affected by the prefetch method until the user requests a page.

+1
source

On the topic, the specification does not work and simply states:

The prefetch keyword indicates that proactively retrieving and caching the specified resource is likely to be useful because it is likely that the user will need this resource.

From this we can say that the execution of JS on preloaded pages is undefined.

Regardless of whether the browser provider sees a benefit when running JS on preloaded pages, the provider does not work.

+1
source

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


All Articles