Search code examples
node.jshttphttp-headershttp-streaming

Chrome is not rendering chunked json response


I want to make a streaming endpoint that keeps sending a json object. I have tried using chunked transfer encoding and was able to successfully make all the browsers (chrome, firefox, safari) render the data sent from the server if it was either text/html or text/plain. But when I use application/json as the content-type it does not work on chrome. Here's the code:

var http = require('http');

http.createServer(function(request, response){
    response.setHeader('Connection', 'Transfer-Encoding');
    response.setHeader('Transfer-Encoding', 'chunked');
    response.setHeader('Cache-Control', 'no-cache');
    // response.setHeader('Content-Type', 'application/json');
    response.setHeader('Content-Type', 'text/html');


    setInterval(function(){     
        response.write('test </br>');   
         // response.write('{"test": "test"}');
    }, 10);

}).listen(8000);

The above code works as expected, but cant make it work with the application/json type. Am I missing something ?


Solution

  • This is a bug in browsers. and its fixed in latest chrome(at least in canary).

    Chrome Bug - Transfer-Encoding chunked not support on text/plain

    Safari (i have tested on Mac OSX) is the only browser that is not rendering the non-html content with chunked encoding.