نکاتی درباره‌ی ایجاد صفحات HTML سریع

  • Revision slug: HTML/Tips_for_authoring_fast-loading_HTML_pages
  • Revision title: Tips for authoring fast-loading HTML pages
  • Revision id: 329343
  • Created:
  • Creator: navid-emami
  • Is current revision? خیر
  • توضیح

Revision Content

نکات زیر بر اساس دانش و تجربه به دست آمده‌اند.

یک صفحه‌ی وب بهینه نه تنها برای مخاطب‌های شما تعاملی‌تر است، بلکه روی وب‌سرور و اتصال اینترنت شما نیز تاثیرگذار است. این امر می‌تواند برای سایت‌هایی که ترافیک بالایی دارند مانند سایت‌های خبری، مساله‌ای اساسی باشد.

بهینه‌سازی عملکرد یک صفحه فقط برای کاربران با اینترنت کم سرعت مطرح نیست. بلکه برای آن دسته از کاربران با اینترنت پرسرعت نیز تاثیر به سزایی دارد.

نکته‌ها

کاهش دادن وزن صفحه

وزن صفحه، یکی از مهم‌ترین فاکتورهای بارگیری یک صفحه است.

کاهش وزن صفحه به این معنا است که با حذف کردن جاهای خالی و خطوط کمکی (comment) و با انتقال اسکریپت‌ها و برگه‌های سبک سلسله‌مراتبی به فایل‌های خارجی، سرعت بارگیری صفحه را تا حد امکان افزایش دهیم.

ابزاری مانند HTML Tidy می‌توانند به صورت خودکار جاهای خالی اضافی را از یک سند HTML حذف کنند. ابزار دیگری نیز وجود دارند که فایل‌های جاوااسکریپت را با تکنیک‌های فشرده‌سازی، فشرده کرده و با کاهش حجم آن‌ها باعث افزایش سرعت بارگیری صفحه می‌شوند.

کاهش تعداد فایل‌ها

با کاهش یافتن تعداد فایل‌های مورد نیاز یک صفحه، تعداد درخواست‌های HTTP جهت بارگیری صفحه نیز کاهش می‌یابد.

 

Reducing the number of files referenced in a web page lowers the number of HTTP connections required to download a page.

Depending on a browser's cache settings, it may send an If-Modified-Since request to the web server for each CSS, JavaScript or image file, asking whether the file has been modified since the last time it was downloaded.

By reducing the number of files that are referenced within a web page, you reduce the time required for these requests to be sent, and for their responses to be received.

If you use background images a lot in your css, you can reduce the amount of HTTP look-ups needed by combining the images into one, known as an image sprite. Then you just apply the same image each time you need it for a background, and adjust the x/y coordinates appropriately. This technique works best with elements that will have limited dimensions, and will not work for every use of a background image. However the fewer http requests and single image caching can help reduce pageload time.

Too much time spent querying the last-modified time of referenced files can delay the initial display of a web page, since the browser must check the modification time for each CSS or JavaScript file, before rendering the page.

Reduce domain lookups

Since each separate domain costs time in a DNS lookup, page-load time will grow along with the number of separate domains appearing in CSS link(s) and JavaScript and image src(es).

This may not always be practical; however, you should always take care to use only the minimum necessary number of different domains in your pages.

Cache reused content

Make sure that any content that can be cached, is cached, and with appropriate expiration times.

In particular, pay attention to the Last-Modified header. It allows for efficient page caching; by means of this header, information is conveyed to the user agent about the file it wants to load, such as when it was last modified. Most web servers automatically append the Last-Modified header to static pages (e.g. .html, .css), based on the last-modified date stored in the file system. With dynamic pages (e.g. .php, .aspx), this, of course, can't be done, and the header is not sent.

So, in particular for pages which are generated dynamically, a little research on this subject is beneficial. It can be somewhat involved, but it will save a lot in page requests on pages which would normally not be cacheable.

More information:

  1. HTTP Conditional Get for RSS Hackers
  2. HTTP 304: Not Modified
  3. On HTTP Last-Modified and ETag

Optimally order the components of the page

Download page content first, along with any CSS or JavaScript that may be required for its initial display, so that the user gets the quickest apparent response during page-loading. This content is typically text, and can therefore benefit from text compression in transit, thus providing an even quicker response to the user.

