Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  1. When the user loads the page, the "news feed" container is blank. But the page triggers a javascript which renders the items of the news feed onto the screen. This would tie into Backbone's models and collections, etc.

  2. When the user loads the page, the "news feed" is rendered by the server. Even if javascript was turned off, the items would still show because the server rendered it via a templating engine.

I want to use Backbone.js to keep my javascript clean. So, I should pick #1, right?? But #1 is much more complicated than #2.

By the way, the reason I'm asking this question is because I don't want to use the routing feature of Backbone.js. I would load each page individually, and use Backbone for the individual items of the page. In other words, I'm using Backbone.js halfway.

If I were to use the routing feature of Backbone.js, then the obvious answer would be #1, right? But I'm afraid it would take too much time to build the route system, and time should be balanced into my equation as well.

I'm sorry if this question is confusing: I just want to know the best practice of using Backbone.js and saving time as well.

like image 376
TIMEX Avatar asked Dec 04 '11 00:12

TIMEX


1 Answers

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.

like image 109
Platinum Azure Avatar answered Nov 03 '22 01:11

Platinum Azure