开发者

Best Practices for Optimizing Dynamic Page Load Times (JSON-generated HTML)

I have a Rails app where I load up a base HTML layout and I fill in the main content with rows of divs from JSON. This works in 2 steps:

  1. Render the HTML
  2. Ajax call to get the JSON

This has the benefit of being able to cache the HTML layout which doesn't change much, but it seems to have more drawbacks:

  1. 2 HTTP requests
  2. HTML isn't that complex, the generated html is where all the work is done, so I'm not saving that much on time probably.
  3. Each request in my specific case requires that we check the current user, their roles, and some things related to that user, so those 2 calls are somewhat involved.

Granted, memcached will probably solve a lot of this, I am wondering if there are some best practices here. I'm thinking I could do this:

  • Render the first page of JSON inline, in a script block, along with the HTML. This would cut out those 2 server calls requiring user authentication. And, assuming 80% of the time you don't need to make the second ajax call (paginati开发者_运维百科on/sorting in this case), that seems like a fairly good solution.

What are your thoughts on how to approach this?


There are advantages and disadvantages to doing stuff like this. In general I'd say it's only a good idea, if whatever you're delaying via an ajax call would delay the page load enough to annoy the end user for most of the use cases on your page.

A good example of this is browsing a repository on github. 90% of the time all you want is to navigate the files, so they use an ajax load to fill in the commit messages per file after the page load.

It sounds like you're trying to do this to speed up or do something fancy for your users, but I think you should consider instead, what part is slow, and what speed of page load (and maybe for what information on that page) on your users are expecting. As you say, using memcached or fragment caching might well give you the improvements you're looking for.


Are you using some kind of monitoring tool? I'm using the free version of New Relic RPM on Heroku. It gives a lot of data on request times for individual controller actions. Data like that could help you focus your optimization process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