Any dynamic features that require the page to complete loading before being used, should be initially disabled, and then only enabled after the page has loaded. This will cause the JavaScript to be loaded after the page contents, which will improve the overall appearance of the page load.

Reduce the number of inline scripts

Inline scripts can be expensive for page loading, since the parser must assume that an inline script could modify the page structure while parsing is in progress. Reducing the use of inline scripts in general, and reducing the use of document.write() to output content in particular, can improve overall page loading. Use modern AJAX methods to manipulate page content for modern browsers, rather than the older approaches based on document.write().

Use modern CSS and valid markup

Use of modern CSS reduces the amount of markup, can reduce the need for (spacer) images, in terms of layout, and can very often replace images of stylized text -- that "cost" much more than the equivalent text-and-CSS.

Using valid markup has other advantages. First, browsers will have no need to perform error-correction when parsing the HTML. ((This is aside from the philosophical issue of whether to allow format variation in user input, and then programmatically "correct" or normalize it; or whether, instead, to enforce a strict, no-tolerance input format)).

Moreover, valid markup allows for the free use of other tools which can pre-process your web pages. For example, HTML Tidy can remove whitespace and optional ending tags; however, it will refuse to run on a page with serious markup errors.

Chunk your content

Tables for layouts are a legacy method that should not be used any more. Layouts utilizing {{ HTMLElement("div") }} blocks, and in the near future, CSS3 Multi-column Layout or CSS3 Flexible Box Layout, should be used instead.

Tables are still considered valid markup, but should be used for displaying tabular data. To help the browser render your page quicker, you should avoid nesting your tables.

Rather than deeply nesting tables as in:

<TABLE>
  <TABLE>
    <TABLE>
          ...
    </TABLE>
  </TABLE>
</TABLE>

use non-nested tables or divs as in

<TABLE>...</TABLE>
<TABLE>...</TABLE>
<TABLE>...</TABLE>

See also: CSS3 Multi-column Layout Spec and CSS3 Flexible Box Layout

Specify sizes for images and tables

If the browser can immediately determine the height and/or width of your images and tables, it will be able to display a web page without having to reflow the content. This not only speeds the display of the page but prevents annoying changes in a page's layout when the page completes loading. For this reason, height and width should be specified for images, whenever possible.

Tables should use the CSS selector:property combination:

  table-layout: fixed;

and should specify widths of columns using the COL and COLGROUP html tags.

Choose your user-agent requirements wisely

To achieve the greatest improvements in page design, make sure that reasonable user-agent requirements are specified for projects. Do not require your content to appear pixel-perfect in all browsers, especially not in down-version browsers.

Ideally, your basic minimum requirements should be based on the consideration of modern browsers that support the relevant standards. This can include: Firefox 3.6+ on any platform, Internet Explorer 8.0+ on Windows, Opera 10+ on Windows, and Safari 4 on Mac OS X.

Note, however, that many of the tips listed in this article are common-sense techniques which apply to any user agent, and can be applied to any web page, regardless of browser-support requirements.

Example page structure

· HTML

· HEAD
· LINK ...
CSS files required for page appearance. Minimize the number of files for performance while keeping unrelated CSS in separate files for maintenance.
· SCRIPT ...
JavaScript files for functions required during the loading of the page, but not any DHTML that can only run after page loads.
Minimize the number of files for performance while keeping unrelated JavaScript in separate files for maintenance.
· BODY
· User visible page content in small chunks (tables / divs) that can be displayed without waiting for the full page to download.
· SCRIPT ...
Any scripts which will be used to perform DHTML. DHTML script typically can only run after the page has completely loaded and all necessary objects have been initialized. There is no need to load these scripts before the page content. That only slows down the initial appearance of the page load.
Minimize the number of files for performance while keeping unrelated JavaScript in separate files for maintenance.
If any images are used for rollover effects, you should preload them here after the page content has downloaded.

Original Document Information

  • Author(s): Bob Clary, Technology Evangelist, Netscape Communications
  • Last Updated Date: Published 04 Apr 2003
  • Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.
  • Note: This reprinted article was originally part of the DevEdge site.

 

Revision Source

