Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect connection protocol (HTTP/2, spdy) from javascript

Is it possible to get the protocol that the browser used to get the active page?
Something like:

performance.navigation.protocol // e.g. "HTTP/2" or "SPDY/3.1" or "HTTP/1.1"

I know that it's possible to detect the protocol server side and then pass the info, but I'm looking for a JS solution.

(a similar question contains a broken link and no answer)

like image 565
Razor Avatar asked Mar 16 '16 16:03

Razor


1 Answers

It is being standardised as performance.timing.nextHopProtocol, but chrome has a non-standard implementation already under window.chrome.loadTimes().connectionInfo:

if ( window.performance && performance.timing.nextHopProtocol ) {
    console.log('Protocol:' + performance.timing.nextHopProtocol);
} else if (window.performance && window.performance.getEntries) {
    console.log(performance.getEntries()[0].nextHopProtocol);
} else if ( window.chrome && window.chrome.loadTimes ) {
    console.log('Protocol:' + window.chrome.loadTimes().connectionInfo);
} else {
    console.log("Browser does not expose connection protocol");
}
like image 132
Razor Avatar answered Oct 11 '22 02:10

Razor