Skip to content Skip to sidebar Skip to footer

Should I Render This Template Using Javascript Or The Server?

I'm rendering a news feed. I'm planning to use Backbone.js for my javascript stuff because I'm sick of doing manual DOM binds with JQuery. So right now I'm looking at 2 options.

Solution 1:

There are advantages and disadvantages to both, so I would say this: choose the option that is best for you, according to your requirements.

I don't know Backbone.js, so I'm going to keep my answer to client- versus server-side rendering.

Client-side Rendering

This approach allows you to render your structure quickly on the server-side, then let the user's JavaScript pick up the actual content.

Pros:

  • Quicker perceived user experience: if there's enough static content on the initial render, then the user gets their page back (or at least the beginning of it) quicker and they won't be bothered about the dynamic content, because in all likelihood that will render reasonably quickly too.
  • Better control of caching: By requiring that the browser makes multiple requests, you can set up your server to use different caching headers for each URL, depending on your requirements. In this way, you could allow users to cache the initial page render, but require that a user fetch dynamic (changing) content every time.

Cons:

  • User must have JavaScript enabled: This is an obvious one and I shouldn't even need to mention it, but you are cutting out a (very small) portion of your user base if you don't provide a graceful alternative to your JS-heavy site.
  • Complexity: This one is a little subjective, but in some ways it's just simpler to have everything in your server-side language and not require so much back-and-forth. Of course, it can go both ways.
  • Slow post-processing: This depends on the browser, but the fact is that if a lot of DOM manipulation or other post-processing needs to occur after retrieving the dynamic content, it might be faster to let the server do it if the server is underutilized. Most browsers are good at basic DOM manipulation, but if you have to do JSON parsing, sorting, arithmetic, etc., some of that might be faster on the server.

Server-side Rendering

This approach allows the user to receive everything at once and also caters to browsers that don't have good JavaScript support, but it also means everything takes a bit longer before the browser gets the first <html> tag.

Pros:

  • Content appears all at once: If your server is fast, it will render everything all at once, and that's that. No messy XmlHttpRequests (does anyone still use those directly?).
  • Quick post-processing: Just like you wouldn't want your application layer to do sorting of a database queryset because the database is faster, you might also want to reserve a good amount of processing on the server-side. If you design for the client-side approach, it's easy to get carried away and put the processing in the wrong place.

Cons:

  • Slower perceived user experience: A user won't be able to see a single byte until the server's work is all done. Sure, the server is probably going to zip through it, but it's still a few extra seconds on the user's side and you would do them a favor by rendering what you can right away.
  • Does not scale as well because server spends more time on requests: It might be that you really want the server to finish a request quickly and move on to the next connection.

Which of these are most important to your requirements? That should inform your decision.

Solution 2:

I don't know backbone, but here's a simple thought: if at all possible and secure, do everything on the client instead of the server. That way the server has less work to do and can therefore handle more connections and scale better.

Solution 3:

But #1 is much more complicated than #2.

Not really. Once you get your hang of Backbone and jQuery and client-side templating (and maybe throw CoffeeScript into the mix, too), then this is not really difficult. In fact, it greatly simplifies your server code, as all the display-related functions are now removed. You could also even have different clients (mobile version, for example) running against the same server.

Even if javascript was turned off, the items would still show because the server rendered it via a templating engine.

That is the important consideration here. If you want to support users without Javascript, then you need a non-JS version.

If you already have a non-JS version, you can think about if you still need the "enhanced" version, and if you do, if you want to re-use the server-side templating you already have coded and tested and need to maintain anyway, or duplicate the effort client-side, which adds development cost, but as you say may provide a superior experience and lower the load on the server (although I cannot imagine that fetching rendered data versus fetching XML data makes that much of a difference).

If you do not need to support users without Javascript, then by all means, render on the client.

Solution 4:

I think Backbone's aim is to organize a Javascript in-page client application. But first of all you should take a position on the next statement:

Even if javascript was turned off, the web-app still works in "post-back mode".

Is that one of your requirements? (This is not a simple requirement.) If no, then I'll advice you: "Do more JS". But if yes then I believe your best friend is jQuery load function.

A Note: I'm a Java programmer and there's a lot of server-side frameworks that bring the ability to write applications that work ajax-ly when js is enabled and switch on post-backs when it isn't. I think Wicket and Echo2 are two of them but it's meant they are server-side libraries...

Post a Comment for "Should I Render This Template Using Javascript Or The Server?"