Facebook 如何如此快速地呈现其页面

发布于 2024-12-28 11:48:25 字数 1564 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2025-01-04 11:48:25

您将希望使用这种类型的范例来获得最佳的用户体验。

AJAX 是您的朋友

仅发送向用户显示页面轮廓所需的最少信息。

<html>
<head>
...scripts, meta, css etc...
</head>
<body>
<h1>Facebook Info</h1>
<div id=facebook-info>
<p>loading...</p>
</div>

<h1>Twitter Info</h1>
<div id=twitter-info>
<p>loading...</p>
</div>

</body>
</html>

然后在每个内容部分(您尚未加载)都有一个微调器。

在加载 javascript 时(为了清楚起见,我缩写了......请参阅 http://jQuery.com/ 有关其 ajax 调用的更多信息),您可以调用输出一些 JSON 的特定 php 页面。这是真正的工作发生并需要时间的地方。

<script>
$(document).ready(function(){
   $.ajax('/content/getFacebookInfo.php',displayFacebookContent);
   $.ajax('/content/getTwitterInfo.php',displayTwitterContent);
});
</script>

然后在 displayFacebookContent 和 displayTwitterContent 回调函数中,您可以在此处动态构建 DOM 内容。

<script>
var displayTwitterContent = function(response) {
  var html = "<ul>";
  foreach(var i in response.posts) {
     html += "<li>" + response.posts[i].message + "</li>";
  }

  html += "</ul>";
  $('#titter-info).append(html);
};
</script>

再次强调,这只是为了展示概念,您必须充实脚本并构建用 JSON 对象响应的 php 处理程序。

通过将艰苦的工作转移给 AJAX 处理程序,可以快速提供主要 HTML,并且用户会感到满意。当他的眼睛在页面上移动时,一些 AJAX 调用将完成,您将动态地将可用数据插入到 DOM 中,他们会看到它。

这有点像谷歌在您键入时进行的搜索,它会向服务器发出 AJAX 回调,并在您单击“提交”之前抓取内容以供您实时显示。

还研究应用程序级别和 Web 服务器级别的服务器端缓存。您还可以在网络级别进行一些缓存。

You will want to use this type of paradigm for getting the best user experience.

AJAX is your friend

Only send down the minimal information you need to get the outline of the page displayed to the user.

<html>
<head>
...scripts, meta, css etc...
</head>
<body>
<h1>Facebook Info</h1>
<div id=facebook-info>
<p>loading...</p>
</div>

<h1>Twitter Info</h1>
<div id=twitter-info>
<p>loading...</p>
</div>

</body>
</html>

Then in each of the content sections (that you have yet to load) have a spinner.

In the loading of your javascript (I'm abbreviating for sake of clarity...see http://jQuery.com/ for more info on their ajax calls), you can make calls to specific php pages that spit out some JSON. This is where the real work happens and takes the time.

<script>
$(document).ready(function(){
   $.ajax('/content/getFacebookInfo.php',displayFacebookContent);
   $.ajax('/content/getTwitterInfo.php',displayTwitterContent);
});
</script>

Then in the displayFacebookContent and displayTwitterContent callback functions, this is where you build the content for the DOM dynamically.

<script>
var displayTwitterContent = function(response) {
  var html = "<ul>";
  foreach(var i in response.posts) {
     html += "<li>" + response.posts[i].message + "</li>";
  }

  html += "</ul>";
  $('#titter-info).append(html);
};
</script>

Again, this is abbreviated to show the concept, you will have to flesh out the scripting and also build the php handlers that respond with JSON objects.

By offloading the hard work to the AJAX handlers the main HTML is served down quickly and the user is happy. By the time his eyes move around the page, some of the AJAX calls will have completed and you'll have dynamically inserted useable data into the DOM and they'll see it.

It's kinda like google's search as you type, it's making an AJAX call back to the server and grabbing content for you to display in real time before you even click submit.

Also research into server-side caching at the application level as well as the webserver level. There's also some caching you can do at the network level too.

输什么也不输骨气 2025-01-04 11:48:25

我对 Facebook 的内部结构已经不太熟悉了,但是有一些方法和技术可以实现这些事情:

  • 如果你查看源代码,你会发现在正常环境中 Facebook 仅加载 JavaScript 源文件。快速 JavaScript 密集型应用程序所做的通常是加载静态压缩的 JavaScript(包括视图文件),然后仅请求 JSON 中的动态数据,然后将其呈现在客户端上。我最喜欢的仍然是编译速度非常快的 EJS
  • 在支持 HTML5 的浏览器中,他们可能使用 HTML 5 历史记录。生成的 URL 还指向静态页面,因此当直接请求时,您不会丢失任何真实信息。作为后备,您可以执行基于哈希的历史记录,这仍然使后退按钮保持工作状态。
  • 这只需使用 AJAX 即可完成

I am not extremely familiar with the Facebook internals anymore but there are ways and technologies for these things:

  • If you look at the source you will see that in a normal environment Facebook loads only JavaScript source files. What fast JavaScript heavy applications do is usually load the static compressed JavaScript (including view files) and then only request the dynamic Data in JSON and then render it on the client. My favorite is still the really fast compiled EJS.
  • In HTML5 ready browsers they probably use the HTML 5 history. The generated URL also point to a static page so when requested directly you don't loose any real information. As a fallback you can do hash based history which still keeps the back button working.
  • This is simply done using AJAX
高跟鞋的旋律 2025-01-04 11:48:25

在所有基础设施之间,他们使用 CassandraMemcached,用 C++ 等编译的 PHP。 此处是 Facebook 基础设施的完整描述。

Between all infrastructure, they use Cassandra, Memcached, PHP compiled in C++ and others. Here a complete description of Facebook infra.

余罪 2025-01-04 11:48:25

最后我找到了解决方案。他们广泛使用缓存技术和 jQuery 历史记录插件,该插件支持历史记录,从而使其对搜索引擎友好。

可以在这里找到
http://itswadesh。 wordpress.com/2012/03/29/facebook-type-vertical-navigation-menu-created-with-jquery/

Finally i found out the solution. They use caching technology extensively with jQuery history plugin which enables history hence making it search engine friendly.

Can be found here
http://itswadesh.wordpress.com/2012/03/29/facebook-type-vertical-navigation-menu-created-with-jquery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文