-4

I have seen many sites rendering json data in string format along with html in their page response:

Take an instance of this for example: view-source:https://www.netflix.com/in/

enter image description here

What is the benefit of rendering JSON in string format in page response itself. One can render the page and can load data via ajax too when the page has rendered.

I am thinking to build a SPA website totally on Angular with no server controls on my page, all data coming via REST API.

What should be my approach on page rendering:

  1. Load the page (kind of blank) and then do a hit via ajax calls via Angular and fill the data into the page. This could have loading effect initially.

  2. Load everything in html form page initially via server side code. No ajax request on load of the page this time. If a user wants some more data I can do an ajax request via Angular further.

  3. Load JSON, the data for the page (in stringify format in script tags) + some html. The JSON data will be used to by Angular for templating and render the html there only. No ajax request is made this time too on page load since I rendered the data in json format along with page source. This is the case I posted earlier with Netflix url.

What should be the best approach based on usability issue. I know Angular is great, but what is the best approach here to make a SPA.

Raghav
  • 8,772
  • 6
  • 82
  • 106
  • 1
    I wonder why do the questions get negative vote like this. Can someone suggest what did I ask "off the topic"? I came up to some ideas on implementing some feature and I could not get precise solution via searching Google that is why I posted here to get brilliant ideas from this wonderful community. – Raghav Sep 02 '16 at 11:06
  • 5
    downvote without a comment is totally unhelpful – Iván Rodríguez Torres Sep 02 '16 at 11:07
  • 2
    All three approaches you've listed are viable, and you're asking for opinions which is the best one. Those kind of questions aren't really suitable for SO. http://stackoverflow.com/help/dont-ask – JJJ Sep 02 '16 at 11:10
  • You should read the answer to this question in the following link http://stackoverflow.com/questions/21862054/single-page-application-advantages-and-disadvantages – Pritish Vaidya Sep 02 '16 at 11:48
  • That link doesn't represent what I asked. – Raghav Sep 02 '16 at 12:23
  • 2
    This question is heavily opinion based, and not strictly suitable for SO... – Iain J. Reid Sep 02 '16 at 22:11
  • At least I could any opinion... – Raghav Sep 02 '16 at 23:49
  • 2
    @IvanRodriguezTorres let's just say that a screenshot of code is something that never should be done. – John Dvorak Sep 03 '16 at 13:34

2 Answers2

4

What is the benefit of rendering JSON in string format in page response itself. One can render the page and can load data via ajax too when the page has rendered.

What should be the best approach based on usability issue. I know Angular is great, but what is the best approach here to make a SPA.

You need to balance two opposite requirements:

  • the more data you send (either as formatted view or as information to enrich the view), the faster the application will be ready and the less you will have to rely on further AJAX calls.
  • the more data you send, the longer it will take to load the application.

So there is not one rule that fits all. As a rule of thumb, data that you're going to need anyway can just as well be loaded from the start. Where you load it and how you do so depends on its nature.

Data that might be needed - but then again, not - is better left not loaded, and deferred to further AJAX calls should they prove necessary.

Another important factor is how you're serving your data and to whom. Browsers supporting request pipelining, for example, can make multiple AJAX calls scalable enough for your purposes. If you don't have this advantage, you might find it useful to do the pipelining yourself, multiplexing several AJAX calls into one to reduce latency and often improving compression performances. This, on the other hand, impacts on application flexibility because it increases de facto coupling between functions.

With this in mind let us see your options:

  1. Load the page (kind of blank) and then do a hit via ajax calls via Angular and fill the data into the page. This could have loading effect initially.
  2. Load everything in html form page initially via server side code. No ajax request on load of the page this time. If a user wants some more data I can do an ajax request via Angular further.
  3. Load JSON, the data for the page (in stringify format in script tags) + some html. The JSON data will be used to by Angular for templating and render the html there only. No ajax request is made this time too on page load since I rendered the data in json format along with page source.

We need to distribute the content between these three strategies.

  • HTML that's not very repetitive, and is going to be immediately needed anyway -- why load it via AJAX? Better to include it from the start. What is to be gained loading it later, except latency?
  • HTML which is not very repetitive, but is not immediately needed, can be loaded in the background as HTML. This speeds up initial page load, improving the application responsivity.
  • HTML which is very repetitive (lists, etc.) might be more efficiently divided into an HTML template (one list entry) and the data needed for its rendering, to be sent as JSON. The difference in weight between the fully developed single HTML list entry, and the JSON data it distills into, indicates the desirability of this option. Clearly if the size of the JSON is the same (or maybe even more) than the HTML, there's no point in sending it as JSON. Except if this could help lessen the load on the web server; for busy servers this can be a factor. Also, if you need to update the list afterwards, you're already going to have to template the data. Sending it in both HTML and JSON forms is prone to errors. Better have only one rendering mechanism.

Also, consider the lifetime of the information. Different pieces of your application might have different shelf lives: the template for a list entry is not likely to change too often, while the list values might. By bundling all long-lived resources together, you maximise the bang for the buck from the various caching mechanisms (browser, proxies, etc.) involved.

This has to be kept in mind if you for example decide to bundle some resources (such as icons, etc.) as inlined base64 values. While you do reduce the number of HTTP calls required, you might increase disproportionately the traffic needed by those same calls.

Say you have one page with ten icons and a CSS:

unprimed web page, unbundled        12 HTTP calls for 80 Kb
  primed web page, unbundled         1 HTTP call for 4 Kb
unprimed web page, bundled           3 HTTP calls for 108 Kb
  primed web page, bundled           2 HTTP calls for 75 Kb

Bundling the resources together improves responsivity for the unprimed user, the first time he fires up the application. Then caching steps in, but the data package that needs to be sent every time is still quite large.

The unbundled version is worse for the first-time user, but on subsequent reloads most information is already stored client-side and generates cache hits and no traffic, yielding much better results than the bundled version.

The point here is that organizing data differently, or having a differently architectured application, could just as well give exactly the opposite result, with a strong indication for bundling everything (or almost everything).

TL;DR

I guess what this all boils down to is, You need to have a clear model of what the application is going to do first. Dividing the data cake between the different strategies needs must come later.

LSerni
  • 55,617
  • 10
  • 65
  • 107
2

I believe that you are asking which is better out of the three below options:

  1. Client side rendering
  2. Server side rendering
  3. Client side rendering with datablocks

Some problems with option 3 are:

  • it is polluting the global window object
  • it would be hard to maintain

A better approach would be to use constant or a service to store the values in a variable.

Option 1 vs option 2 depends on the data. If there is complicated logic to retrieve it, lots of data to process to get it etc., then get it from a service.

If the data can be in the client with no issues, then consider the below points and choose which one you think is best:

  • Option 1 (save data in variable) - simple and faster, but need to touch logic code.
  • Option 2 (retrieve data from service call) - separation of data and no need to touch logic, but separate service call

Some ways to improve option 1 for configuration data like environment variables are:

  • Let your CI/CD inject the correct configuration settings
  • Generate your angular configuration on the server dynamically using something like gulp-ng-config

In the end, the answer depends on your particular scenario and requirements. Personally, I would just choose between option 1 and option 2 as considering option 3 seems like premature optimization. The more important thing is to make sure you are optimizing your code, properly, i.e. bundling it etc,

ScottL
  • 1,755
  • 10
  • 7