Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rel PreRender/PreFetch, does it execute JS?

<!—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 to prerender/prefetch (such as Google Analytics JS), will the prerender/prefetch execute Javascript on the page? Or does it delay any execution of JS until the page is actually requested by the user?

like image 415
Tom Gullen Avatar asked May 15 '12 14:05

Tom Gullen


1 Answers

I've just done some more research on this:

Prefetch will load the top level resource which is often simply the HTML page

Prerender will fetch child elements as well, and will execute 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 is on Chrome and prerender is being used. Also, the current version of Google Analytics makes full use of the Page Visibility API so statistics wont be skewed, however it's likely there is a lot of code in the wild whose statistics are being skewed by not properly handling cases of prerendering.

like image 92
Tom Gullen Avatar answered Sep 18 '22 11:09

Tom Gullen