<p dir="rtl">نکات زیر بر اساس دانش و تجربه به دست آمده‌اند.</p>
<p dir="rtl">یک صفحه‌ی وب بهینه نه تنها برای مخاطب‌های شما تعاملی‌تر است، بلکه روی وب‌سرور و اتصال اینترنت شما نیز تاثیرگذار است. این امر می‌تواند برای سایت‌هایی که ترافیک بالایی دارند مانند سایت‌های خبری، مساله‌ای اساسی باشد.</p>
<p dir="rtl">بهینه‌سازی عملکرد یک صفحه فقط برای کاربران با اینترنت کم سرعت مطرح نیست. بلکه برای آن دسته از کاربران با اینترنت پرسرعت نیز تاثیر به سزایی دارد.</p>
<h2 dir="rtl" name="Tips">نکته‌ها</h2>
<h3 dir="rtl" name="Reduce_page_weight">کاهش دادن وزن صفحه</h3>
<p dir="rtl">وزن صفحه، یکی از مهم‌ترین فاکتورهای بارگیری یک صفحه است.</p>
<p dir="rtl">کاهش وزن صفحه به این معنا است که با حذف کردن جاهای خالی و خطوط کمکی (comment) و با انتقال اسکریپت‌ها و برگه‌های سبک سلسله‌مراتبی به فایل‌های خارجی، سرعت بارگیری صفحه را تا حد امکان افزایش دهیم.</p>
<p dir="rtl">ابزاری مانند <a href="http://tidy.sourceforge.net/" title="http://tidy.sourceforge.net/">HTML Tidy</a> می‌توانند به صورت خودکار جاهای خالی اضافی را از یک سند HTML حذف کنند. ابزار دیگری نیز وجود دارند که فایل‌های جاوااسکریپت را با تکنیک‌های فشرده‌سازی، فشرده کرده و با کاهش حجم آن‌ها باعث افزایش سرعت بارگیری صفحه می‌شوند.</p>
<h3 dir="rtl" name="Minimize_the_number_of_files">کاهش تعداد فایل‌ها</h3>
<p dir="rtl">با کاهش یافتن تعداد فایل‌های مورد نیاز یک صفحه، تعداد درخواست‌های <a href="/en-US/docs/HTTP" title="/en-US/docs/HTTP">HTTP</a> جهت بارگیری صفحه نیز کاهش می‌یابد.</p>
<p dir="rtl">&nbsp;</p>
<p>Reducing the number of files referenced in a web page lowers the number of <a href="/en-US/docs/HTTP" title="en-US/docs/HTTP">HTTP</a> connections required to download a page.</p>
<p>Depending on a browser's cache settings, it may send an <code>If-Modified-Since</code> request to the web server for each CSS, JavaScript or image file, asking whether the file has been modified since the last time it was downloaded.</p>
<p>By reducing the number of files that are referenced within a web page, you reduce the time required for these requests to be sent, and for their responses to be received.</p>
<p>If you use background images a lot in your css, you can reduce the amount of HTTP look-ups needed by combining the images into one, known as an image sprite. Then you just apply the same image each time you need it for a background, and adjust the x/y coordinates appropriately. This technique works best with elements that will have limited dimensions, and will not work for every use of a background image. However the fewer http requests and single image caching can help reduce pageload time.</p>
<p>Too much time spent querying the last-modified time of referenced files can delay the initial display of a web page, since the browser must check the modification time for each CSS or JavaScript file, before rendering the page.</p>
<h3 id="Reduce_domain_lookups" name="Reduce_domain_lookups">Reduce domain lookups</h3>
<p>Since each separate domain costs time in a DNS lookup, page-load time will grow along with the number of separate domains appearing in CSS link(s) and JavaScript and image src(es).</p>
<p>This may not always be practical; however, you should always take care to use only the minimum necessary number of different domains in your pages.</p>
<h3 id="Cache_reused_content" name="Cache_reused_content">Cache reused content</h3>
<p>Make sure that any content that can be cached, is cached, and with appropriate expiration times.</p>
<p>In particular, pay attention to the <code>Last-Modified</code> header. It allows for efficient page caching; by means of this header, information is conveyed to the user agent about the file it wants to load, such as when it was last modified. Most web servers automatically append the <code>Last-Modified</code> header to static pages (e.g. <code>.html</code>, <code>.css</code>), based on the last-modified date stored in the file system. With dynamic pages (e.g. <code>.php</code>, <code>.aspx</code>), this, of course, can't be done, and the header is not sent.</p>
<p>So, in particular for pages which are generated dynamically, a little research on this subject is beneficial. It can be somewhat involved, but it will save a lot in page requests on pages which would normally not be cacheable.</p>
<p>More information:</p>
<ol>
  <li><a class="external" href="http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers">HTTP Conditional Get for RSS Hackers</a></li>
  <li><a class="external" href="http://annevankesteren.nl/archives/2005/05/http-304">HTTP 304: Not Modified</a></li>
  <li><a class="external" href="http://www.cmlenz.net/blog/2005/05/on_http_lastmod.html">On HTTP Last-Modified and ETag</a></li>
