Detecting HTTP2 / SPDY support in browser

Is it possible to detect browser support for HTTP2 / SPDY client side from the browser?

I am trying to show users if their browser supports HTTP2 / SPDY or will use the traditional HTTP / SPDY protocol HTTP.

+3


source to share


2 answers


Thanks, Patrick. I took your advice and used nginx variable $http2

and used PHP to return dynamic JS variable for browser detection. (Passing a cookie or adding a response header for AJAX detection are also options if other readers prefer).

NGINX configuration additions

server {
    ...

    if ($http2) { 
        rewrite ^/detect-http2.js /detect-http2.js.php?http2=$http2 last;
    }
    # fallback to non-HTTP2 rewrite
    rewrite ^/detect-http2.js /detect-http2.js.php last;

    # add response header if needed in the future
    add_header x-http2 $http2;
}

      

detect-http2.js.php



<?
    header('content-type: application/javascript');
    if (isset($_REQUEST['http2'])) {
        echo "var h2Version='". $_REQUEST['http2'] . "';\n";
    }
?>

      

detect-http2.html

<html>
    <body>
        <script src="https://DOMAIN_NAME/detect-http2.js"></script>
        <script>
            document.write('HTTP2-Supported Browser: '+ (typeof h2Version !== 'undefined'));
        </script>
    </body>
</html>

      

+1


source


No, not at all. At least in a way that makes sense or makes sense.

Front-end javascript will work after the server has already executed all assets. Anything you want to do will be done on the server side. An SPDY compatible browser should automatically negotiate with the SPDY server.



All you have to do is configure it for this ( nginx and apache ). You can also send the header Alternate-Protocol

using your https responses. This will give the browser an answer to future SPDY requests if possible (I can't find it in the updated SPDY spec, so it might be outdated information. Take it with a grain of salt).

If you want to know if a site has been rendered with SPDY, there is chrome.loadTimes () in chrome. wasFetchedViaSpdy (obviously only works in chrome). For firefox and safari you will need to check the headers (as far as I know, there are no similar apis, although plugins exist to do this for you). SPDYCheck is another great resource for verifying that your server is configured correctly.

+2


source







All Articles