JavaScript and the end of progressive enhancement
Progressive enhancement is the Right WayTM to do things in web development. It works like this:
Write the HTML for your content, 100% semantically (i.e. only the necessary tags to explain the meaning of the content).
Style the HTML using CSS. You may need to add hooks to your HTML in the form of classes and ids for the CSS to target.
Add JavaScript enhancements to the interface, but only enhancements.
Parallel to this, the functionality of the site/app is progressively enhanced:
All navigation must happen via links and form submissions - don’t use JavaScript for navigation.
Add JavaScript enhancements to navigation, overriding the links and form submissions, for example to avoid page reloads. Every single link and form element should still work when JavaScript is disabled.
But of course you knew that, because that’s how you roll. I’m not sure this is the way forward though. To explain why I’ll need to start with a bit of (over-simplified) history.
A history lesson
Static pages
Originally, most sites on the web were a collection of static HTML pages saved on a server. The bit of the URL after the domain gave the location of the file on the server. This works fine as long as you’re working with static information, and don’t mind each file having to repeat the header, footer, and any other shared HTML.
Server-side dynamism
In order to make the web more useful, techniques were developed to enable the HTML to be generated on the fly, when a request was received. For a long time, this was how the web worked, and a lot of it still does.
Client-side dynamism
As JavaScript has become more prevalent on the client, developers have started using Ajax to streamline interfaces, replacing page loads with partial reloads. This either works by sending HTML snippets to replace a section of page (thus putting the burden of HTML generation on the server) or by sending raw data in XML or JSON and letting the browser construct the HTML or DOM structure.
Shifting to the client
As JavaScript engines increase in speed, the Ajax bottleneck comes in the transfer time. With a fast JavaScript engine it’s quicker to send the data to the client in the lightest way possible and let the client construct the HTML, rather than constructing the HTML on the server and saving the browser some work.
This raises an issue - we now need rendering code on the client and the server, doing the same thing. This breaks the DRY principle and leads to a maintenance nightmare. Remember, all of the functionality needs to work without JavaScript first, and only be enhanced by JavaScript.
No page reloads
If we’re trying to avoid page reloads, why not take that to its logical conclusion? All the server needs to do is spit out JSON - the client handles all of the rendering. Even if the entire page needs to change, it might be faster to load the new data asynchronously, then render a new template using this new data.
Working this way, a huge amount of functionality would need to be written twice; once on the server and once on the client. This isn’t going work for most people, so we’re left with two options - either abandon the “no reload” approach or abandon progressive enhancement.
The end of progressive enhancement
So, where does this leave things? It depends on how strongly you’re tied to progressive enhancement. Up until now, progressive enhancement was a good way to build websites and web apps - it enforced a clean separation between content, layout, behaviour and navigation. But it could be holding us back now, stopping the web moving forward towards more natural interfaces that aren’t over-burdened by its history as something very different to what it is today.
There are still good reasons to keep using progressive enhancement, but it may be time to accept that JavaScript is an essential technology on today’s web, and stop trying to make everything work in its absence.
Or maybe not
I’m completely torn on this issue. I wrote this post as a way of putting one side of the argument across in the hope of generating some kind of discussion. I don’t know what the solution is right now, but I know it’s worth talking about. So let me know what you think!
Appendix: Tools for this brave new world
Backbone.js is a JavaScript MVC framework, perfectly suited for developing applications that live on the client. You’ll want to use some kind of templating solution as well - I use jQuery.tmpl (I’ve written a presentation on the topic if you’re interested in learning more) but there are lots of others as well.
Sammy.js (suggested by justin TNT) looks like another good client-side framework, definitely influenced by Sinatra.
If anybody has any suggestions for other suitable libraries/frameworks I’ll gladly add them in here.