</ol>
<h3 id="Optimally_order_the_components_of_the_page" name="Optimally_order_the_components_of_the_page">Optimally order the components of the page</h3>
<p>Download page content first, along with any CSS or JavaScript that may be required for its initial display, so that the user gets the quickest apparent response during page-loading. This content is typically text, and can therefore benefit from text compression in transit, thus providing an even quicker response to the user.</p>
<p>Any dynamic features that require the page to complete loading before being used, should be initially disabled, and then only enabled after the page has loaded. This will cause the JavaScript to be loaded after the page contents, which will improve the overall appearance of the page load.</p>
<h3 id="Reduce_the_number_of_inline_scripts" name="Reduce_the_number_of_inline_scripts">Reduce the number of inline scripts</h3>
<p>Inline scripts can be expensive for page loading, since the parser must assume that an inline script could modify the page structure while parsing is in progress. Reducing the use of inline scripts in general, and reducing the use of <code>document.write()</code> to output content in particular, can improve overall page loading. Use modern <a href="/en-US/docs/AJAX" title="en-US/docs/AJAX">AJAX</a> methods to manipulate page content for modern browsers, rather than the older approaches based on <code>document.write()</code>.</p>
<h3 id="Use_modern_CSS_and_valid_markup" name="Use_modern_CSS_and_valid_markup">Use modern CSS and valid markup</h3>
<p>Use of modern CSS reduces the amount of markup, can reduce the need for (spacer) images, in terms of layout, and can very often replace images of stylized text -- that "cost" much more than the equivalent text-and-CSS.</p>
<p>Using valid markup has other advantages. First, browsers will have no need to perform error-correction when parsing the HTML. ((This is aside from the philosophical issue of whether to allow format variation in user input, and then programmatically "correct" or normalize it; or whether, instead, to enforce a strict, no-tolerance input format)).</p>
<p>Moreover, valid markup allows for the free use of other tools which can <em>pre-process</em> your web pages. For example, <a class="external" href="http://tidy.sourceforge.net/">HTML Tidy</a> can remove whitespace and optional ending tags; however, it will refuse to run on a page with serious markup errors.</p>
<h3 id="Chunk_your_content" name="Chunk_your_content">Chunk your content</h3>
<p>Tables for layouts are a legacy method that should not be used any more. Layouts utilizing {{ HTMLElement("div") }} blocks, and in the near future, <a href="/en-US/docs/CSS/Using_CSS_multi-column_layouts" title="en-US/docs/CSS3_Columns">CSS3 Multi-column Layout</a> or <a href="/en-US/docs/Using_flexbox" title="en-US/docs/Using_flexbox">CSS3 Flexible Box Layout</a>, should be used instead.</p>
<p>Tables are still considered valid markup, but should be used for displaying tabular data. To help the browser render your page quicker, you should avoid nesting your tables.</p>
<p>Rather than deeply nesting tables as in:</p>
<pre>
&lt;TABLE&gt;
  &lt;TABLE&gt;
    &lt;TABLE&gt;
          ...
    &lt;/TABLE&gt;
  &lt;/TABLE&gt;
