Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome prerendering feature causes problems to interactive pages

For the newer Google chrome browsers, i.e. version 17+ (not sure if other browsers do this too), it seems that by default a pre-rendering feature (see http://support.google.com/chrome/bin/answer.py?hl=en&answer=1385029) is enabled, which means that a page is being loaded even before the user finishes typing the url in the address bar!

I guess this is a fine trick for pages with mostly static contents, which deceives the users to make the page load faster by effectively loading it earlier. However, this is problematic for pages which triggers some action/logic upon loading.

For example, consider a page which establishes a websocket connection to the server and sends a msg as soon as it is loaded. While the user is typing the url for this page, the connection may well already be made and msg is sent, but when the user actually presses 'enter' in the address bar, the websocket connection is lost and re-established again, and the msg is re-sent, probably because websocket treats the actual page load as a refresh. Now, if you have some logic that responds to disconnection or receiving certain msgs, this may cause confusing behaviors at the server-end. Another example may be redirections, in which case a double redirection may happen because the page is effectively loaded twice.

Is there any effective way to deal with this browser feature, apart from turning it off which is something that is out of developers' control? Some JavaScript tricks?

like image 503
skyork Avatar asked Aug 05 '26 14:08

skyork


1 Answers

See https://developers.google.com/chrome/whitepapers/pagevisibility

Use document.webkitVisibilityState to check if it's "prerender" state. Don't connect websocket if it's "prerender" state.

Hope this helps. Cheer!

like image 113
Nites Avatar answered Aug 08 '26 06:08

Nites



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!