HTTP Flushing in ASP.NET MVC – CourtesyFlush

Sometime back I was working on my project to improve the page load speeds. I was following the regular techniques to improve page speed like bundling and minifying statics content, placing them in proper places and loading content from CDN etc.

While doing these changes I came to know about HTTP Flushing. At that time I did not spend time to understand the uses of HTTP Flush. Recently in one of my projects I wanted to use it to check myself.

I found that Nik Molnar wrote about how to make use of HTTP Flushing in ASP.NET MVC. He also wrote a nuget package called CourtesyFlush and you can check the code here.

When the request hits the Action method and if takes more time ( like DB calls, expensive work) the browser waits until the complete page is ready.

If we flush the part of the page like <head> the browser start loading the content of head element meanwhile Action method completes its request and send the remaining page to the browser. This will improve the user perceived performance of the page.

Here I have created a sample MVC 5 website and added some delay in the “About” action method to mimic its taking time to process.

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            // assume long running action here
            Thread.Sleep(2000);

            ViewBag.Message = "Your description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Check the page load time in dev tools network view. As you can see the scripts and css did not load until full page completed.

BeforeHTTPFlush

Let’s add CourtesyFlush nuget package and try the same page. To make this work you’ll want to move everything which you want flush out of _Layout.cshtml to a new file called _Head.cshtml.

public ActionResult About()
{
this.FlushHead();

	// assume long running action here
	Thread.Sleep(2000);

	ViewBag.Message = "Your application description page.";

	return View();
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

We created a separate _Head partial view. Add this to the _Layout view.

@Html.FlushHead();
<body>

    @* code removed for bravity *@

    @RenderBody()

    @RenderSection("scripts", required: false)
</body>
</html>

Check the network view now. The content of the head section starts loading before page is ready.

BufferAfterHTTPFlush

 

 

 

Git Init – Start learning Git

Recently I started learning/using Git source control for my personal projects. Not yet settled on any particular project but hey, its fun to learn Git at least. I personally use GituHub or Bitbucket for my projects.

Git is a distributed version control system. Meaning every developer can have full source code copy in his machine. You can find a good discussion over here why to use Git over TFS.

Coming from TFS world, Git terms will confuse a little bit initially. So hear are the details I followed to understand some of the common tasks of the Git. The following cheat sheet might help you to start with. Table source

TFS Version Control Git
Workspace Repository (aka. “Repo”)
Get Latest  (First time) Clone
Get Latest (After first time) Pull
Check in Commit + Push
Check out (just start typing)
Branch Branch
Merge Merge
Code Review “pull request”
Shelveset Stash or Branch
Label Tag

I am a single developer on my projects. So chances of merge conflicts are less. But as the number of developers increase in the project you start facing merge conflicts. I am currently using KDiff3 as merge conflict resolver.

Apart from this, start learning Git online in the following links.

https://try.github.io

http://pcottle.github.io/learnGitBranching/

Once you are comfortable, start with these open source projects to use your Git skills.

http://up-for-grabs.net/#/