&lt;/TABLE&gt;</pre>
<p>use non-nested tables or divs as in</p>
<pre>
&lt;TABLE&gt;...&lt;/TABLE&gt;
&lt;TABLE&gt;...&lt;/TABLE&gt;
&lt;TABLE&gt;...&lt;/TABLE&gt;
</pre>
<p>See also: <a class="external" href="http://www.w3.org/TR/css3-multicol/" title="http://www.w3.org/TR/css3-multicol/">CSS3 Multi-column Layout Spec</a> and <a class="external" href="http://www.w3.org/TR/css3-flexbox/" title="http://www.w3.org/TR/css3-flexbox/">CSS3 Flexible Box Layout</a></p>
<h3 id="Specify_sizes_for_images_and_tables" name="Specify_sizes_for_images_and_tables">Specify sizes for images and tables</h3>
<p>If the browser can immediately determine the height and/or width of your images and tables, it will be able to display a web page without having to reflow the content. This not only speeds the display of the page but prevents annoying changes in a page's layout when the page completes loading. For this reason, <code>height</code> and <code>width</code> should be specified for images, whenever possible.</p>
<p>Tables should use the CSS selector:property combination:</p>
<pre>
  table-layout: fixed;
</pre>
<p>and should specify widths of columns using the <code>COL</code> and <code>COLGROUP</code> html tags.</p>
<h3 id="Choose_your_user-agent_requirements_wisely" name="Choose_your_user-agent_requirements_wisely">Choose your user-agent requirements wisely</h3>
<p>To achieve the greatest improvements in page design, make sure that reasonable user-agent requirements are specified for projects. Do not require your content to appear pixel-perfect in all browsers, especially not in down-version browsers.</p>
<p>Ideally, your basic minimum requirements should be based on the consideration of modern browsers that support the relevant standards. This can include: Firefox 3.6+ on any platform, Internet Explorer 8.0+ on Windows, Opera 10+ on Windows, and Safari 4 on Mac OS X.</p>
<p>Note, however, that many of the tips listed in this article are common-sense techniques which apply to any user agent, and can be applied to any web page, regardless of browser-support requirements.</p>
<h2 id="Example_page_structure" name="Example_page_structure">Example page structure</h2>
<p>· <code>HTML</code></p>
<dl>
  <dd>
    · <code>HEAD</code></dd>
</dl>
<dl>
  <dd>
    <dl>
      <dd>
        · <code>LINK </code>...<br />
        CSS files required for page appearance. Minimize the number of files for performance while keeping unrelated CSS in separate files for maintenance.</dd>
    </dl>
  </dd>
</dl>
<dl>
  <dd>
    <dl>
      <dd>
        · <code>SCRIPT </code>...<br />
        JavaScript files for functions <strong>required</strong> during the loading of the page, but not any DHTML that can only run after page loads.</dd>
      <dd>
        Minimize the number of files for performance while keeping unrelated JavaScript in separate files for maintenance.</dd>
    </dl>
  </dd>
</dl>
<dl>
  <dd>
    · <code>BODY</code></dd>
  <dd>
    · User visible page content in small chunks (tables / divs) that can be displayed without waiting for the full page to download.</dd>
</dl>
<dl>
  <dd>
    <dl>
      <dd>
        · <code>SCRIPT </code>...<br />
        Any scripts which will be used to perform DHTML. DHTML script typically can only run after the page has completely loaded and all necessary objects have been initialized. There is no need to load these scripts before the page content. That only slows down the initial appearance of the page load.</dd>
      <dd>
        Minimize the number of files for performance while keeping unrelated JavaScript in separate files for maintenance.</dd>
      <dd>
        If any images are used for rollover effects, you should preload them here after the page content has downloaded.</dd>
    </dl>
  </dd>
</dl>
<h2 id="Related_Links" name="Related_Links">Related Links</h2>
<ul>
  <li>Book: <a class="external" href="http://www.websiteoptimization.com/">"Speed Up Your Site" by Andy King</a></li>
  <li><a class="external" href="http://webmonkey.wired.com/webmonkey/design/site_building/tutorials/tutorial2.html">Site Optimization Tutorial</a> (WebMonkey)</li>
  <li>The excellent and very complete <a class="external" href="http://developer.yahoo.com/performance/rules.html" title="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a> (Yahoo!)</li>
</ul>
<div class="originaldocinfo">
  <h2 id="Original_Document_Information" name="Original_Document_Information">Original Document Information</h2>
  <ul>
    <li>Author(s): Bob Clary, Technology Evangelist, Netscape Communications</li>
    <li>Last Updated Date: Published 04 Apr 2003</li>
    <li>Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.</li>
    <li>Note: This reprinted article was originally part of the DevEdge site.</li>
  </ul>
</div>
<p>&nbsp;</p>
Revert to this revision