Description
According to HTTP Archive data, 71% of sites using socket.io (73k out of 103k sites) have BFCache not available because developers are not following the best practices to handle WebSocket connection to allow the browser to cache the page for BFCache.
I wasn't able to find any mention of suggested best practices in the documentation unfortunately.
Anyone would be open to help developers better use WebSockets connections overall?
Read more here: https://web.dev/articles/bfcache#close-open-connections
In order to make BFCache compatible the following things should happen:
- When the page is restored via BFCache, re-open the WebSocket connection at
pageshow
whenevent.persisted==true
- Close the WebSocket connection using
pagehide
.
Unfortunately if developers are not doing 1) and 2) Modern browsers will not be able to store the page for a future Back/Forward navigation, making Page Loads significantly slower to their users when socket.io is used.
Core Web Vitals Web Performance metrics reflects that, leading to a higher LCP (maybe CLS, depends by the site).
Doing so could have a huge impact to users browsing sites when socket.io library is used, also the library itself will not have to load if the page has been restored from the BFCache and the connection would be restored by using the pageshow method.
Similar to this issue: #1660