Google Chrome doesn’t like to stream

Often if you have a long-loading page, you’ll want to stream the content as it arrives. Firefox does a great job of aggressively rendering content, but other browsers like Google Chrome and IE don’t.

A typical workaround for this is to first “wake up” the browser, then periodically flush the output buffer – this has been known for awhile. What’s less known, is that the textual content you’re sending actually matters as well (at least to Chrome and thus likely Safari). E.g., a standard workaround is this:

// start output buffer
if (ob_get_level() == 0) ob_start();
for($i=0;$i<70;$i++)
{
    echo "printing...<br />";
    print str_pad('',4096)."\n";
 
    ob_flush();
    flush();
    usleep(30000);
}

But what’s interesting is that this slight modification breaks it on Chrome:

// start output buffer
if (ob_get_level() == 0) ob_start();
for($i=0;$i<70;$i++)
{
    echo "printing...\n";
    print str_pad('',4096)."\n";
 
    ob_flush();
    flush();
    usleep(30000);
}

Apparently Google engineers think <br>’s are important, but newline characters, not so much.

Leave a comment

Your email address will not be published. Required fields are marked *