DEV Community

Kumar Swapnil
Kumar Swapnil

Posted on

A step towards a faster Web: Early flushing in c#.net

A simple demonstration to Early flushing in dotnet.

Flushing, Early flushing, head flushing or Progressive HTML is when the server sends the initial part of the HTML document to the client before the entire response is ready. All major browsers start parsing the partial response.If done correctly, the browser won't sit idle after requesting your page, rather, it can start process other important things in the meantime, like requesting static assets which would be used later on the site. It could give a significant perceived performance gain.
In this given example, I have used Thread.Sleep(200). This could be the time where your page does heavy database calls and other computation.

HomeController.cs

public ActionResult Index()
{

  PartialView("/Views/Shared/_HeadPart.cshtml").ExecuteResult(ControllerContext);
  Response.Flush();
  Thread.Sleep(200);
  return PartialView("/Views/Home/Index.cshtml");
}

_HeadPart.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <title>Hey - My ASP.NET Application</title>
</head>

Index.cshtml

<body>
    <div class="row">
        Hey there, How u doin'?
    </div>
</body>
</html>

Results With Early Flushing:

With Early flushing

Results Without Early Flushing

Without Early flushing

Top comments (0)