I wanted to talk about a trend I’ve been seeing as a web developer lately. And by web developer, I mean strictly in the .NET space, meaning technologies like ASP.NET MVC and to a lesser extent, Web Forms. So what is this trend? I’m glad you asked.
The trend seems to be the move from server-side heavy development to client-side development involving frameworks like jQuery, KnockoutJS, Backbone and others. It used to be that the bulk of a web developers work would be on the back-end; creating the data layers, the business logic layer, and then throwing all that data to the client where it would get rendered. The page would be requested, and before sending anything to the browser, you’ve done a database call (or two, or three, etc.), constructed the data as needed to render, then send the entirely processed chunk of HTML to the browser. You might have some JavaScript on the client side to hide this or that depending on usage, but that was it. All of the business logic as well as the rendering of HTML happened on the server and was sent to the client. Even with MVC this was still happening, as views are processed to produce the proper HTML on the server, which is then sent down to the client.
But now, it seems like the back-end has become nothing more than a data repository, and the bulk of the work has been shifted to the client to process data and render it. What I’m seeing more and more (and what I’ve started to do in some of my own work), is when a page is requested, very little HTML is sent to the browser. I’ve seen some projects where the entire HTML for a given page was generated in JavaScript on the client, and the only HTML sent in the initial response from the server was a wrapper div or two. Everything else happened client side. Talk about dynamic.
Even now, you don’t even have to hit the database for any data when a page is requested. You can essentially send down a skeleton of HTML, and then when the browser gets the response, make an AJAX request to a service endpoint (WCF, old school ASMX, .NET Web API, choose your poison) to get a chunk of JSON, then construct the HTML you need from that. And I’ve been leaning more and more towards that method of web development. Why? Can’t really say for sure, at least not right now. It just feels right to do it that way. It makes sense to me.
It has prompted me to ask this question: Is it better to send data with the initial request, or make the request after the page has been loaded by the browser? That might not make sense, let me clarify a bit.
Let’s say you have an MVC project. You have a controller with an Index() action. At this point, you can do one of two things:
- You can go to the database to get whatever data you need, create a view model (if necessary), and provide a view name which processes the model to create the final chunk of HTML to send to the browser.
- You bypass the database, simply provide a view name which contains a skeleton of HTML, and send that response. When it’s received by the browser, you have JavaScript that makes an AJAX call to get JSON data from a service (or another controller endpoint), and then you process that JSON data to construct your HTML. Client frameworks like KnockoutJS and Backbone make this exceptionally (and in some cases, magically) easy to do.
So are there benefits to doing one over the other? I haven’t explored that question much and don’t have any insight/answers at this point in time. But I thought it would make for good discussion. I would like to get some feedback. Feel free to leave a comment with your thoughts.